标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17773 Accepted Submission(s):
10826
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <stack> 6 #include <queue> 7 using namespace std; 8 int w,h; 9 int a[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; //位置数组 10 char str[22][22]; 11 int f[22][22]; //标记该位置是否走过 12 int sum; 13 void dfs(int x,int y) 14 { 15 f[x][y] = 1; 16 for (int i = 0; i < 4; i ++) 17 { 18 int x1=x + a[i][0]; 19 int y1=y + a[i][1]; 20 if (x1 >= 0 && x1 < h && y1 >= 0 && y1 < w && str[x1][y1]!=‘#‘ && f[x1][y1] == 0) 21 { //判断边界、是否是不能经过的红地板、该地板是否已经经过 22 sum ++; 23 dfs(x1,y1); 24 } 25 } 26 } 27 int main () 28 { 29 int i,j,x,y; 30 while (scanf("%d%d",&w,&h),w&&h) 31 { 32 for (i = 0; i < h; i ++) 33 scanf("%s",str[i]); 34 35 memset(f,0,sizeof(f)); 36 for (i = 0; i < h; i ++) 37 for (j = 0; j < w; j ++) 38 if (str[i][j] == ‘@‘) //找出人的位置 39 { 40 x = i; 41 y = j; 42 break; 43 } 44 sum = 1; 45 dfs(x,y); 46 printf("%d\n",sum); 47 } 48 return 0; 49 }
标签:
原文地址:http://www.cnblogs.com/yoke/p/5929557.html