递归回溯法poj1979Red and Black

出自http://poj.org/problem?id=1979

    题意:简单递归回溯法

     

 1 #include <iostream>
 2 using namespace std;
 3 #define W 20
 4 #define H 20
 5 char block[W+1][H+1];
 6 int w,h;
 7 int f(int x,int y)
 8 {
 9  if(x<0 || x>=h || y<0 || y>=w)
10            return 0;
11  if(block[x][y] == '#')
12         return 0;
13        block[x][y]='#';
14  return 1+f(x-1,y)+f(x,y-1)+f(x+1,y)+f(x,y+1);
15 }
16 int main()
17 {
18  int i=0,j=0;
19  while(cin>>w>>h && w!=0 && h!=0)
20  {
21   for(i=0;i<h;i++)
22    for(j=0;j<w;j++)
23     cin>>block[i][j];
24   for(i=0;i<h;i++)
25    for(j=0;j<w;j++)
26     if(block[i][j] == '@')
27     {
28      cout << f(i,j) <<endl;
29         break;
30     }
31  }
32  return 0;
33 }
View Code

 

posted @ 2014-08-05 00:48  tt_tt--->  阅读(101)  评论(0编辑  收藏  举报