-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5427.cpp
More file actions
93 lines (83 loc) · 1.69 KB
/
5427.cpp
File metadata and controls
93 lines (83 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <queue>
#include <tuple>
using namespace std;
int _x[4]={0,0,-1,1};
int _y[4]={1,-1,0,0};
char map[1001][1001];
int test[100];
queue<tuple<int,int,int> > fire;
queue<tuple<int,int,int> >sang;
int main(){
int cas;
scanf("%d",&cas);
int x,y;
for(int c=0;c<cas;c++){
while(!fire.empty()){
fire.pop();
}
while(!sang.empty()){
sang.pop();
}
scanf("%d %d",&y,&x);
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
scanf(" %c",&map[i][j]);
if(map[i][j]=='@'){
sang.push(make_tuple(i,j,0));
}else if(map[i][j]=='*'){
fire.push(make_tuple(i,j,0));
}
}
}
int time=0;
while(!sang.empty()){
while(!fire.empty()&&get<2>(fire.front())==time){
int pox=get<0>(fire.front());
int poy=get<1>(fire.front());
for(int i=0;i<4;i++){
int xx=_x[i]+pox;
int yy = _y[i]+poy;
if(xx>=0&&xx<x&&yy>=0&&yy<y){
if(map[xx][yy]=='@'||map[xx][yy]=='.'){
map[xx][yy]='*';
fire.push(make_tuple(xx,yy,time+1));
}
}
}
fire.pop();
}
while(!sang.empty()&&get<2>(sang.front())==time){
int pox=get<0>(sang.front());
int poy=get<1>(sang.front());
if(pox==0||poy==0||pox==x-1||poy==y-1){
test[c]=time+1;
while(!sang.empty()){
sang.pop();
}
break;
}else{
for(int i=0;i<4;i++){
int xx=_x[i]+pox;
int yy = _y[i]+poy;
if(xx>=0&&xx<x&&yy>=0&&yy<y){
if(map[xx][yy]=='.'){
map[xx][yy]='@';
sang.push(make_tuple(xx,yy,time+1));
}
}
}
}
sang.pop();
}
time++;
}
}
for(int i=0;i<cas;i++){
if(test[i]==0){
printf("IMPOSSIBLE\n");
}else{
printf("%d\n",test[i]);
}
}
}