您的位置:首页 > 其它

bzoj 1667: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛(BFS)

2017-09-18 22:39 549 查看

1667: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛

Time Limit: 1 Sec  Memory Limit: 64 MBSec  Special Judge
Submit: 660  Solved: 169

[Submit][Status][Discuss]

Description

经过跟Farmer John长达数年的谈判,奶牛们终于如愿以偿地得到了想要的旱冰鞋。农场上大部分的区域都很平整,适合在上面滑动,但有一些小块的土地上有很多的岩石,凭奶牛们的旱冰技术,是没有办法通过的。 农场可以看成一个被划分成R(1<=R<=113)行C(1<=C<=77)列的矩阵。快要开饭了,贝茜发现自己在坐标为(1,1)的格子里,并且她想赶到坐标为(R,C)的牛棚去享用她的晚饭。贝茜知道,以她所在的格子为起点,每次向上、下、左、右滑动(但不能沿对角线滑动),一定能找到一条通往牛棚的、不经过任何有大量岩石的格子的路。请你为她指出任意一条通往牛棚的路径。

Input

* 第1行: 两个用空格隔开的整数,R和C * 第2..R+1行: 每行包含C个字符(不含空格),字符只可能是'.'或'*'。是'.' 的话,表示贝茜能从这个格子里通过,是'*'的话,则这个格子 是不能通过的多岩石地带

Output

* 第1..?行: 每行包含2个用空格隔开的整数,表示贝茜回牛棚路径所通过的格 子的坐标。输出的第一行显然应该是1 1,最后一行是R C。输出中 的其余行,依次给出路径中格子的坐标,相邻的两个坐标所表示的 格子必须相邻。

Sample Input

5 8

..*...**

*.*.*.**

*...*...

*.*.*.*.

....*.*.

Sample Output

1 1

1 2

2 2

3 2

3 3

3 4

2 4

1 4

1 5

1 6

2 6

3 6

3 7

3 8

4 8

5 8

记录路径的BFS,模板

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
typedef struct
{
int x;
int y;
}Point;
Point fa[120][80], now, temp;
queue<Point> q;
char str[120][80];
int bet[120][80], dir[4][2] = {1,0,0,1,-1,0,0,-1};
void Print(int x, int y)
{
if(fa[x][y].x!=0)
Print(fa[x][y].x, fa[x][y].y);
printf("%d %d\n", x, y);
}
int main(void)
{
int n, m, i, j;
scanf("%d%d", &n, &m);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
scanf(" %c", &str[i][j]);
}
memset(bet, 62, sizeof(bet));
bet[1][1] = 0;
now.x = now.y = 1;
q.push(now);
while(q.empty()==0)
{
now = q.front();
q.pop();
for(i=0;i<=3;i++)
{
temp.x = now.x+dir[i][0];
temp.y = now.y+dir[i][1];
if(str[temp.x][temp.y]=='.' && bet[now.x][now.y]+1<bet[temp.x][temp.y])
{
bet[temp.x][temp.y] = bet[now.x][now.y]+1;
q.push(temp);
fa[temp.x][temp.y].x = now.x;
fa[temp.x][temp.y].y = now.y;
}
}
}
Print(n, m);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: