您的位置:首页 > 其它

HDU1312 Red and Black(BFS)(step与sum)

2018-02-07 17:20 399 查看
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. 

InputThe 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,
4000
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) 

OutputFor 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


题意:给出一个m行n列的迷宫(注意不是n行m列),'.'表示能走的格子,'#'表示墙,从起点'@'开始,问你在这个迷宫中最多能走多少个格子。

解题思路:好多天没有做题手好生啊…看什么题都没有思路所以这才掘个坟把以前刚学习bfs的第一道题重新拿出来做一下…

bfs的搜索方式可以类比病毒在机体内的感染过程=。=

从母体开始不断的向四周感染,直到这个机体内的所有细胞都已经被感染完毕。

那么在“不断地向四周感染”的过程中,我们又可以放大来看,感染一周的细胞需要个顺序,也就是先感染上面的细胞然后是右、下、左。这一轮感染完毕。

下一轮感染最开始的细胞也就是上一轮最开始被感染的那个细胞,也就是上一轮中的“上面的细胞”。

Mark以做提醒:

结构体中的step是伴随每个状态记录从起点到当前点的距离。

而本题中要求的并不是最短距离而是能够到达的所有格子的个数,所以我们需要另外设置一个计数变量sum,当每能走出一步时,sum即进行累加。

也就是说这道题我们的bfs并不是解决最短路径的问题,而是借助bfs的方式来统计这个图中所有符合条件的格子。

如果以上能够理解,那么本题为什么用bfs而不是dfs呢?0.0

AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

char mp[25][25];
int n,m,book[25][25];
int nx[4][2]={0,1,-1,0,0,-1,1,-0};

struct node
{
int x,y;
};

node getnode(int x,int y)
{
node q;
q.x=x;
q.y=y;
return q;
}

int bfs(int x,int y)
{
int sum=1;
queue<node> q;
q.push(getnode(x,y));
while(!q.empty())
{
for(int i=0;i<4;i++)
{
int tx=q.front().x+nx[i][0];//优先队列为q.top()
int ty=q.front().y+nx[i][1];
if(tx>=0&&tx<n&&ty>=0&&ty<m&&book[tx][ty]==0&&mp[tx][ty]=='.')
{
book[tx][ty]=1;
sum++;
q.push(getnode(tx,ty));
}
}
q.pop();
}
return sum;
}

int main()
{
while(~scanf("%d%d",&m,&n)&&n+m)
{
memset(book,0,sizeof(book));
for(int i=0;i<n;i++)
scanf("%s",mp[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mp[i][j]=='@')
{
book[i][j]=1;
printf("%d\n",bfs(i,j));
}
}
}
}
return 0;
}

针对循环优化一下:一找到起点即结束循环。

int flag=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mp[i][j]=='@')
{
book[i][j]=1;
printf("%d\n",bfs(i,j));
flag=1;
break;
}
}
if(flag) break;
}


优化后的运行时间是0ms0.0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: