poj-1979 && hdoj - 1312 Red and Black (简单dfs)
http://poj.org/problem?id=1979
基础搜索。
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\n", 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 25 38 using namespace std; 39 40 char filed[25][25]; 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) 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]=='.') 51 { 52 sum++; 53 filed[xx][yy]='#'; 54 dfs(xx,yy); 55 } 56 } 57 } 58 int main() 59 { 60 //freopen("a.txt","r",stdin); 61 while(~scanf("%d%d",&w,&h)&&w+h) 62 { 63 getchar(); 64 for(int i=0;i<h;i++) 65 scanf("%s",filed[i]); 66 int a,b; 67 for(int i=0;i<h;i++) 68 for(int j=0;j<w;j++) 69 if(filed[i][j]=='@') 70 { 71 a=i; 72 b=j; 73 } 74 //printf("%d %d\n",a,b); 75 sum=1; 76 filed[a][b]='#'; 77 dfs(a,b); 78 printf("%d\n",sum); 79 } 80 return 0; 81 }