Red and Black 搜索

转载自http://blog.csdn.net/zone_programming/article/details/6688424  DFS

转载自http://blog.163.com/cristiano_faith/blog/static/1725054812011116103645188/ BFS

Red and Black

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 11

Font: Times New Roman | Verdana | Georgia

Font Size:

Problem Description

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)

Output

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

Source

Asia 2004, Ehime (Japan), Japan Domestic

搜索真心不会:来自转载:
   
   
   
   
[cpp] view plain copy print ?
  1. #include<iostream>  
  2. #include<string.h>  
  3. #include<stdio.h>  
  4. using namespace std;  
  5. int direct[4][2] = { -1, 0, 1, 0, 0, 1, 0, -1 };  /*定义方向, 左右,上下*/  
  6.   
  7. char map[21][21];               /*输入的字符串*/  
  8. bool mark[21][21];              /*标记走过的路程*/  
  9. bool flag;  
  10. int W, H;  
  11. int Dx, Dy;            //记录起始位置@,从这里开始进行搜索  
  12. int ans;                //记录满足的个数。初始化为1,因为@也包含在内  
  13. /*****底下是核心算法,主要是从 
  14. 上下左右这四个方向进行搜索,注意 
  15. 满足搜索的条件是不能越界,不能是#, 
  16. 还有就是没有搜索过的--》主要是靠mark[i][j] 
  17. 来实现*******/  
  18. void DFS( int x, int y )  
  19. {  
  20.     mark[x][y] = true;  
  21.     forint k = 0; k < 4; k ++ )  
  22.     {  
  23.         int p = x + direct[k][0];  
  24.         int q = y + direct[k][1];  
  25.         if( p >= 0 && q >= 0 && p < H && q < W && !mark[p][q] && map[p][q] != '#' )  
  26.         {  
  27.             ans ++;  
  28.             DFS( p, q );  
  29.         }  
  30.     }  
  31.     return;  
  32. }  
  33.   
  34. int main()  
  35. {  
  36.     int i, j, k;  
  37.     while( cin >> W >> H && ( W || H ) )   // W -> column, H -> row;  
  38.     {  
  39.         memset( mark, falsesizeof( mark ) );  
  40.         for( i = 0; i < H; i ++ )  
  41.             for( j = 0; j < W; j ++ )  
  42.             {  
  43.                 cin >> map[i][j];  
  44.                 if( map[i][j] == '@' )  
  45.                 {  
  46.                     Dx = i;  
  47.                     Dy = j;  
  48.                 }  
  49.             }  
  50.         ans = 1;  
  51.         DFS( Dx, Dy );  
  52.         cout << ans << endl;  
  53.     }  



题目大意:一个矩形空间,上有黑色同红色两种砖,求从某一黑砖出发可以经过的黑砖的数目

 原先用DFS做的,但调试很久都不知哪里出错,后来改用BFS  

#include<stdio.h>

#include<queue>

using namespace std;

int n,m,total;

int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};

char map[20][20];

typedef struct

{

    int x,y;

}point;

point start;

int bfs(point start)

{

    queue<point>q;

    int i;

    point cur,next;

    map[start.x][start.y]='#';

    q.push(start);

    while(!q.empty())

    {

       cur=q.front();

       q.pop();

       for(i=0;i<4;i++)

       {

           next.x=cur.x+dir[i][0];

           next.y=cur.y+dir[i][1];

           if(next.x>=0&&next.x<m&&next.y>=0&&next.y<n)

              if(map[next.x][next.y]!='#')

              {

                  map[next.x][next.y]='#';

                  total++;

                  q.push(next);

              }

 

       }

    }

    return total;

   

}

int main()

{

    int i,j;

    while(scanf("%d%d",&n,&m)!=EOF&&n!=0)

    {  

       getchar();

       for(i=0;i<m;i++)

       {

           for(j=0;j<n;j++)

           {  

              scanf("%c",&map[i][j]);

              if(map[i][j]=='@')

              {start.x=i;start.y=j;}

           }

           getchar();

       }

       total=1;

       printf("%d\n",bfs(start));

    }

    return 0;

}



你可能感兴趣的:(Red and Black 搜索)