标签:put 图像 otto logs 测试数据 mission while else tput
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 874 Accepted Submission(s): 299
1 //2017-08-12 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <queue> 7 8 using namespace std; 9 10 const int N = 210; 11 char G[N][N]; 12 int n, m; 13 int dx[4] = {0, 1, 0, -1}; 14 int dy[4] = {1, 0, -1, 0}; 15 bool vis[N][N]; 16 struct Node{ 17 int x, y; 18 Node(int _x, int _y):x(_x), y(_y){} 19 }; 20 21 void bfs(int x, int y, int op){ 22 Node node(x, y); 23 memset(vis, 0, sizeof(vis)); 24 vis[x][y] = op+1; 25 G[x][y] = ‘#‘; 26 queue<Node> q; 27 q.push(node); 28 while(!q.empty()){ 29 Node a = q.front(); 30 q.pop(); 31 for(int i = 0; i < 4; i++){ 32 int nx = a.x + dx[i]; 33 int ny = a.y + dy[i]; 34 if(nx>=0 && nx < n && ny>=0 && ny < m && !vis[nx][ny] && G[nx][ny] == ‘0‘+op){ 35 Node tmp(nx, ny); 36 q.push(tmp); 37 vis[nx][ny] = op+1; 38 G[nx][ny] = ‘#‘; 39 } 40 } 41 } 42 } 43 44 int main() 45 { 46 //freopen("data1006.txt", "r", stdin); 47 while(scanf("%d%d", &n, &m)!=EOF){ 48 for(int i = 0; i < n; i++){ 49 scanf("%s", G[i]); 50 } 51 for(int i = 0; i < m; i++){ 52 if(G[0][i] == ‘0‘)bfs(0, i, 0); 53 if(G[n-1][i] == ‘0‘)bfs(n-1, i, 0); 54 } 55 for(int i = 0; i < n; i++){ 56 if(G[i][0] == ‘0‘)bfs(i, 0, 0); 57 if(G[i][m-1] == ‘0‘)bfs(i, m-1, 0); 58 } 59 int cnt1 = 0; 60 for(int i = 0; i < n; i++){ 61 for(int j = 0; j < m; j++){ 62 if(G[i][j] == ‘1‘){ 63 bfs(i, j, 1); 64 cnt1++; 65 } 66 } 67 } 68 int cnt2 = 0; 69 for(int i = 0; i < n; i++){ 70 for(int j = 0; j < m; j++){ 71 if(G[i][j] == ‘0‘){ 72 bfs(i, j, 0); 73 cnt2++; 74 } 75 } 76 } 77 if(cnt1==1 && cnt2 == 0)printf("1\n"); 78 else if(cnt1 == 1 && cnt2 == 1)printf("0\n"); 79 else printf("-1\n"); 80 } 81 82 return 0; 83 }
标签:put 图像 otto logs 测试数据 mission while else tput
原文地址:http://www.cnblogs.com/Penn000/p/7351756.html