您的位置:首页 > 其它

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

2015-05-07 19:10 459 查看

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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)

[align=left]Output[/align]
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).

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

45
59
6
13

[align=left]Source[/align]
Asia 2004, Ehime (Japan), Japan Domestic

题目大意:

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

解题思路:

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

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

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

bfs代码:

# include<cstdio>
# include<iostream>
# include<queue>
# include<cstring>

using namespace std;

# define MAX 23

char grid[MAX][MAX];
int book[MAX][MAX];
int w,h;

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

struct node
{
int x,y;
char val;
};

int can_move ( int x,int y )
{
if ( x>=0&&x<h&&y>=0&&y<w&&book[x][y]==0&&grid[x][y]=='.' )
return 1;
else
return 0;
}

int bfs ( node start )
{
int cnt = 0;
queue<node>Q;
Q.push(start);
start.val = '#';
while ( !Q.empty() )
{
node now = Q.front();
Q.pop();
for ( int i = 0;i < 4;i++ )
{
int tx = now.x+nxt[i][0], ty = now.y+nxt[i][1];
if ( can_move(tx,ty) )
{
book[tx][ty] = 1;
node newnode;
newnode.x = tx, newnode.y = ty, newnode.val = '#';
cnt++;
Q.push(newnode);
}
}
}
return cnt;
}

int main(void)
{
while ( scanf("%d%d",&w,&h)!=EOF )
{
if ( w==0&&h==0 )
break;
memset(grid,0,sizeof(grid));
for ( int i = 0;i < h;i++ )
{
scanf("%s",grid[i]);
}
int stx,sty;
for ( int i = 0;i < h;i++ )
{
for ( int j = 0;j < w;j++ )
{
if ( grid[i][j]=='@' )
{
stx = i;
sty = j;
}
}
}
book[stx][sty] = 1;
node start;
start.x = stx, start.y = sty, start.val = '@';
int ans = bfs(start);
printf("%d\n",ans+1);
memset(book,0,sizeof(book));
}

return 0;
}


dfs代码:

# include<cstdio>
# include<iostream>
# include<cstring>

using namespace std;

# define MAX 23

char grid[MAX][MAX];
int book[MAX][MAX];
int nxt[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};
int h,w;
int cnt;

int can_move ( int x,int y )
{
if( x>=0&&x<h&&y>=0&&y<w&&book[x][y]==0&&grid[x][y]=='.' )
return 1;
else
return 0;
}

void dfs ( int x,int y )
{
book[x][y] = 1;
for ( int i = 0;i < 4;i++ )
{
int tx = x+nxt[i][0], ty = y+nxt[i][1];
if ( can_move(tx,ty) )
{
cnt++;
book[tx][ty] = 1;
dfs(tx,ty);
}
}
return;
}

int main(void)
{
while ( scanf("%d%d",&w,&h)!=EOF )
{
if ( h==0&&w==0 )
break;
cnt = 0;
memset(grid,0,sizeof(grid));
for ( int i = 0;i < h;i++ )
{
scanf("%s",grid[i]);
}
int stx,sty;
for ( int i = 0;i < h;i++ )
{
for ( int j = 0;j < w;j++ )
{
if ( grid[i][j]=='@' )
{
stx = i;
sty = j;
}
}
}
dfs(stx,sty);
printf("%d\n",cnt+1);
memset(book,0,sizeof(book));
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: