http://acm.hdu.edu.cn/showproblem.php?pid=1547
dfs
我的代码
1 #include <stdio.h>
2 #include <string.h>
3 const int N=210;
4 const int dx[6]={0,1,1,0,-1,-1};
5 const int dy[6]={2,1,-1,-2,-1,1};
6 char maze[N][N];
7 int m,n,ans;
8 void dfs1(int x,int y,char c)
9 {
10 maze[x][y]=0; ans++;
11 int d,nx,ny;
12 for (d=0;d<6;d++)
13 {
14 if (y==1 && d==3) continue;
15 nx=x+dx[d]; ny=y+dy[d];
16 if (maze[nx][ny]==c) dfs1(nx,ny,c);
17 }
18
19 }
20 void dfs2(int x,int y)
21 {
22 maze[x][y]=0;
23 int d,nx,ny;
24 for (d=0;d<6;d++)
25 {
26 if (y==1 && d==3) continue;
27 nx=x+dx[d]; ny=y+dy[d];
28 if (maze[nx][ny]) dfs2(nx,ny);
29 }
30
31 }
32 int main()
33 {
34 int x,y,i,j;
35 while (~scanf("%d%d%d%d",&n,&m,&x,&y))
36 {
37 ans=0; m=m*2-1;
38 memset(maze,0,sizeof(maze));
39 for (i=1;i<=n;i++)
40 {
41 char s[N];
42 scanf("%s",s);
43 if (i%2) for (j=0;s[j];j++) maze[i][j*2+1]=s[j];
44 else for (j=0;s[j];j++) maze[i][j*2+2]=s[j];
45 for (j=1;j<=m;j++) if (maze[i][j]=='E') maze[i][j]=0;
46 }
47 if (x%2) y=y*2-1; else y=y*2;
48 dfs1(x,y,maze[x][y]);
49 if (ans<3) ans=0;
50 else
51 {
52 for (i=1;i<=m;i++)
53 if (maze[1][i]) dfs2(1,i);
54 for (i=1;i<=n;i++)
55 for (j=1;j<=m;j++)
56 if (maze[i][j]) ans++;
57 }
58 printf("%d\n",ans);
59 }
60 return 0;
61 }