HDU 1312 Red and Black(基础bfs或者dfs)

Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11843    Accepted Submission(s): 7380


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
 

题目大意:

  给你一个h*w的棋盘。让你在这个棋盘中找出由@点开始走的‘.’ 有多少个,并且每次只能走相邻的四个方向。

解题思路:

  这题bfs和dfs都能过的,为的就是加强dfs和bfs的思想,最近做的比赛的时候,感觉自己对于基础算法的理解还不是很到位,所以,加强这方面的练习,从最为基础的

题目来加深理解。bfs的时候,我们要把node压入队列,并且在每次can_move判断后就要对其进行新的入队操作。然后,bfs的解题思路就是说从当前的@点开始,一步一步的走完所有@周围的点,每次我们的扩展按照从“从小到大”的原则来进行,也就是说,我们第一次扩展是把距离@为1的点加入队列,把这个状态结束后,我们就把距离@为2的点加入队列,当这个状态结束后,我们再把距离@为3的点加入队列,,,一直到我们扩展完所有的状态最终使得我们的队列为空,这个时候,我们的解就找到了。

  而用dfs来做的时候,就相当于一条路走到黑的原则,每找到一个'.'点后,就用'#'来替换他,直到跑完整个地图的所有位置,输出'#'的位置就可以了, 实际上就是can_move()函数的编写了.

bfs代码:

 1 # include<cstdio>
 2 # include<iostream>
 3 # include<queue>
 4 # include<cstring>
 5 
 6 using namespace std;
 7 
 8 # define MAX 23
 9 
10 char grid[MAX][MAX];
11 int book[MAX][MAX];
12 int w,h;
13 
14 int nxt[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};
15 
16 struct node
17 {
18     int x,y;
19     char val;
20 };
21 
22 int can_move ( int x,int y )
23 {
24     if ( x>=0&&x<h&&y>=0&&y<w&&book[x][y]==0&&grid[x][y]=='.' )
25         return 1;
26     else
27         return 0;
28 }
29 
30 int bfs ( node start )
31 {
32     int cnt = 0;
33     queue<node>Q;
34     Q.push(start);
35     start.val = '#';
36     while ( !Q.empty() )
37     {
38         node now = Q.front();
39         Q.pop();
40         for ( int i = 0;i < 4;i++ )
41         {
42             int tx = now.x+nxt[i][0], ty = now.y+nxt[i][1];
43             if ( can_move(tx,ty) )
44             {
45                 book[tx][ty] = 1;
46                 node newnode;
47                 newnode.x = tx, newnode.y = ty, newnode.val = '#';
48                 cnt++;
49                 Q.push(newnode);
50             }
51         }
52     }
53     return cnt;
54 }
55 
56 
57 int main(void)
58 {
59     while ( scanf("%d%d",&w,&h)!=EOF )
60     {
61         if ( w==0&&h==0 )
62             break;
63         memset(grid,0,sizeof(grid));
64         for ( int i = 0;i < h;i++ )
65         {
66             scanf("%s",grid[i]);
67         }
68         int stx,sty;
69         for ( int i = 0;i < h;i++ )
70         {
71             for ( int j = 0;j < w;j++ )
72             {
73                 if ( grid[i][j]=='@' )
74                 {
75                     stx = i;
76                     sty = j;
77                 }
78             }
79         }
80         book[stx][sty] = 1;
81         node start;
82         start.x = stx, start.y = sty, start.val = '@';
83         int ans = bfs(start);
84         printf("%d
",ans+1);
85         memset(book,0,sizeof(book));
86     }
87 
88 
89     return 0;
90 }

dfs代码:

 1 # include<cstdio>
 2 # include<iostream>
 3 # include<cstring>
 4 
 5 using namespace std;
 6 
 7 # define MAX 23
 8 
 9 char grid[MAX][MAX];
10 int book[MAX][MAX];
11 int nxt[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};
12 int h,w;
13 int cnt;
14 
15 int can_move ( int x,int y )
16 {
17     if( x>=0&&x<h&&y>=0&&y<w&&book[x][y]==0&&grid[x][y]=='.' )
18         return 1;
19     else
20         return 0;
21 }
22 
23 void dfs ( int x,int y )
24 {
25     book[x][y] = 1;
26     for ( int i = 0;i < 4;i++ )
27     {
28         int tx = x+nxt[i][0], ty = y+nxt[i][1];
29         if ( can_move(tx,ty) )
30         {
31             cnt++;
32             book[tx][ty] = 1;
33             dfs(tx,ty);
34         }
35     }
36     return;
37 }
38 
39 int main(void)
40 {
41     while ( scanf("%d%d",&w,&h)!=EOF )
42     {
43         if ( h==0&&w==0 )
44             break;
45         cnt = 0;
46         memset(grid,0,sizeof(grid));
47         for ( int i = 0;i < h;i++ )
48         {
49             scanf("%s",grid[i]);
50         }
51         int stx,sty;
52         for ( int i = 0;i < h;i++ )
53         {
54             for ( int j = 0;j < w;j++ )
55             {
56                 if ( grid[i][j]=='@' )
57                 {
58                     stx = i;
59                     sty = j;
60                 }
61             }
62         }
63         dfs(stx,sty);
64         printf("%d
",cnt+1);
65         memset(book,0,sizeof(book));
66     }
67 
68 
69     return 0;
70 }
原文地址:https://www.cnblogs.com/wikioibai/p/4485793.html