Https://vjudge.net/contest/65959#problem/I Topic
a piece of ground with grass and lots, two people want to burn the grass, so that they can be happy Ooxx, they both have to set fire on a piece of ground (each can only be put once), can be in the same or different positions, the fire can spread around, spread a phone call for 1 minutes, Ask them the minimum time they spent burning the grass. ideas for solving problems
The idea is that I see a great God, feel good, that is, the two people can set fire to the location to try again, compared to the shortest time, the specific use of a structural array will all have the position of the grass recorded, and then it 22 for all the combination of BFS. Find out what time they spend. Code
# include<stdio.h> # include <queue> # include <string.h> using namespace std; # define max struct Node {int x, y, step;}
V[105];
Char Num[max][max];
int dir[4][2] = {{1,0},{-1,0}, {0,1}, {0,-1}};//four orientations int T, m, n; int Array[max][max];
int PAN () {int i, J; for (i = 0; i<n; i++) for (j = 0; j<m; j +) {if (num[i][j] = = ' # ' && array[i][j] = = 0) It's useless to judge
The grass burnt out, array[i][j] = 0 o'clock stands for the grass did not burn {return 0;
} return 1;
int BFS (node s, node Q) {queue<node>q;
Q.push (s);
Q.push (q);
ARRAY[S.X][S.Y] = array[q.x][q.y] = 1;//Mark start position has been burned while (Q.size ()) {s = Q.front ();
Q.pop ();
for (int i = 0;i<4; i++) {q = s;
Q.x +=dir[i][0];
Q.y +=dir[i][1]; if (q.x>=0 && q.x<m && q.y >=0 && q.y<n && array[q.x][q.y] = 0 && num[
Q.X][Q.Y] = = ' # ') {Q.step +=1;
ARRAY[Q.X][Q.Y] = 1;
Q.push (q);
}} return s.step;
int main (void) {int i,j, k,t;
scanf ("%d", &t);
for (i = 1; i<=t; i++) {T = 0;
int ans = 99999;
scanf ("%d%d", &n,&m);
for (j = 0; j<n; j + +) {scanf ("%s", Num[j]); for (k = 0; k<m; k++) {if (num[j][k] = = ' # ') {v[t].x =
J
V[t].step = 0;
V[t++].y = k; The time {for (k = j; k<t; + +) is calculated for (j = 0; j<t; j +)//for any two locations with the least amount of phone calls.
+) {memset (array, 0, sizeof (array));
int L = BFS (V[j], v[k]);
if (L<ans && PAN ()) {ans = l; '} ' if (ans = = 99999) {printf ("Case%d: -1\n", i);
else{printf ("Case%d:%d\n", I, ans);
} return 0; }