标签:
题意:一张图,判断5个等级的个数;如果连通块中有m种字母出现,就是m等级(1 <= m <= 5).
思路:dfs连通块类似,注意vis数组放到全局变量里,dfs会返回,因此用到数组的时候需要开全局变量,容易出错。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #define repu(i,a,b) for(int i=a;i<b;i++) 6 #include <queue> 7 using namespace std; 8 int dx[4]= {1,0,-1,0}; 9 int dy[4]= {0,1,0,-1}; 10 int w,h; 11 char g[1200][1220]; 12 int vis[7]; 13 struct point 14 { 15 int x,y; 16 }; 17 void dfs(const point &t) 18 { 19 int x,y,a,b,i; 20 x=t.x; 21 y=t.y; 22 point tmp; 23 if (g[x][y]==‘.‘) return ; 24 else if(g[x][y] == ‘B‘) vis[0] = 1;///开全局担心有点浪费空间 25 else if(g[x][y] == ‘G‘) vis[1] = 1; 26 else if(g[x][y] == ‘W‘) vis[2] = 1; 27 else if(g[x][y] == ‘R‘) vis[3] = 1; 28 else if(g[x][y] == ‘Y‘) vis[4] = 1; 29 g[x][y]=‘.‘; 30 repu(i,0,4) 31 { 32 a = x + dx[i]; 33 b = y + dy[i]; 34 if (a < 0||b < 0||a >= h||b >= w) 35 continue; 36 tmp.x = a; 37 tmp.y = b; 38 dfs(tmp); 39 } 40 return; 41 } 42 43 int main() 44 { 45 int t[7]; 46 //freopen("1.txt","r",stdin); 47 int kase; 48 cin>>kase; 49 while(kase--) 50 { 51 cin>>h>>w; 52 point tmp; 53 memset(t,0,sizeof(t)); 54 repu(i,0,h) 55 cin>>g[i]; 56 repu(i,0,h) 57 repu(j,0,w) 58 if(g[i][j]!=‘.‘) 59 { 60 tmp.x=i; 61 tmp.y=j; 62 dfs(tmp); 63 int v = 0; 64 repu(i,0,6)///而且还有循环,浪费时间 65 if(vis[i]) 66 v++; 67 t[v]++; 68 memset(vis,0,sizeof(vis)); 69 } 70 repu(i,1,5) 71 cout<<t[i]<<" "; 72 cout<<t[5]<<endl; 73 } 74 return 0; 75 }
标签:
原文地址:http://www.cnblogs.com/ACMERY/p/4546993.html