AOJ 0118 Property Distribution (DFS)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46522

简单DFS,题目翻译参考  http://blog.csdn.net/synapse7/article/details/14453017

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16 
17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
18 
19 #define ll long long
20 #define inf 0x7f7f7f7f
21 #define lc l,m,rt<<1
22 #define rc m + 1,r,rt<<1|1
23 #define pi acos(-1.0)
24 
25 #define L(x)    (x) << 1
26 #define R(x)    (x) << 1 | 1
27 #define MID(l, r)   (l + r) >> 1
28 #define Min(x, y)   (x) < (y) ? (x) : (y)
29 #define Max(x, y)   (x) < (y) ? (y) : (x)
30 #define E(x)        (1 << (x))
31 #define iabs(x)     (x) < 0 ? -(x) : (x)
32 #define OUT(x)  printf("%I64d
", x)
33 #define lowbit(x)   (x)&(-x)
34 #define Read()  freopen("a.txt", "r", stdin)
35 #define Write() freopen("dout.txt", "w", stdout);
36 #define maxn 1000000000
37 #define N 110
38 using namespace std;
39 
40 char filed[N][N];
41 int w,h,sum;
42 int dir[4][2]={-1,0,1,0,0,1,0,-1};
43 
44 void dfs(int x,int y,char c)
45 {
46     for(int i=0;i<4;i++)
47     {
48         int xx=x+dir[i][0];
49         int yy=y+dir[i][1];
50         if(xx>=0&&xx<h&&yy>=0&&yy<w&&filed[xx][yy]==c)
51         {
52             filed[xx][yy]='.';
53             dfs(xx,yy,c);
54         }
55     }
56 }
57 int main()
58 {
59     //freopen("a.txt","r",stdin);
60     while(~scanf("%d%d",&h,&w)&&w+h)
61     {
62         getchar();
63         for(int i=0;i<h;i++)
64             scanf("%s",filed[i]);
65         sum=0;
66         for(int i=0;i<h;i++)
67             for(int j=0;j<w;j++)
68             if(filed[i][j]!='.')
69             {
70                 dfs(i,j,filed[i][j]);
71                 filed[i][j]='.';
72                 sum++;
73             }
74         //printf("%d %d
",a,b);
75         printf("%d
",sum);
76     }
77    return 0;
78 }
View Code
原文地址:https://www.cnblogs.com/nowandforever/p/4372601.html