-
Notifications
You must be signed in to change notification settings - Fork 1
/
1066 - Gathering Food.cpp
86 lines (82 loc) · 1.91 KB
/
1066 - Gathering Food.cpp
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
#include<bits/stdc++.h>
using namespace std;
struct pos
{
char ch;
int x;
int y;
pos(char c,int a, int b)
{
ch = c;
x = a;
y = b;
}
};
bool operator < (pos a, pos b)
{
return a.ch < b.ch;
}
vector<pos> pv;
int dirx[] = {1,-1,0,0};
int diry[] = {0,0,1,-1};
char grid[12][12];
int vis[12][12];
int bfs(int x, int y, int f, int g,int n)
{
queue<int> Q;
grid[x][y] = '.';
vis[x][y]=0;
Q.push(x);
Q.push(y);
while(! Q.empty()){
int tx = Q.front();Q.pop();
int ty = Q.front(); Q.pop();
for(int i=0; i<=3; i++){
int a = tx+dirx[i];
int b = ty+diry[i];
if(a==f && b==g){
return (vis[tx][ty]+1);
}
if((a>=0 && a<n && b>=0 && b<n) && (grid[a][b] =='.' && vis[a][b] == 0)){
vis[a][b] = vis[tx][ty]+1;
Q.push(a);
Q.push(b);
}
}
}
return -1;
}
int main()
{
int tc, m ,n;
scanf("%d",&tc);
for(int t=1; t<=tc; t++){
scanf("%d",&n);
int ax,ay,bx,by,cx,cy;
for(int i=0; i<n; i++){
scanf("%s",grid[i]);
for(int j=0; j<n; j++){
if(isalpha(grid[i][j])){
pv.push_back(pos(grid[i][j],i,j));
}
}
}
sort(pv.begin(), pv.end());
int ans = 0;int sz = pv.size();
for(int i=0; i<sz-1; i++){
memset(vis,0,sizeof vis);
int tem = bfs(pv[i].x,pv[i].y, pv[i+1].x, pv[i+1].y,n);
if(tem==-1){
ans = -1;
break;
}
ans+=tem;
}
if(ans != -1)
printf("Case %d: %d\n",t,ans);
else
printf("Case %d: Impossible\n",t);
pv.clear();
}
return 0;
}