Property Distribution
题目看不懂,不过看输入输出结果猜出来了,查看所以块得到数量,DFS搜索下就可以了。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 char a[110][110]; 6 int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}, h, w, ans; 7 bool vis[110][110]; 8 9 void dfs(int x, int y, char c){ 10 vis[x][y] = true; 11 for(int i = 0; i < 4; i ++){ 12 int xx = x+dx[i], yy = y + dy[i]; 13 if(0 <= xx && xx < h && 0 <= yy && yy < w && a[xx][yy] == c && !vis[xx][yy]){ 14 dfs(xx,yy,c); 15 } 16 } 17 } 18 19 int main(){ 20 while(cin>>h>>w){ 21 if(h==0&w==0) break; 22 memset(a,0,sizeof(a)); 23 memset(vis,false,sizeof(vis)); 24 ans = 0; 25 // for(int i = 0; i < h; i ++) gets(a[i]); 26 for(int i = 0; i < h; i ++) 27 for(int j = 0; j < w; j ++) 28 cin>>a[i][j]; 29 for(int i = 0; i < h; i ++){ 30 for(int j = 0; j < w; j ++){ 31 if(!vis[i][j]){ 32 ans++; 33 dfs(i,j,a[i][j]); 34 } 35 } 36 } 37 cout << ans << endl; 38 } 39 return 0; 40 }