Problem solving: It's amazing that BFS can do this kind of thing ............. Outside the world ........
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #include <queue>10 #define LL long long11 using namespace std;12 int rows,cols,mp[1000][1500];13 queue<int>q;14 void bfs(int x,int y) {15 const int dir[4][2] = {0,-1,0,1,-1,0,1,0};16 int i,j,tx,ty,u,v;17 q.push(x);18 q.push(y);19 while(!q.empty()) {20 tx = q.front();21 q.pop();22 ty = q.front();23 q.pop();24 for(i = 0; i < 4; i++) {25 u = tx+dir[i][0];26 v = ty+dir[i][1];27 if(!mp[u][v]) continue;28 if(u < 0 || v < 0 || u >= 999 || v >= 1499) continue;29 mp[u][v] = 0;30 q.push(u);31 q.push(v);32 }33 }34 }35 int main() {36 int ks,i,j;37 scanf("%d",&ks);38 while(ks--) {39 scanf("%d %d",&cols,&rows);40 for(i = 0; i < 1000; i++)41 for(j = 0; j < 1500; j++)42 mp[i][j] = 1;43 for(i = 1; i <= rows; i++) {44 for(j = 1; j <= cols; j++)45 scanf("%d",mp[i]+j);46 }47 while(!q.empty()) q.pop();48 bfs(0,0);49 for(i = 1; i <= rows; i++) {50 for(j = 1; j <= cols-1; j++)51 printf("%d ",mp[i][j]);52 printf("%d\n",mp[i][j]);53 }54 }55 return 0;56 }View code
Why do we need to add a circle of 1? Hey ..... Because the search starts from the upper left corner. If it is all 0, it may not be on the right .. Then the search will fail, resulting in wa ..... The ring is added to ensure that the outside of the Zero ring is a continuous one.