您的位置:首页 > 其它

bzoj 3393 && bzoj 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课(BFS)

2017-09-12 17:02 513 查看

1644: [Usaco2007 Oct]Obstacle Course 障碍训练课

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 680  Solved: 326
[Submit][Status][Discuss]

Description

考虑一个 N x N (1 <= N <= 100)的有1个个方格组成的正方形牧场。有些方格是奶牛们不能踏上的,它们被标记为了'x'。例如下图:. . B x .
. x x A .
. . . x .
. x . . .
. . x . .贝茜发现自己恰好在点A处,她想去B处的盐块舔盐。缓慢而且笨拙的动物,比如奶牛,十分讨厌转弯。尽管如此,当然在必要的时候她们还是会转弯的。对于一个给定的牧场,请你计算从A到B最少的转弯次数。开始的时候,贝茜可以使面对任意一个方向。贝茜知道她一定可以到达。

Input

第 1行: 一个整数 N 行2..N + 1: 行 i+1 有 N 个字符 ('.', 'x', 'A', 'B'),表示每个点的状态。

Output

行 1: 一个整数,最少的转弯次数。

Sample Input

3.xA...Bx.

Sample Output

2

BFS
bet[i][j][k]表示到达点(i, j)此时面向k方向的最少拐弯次数
bzoj3393和这道题一样,都是求最小拐弯次数,唯一的区别就是注意3393行和列是反的
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
typedef struct
{
int x, y;
int lt;
}Point;
Point now, temp;
queue<Point> q;
char str[105][105];
int bet[105][105][5], dir[4][2] = {1,0,0,1,-1,0,0,-1};
int main(void)
{
int n, i, j, x, y, ans, k;
scanf("%d", &n);
memset(str, 'x', sizeof(str));
memset(bet, 1, sizeof(bet));
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf(" %c", &str[i][j]);
if(str[i][j]=='A')
{
bet[i][j][4] = -1;
now.x = i, now.y = j;
now.lt = 4;
q.push(now);
}
if(str[i][j]=='B')
x = i, y = j;
}
}
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]=='x')
continue;
temp.lt = i;
if(temp.lt==now.lt) k = bet[now.x][now.y][now.lt];
else k = bet[now.x][now.y][now.lt]+1;
if(bet[temp.x][temp.y][temp.lt]>k)
{
bet[temp.x][temp.y][temp.lt] = k;
q.push(temp);
}
}
}
ans = 100000;
for(i=0;i<=3;i++)
ans = min(ans, bet[x][y][i]);
printf("%d\n", ans);
return 0;
}
/*
6
A..x.B
xx.x.x
...x..
.xxxx.
...x..
.x...x
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: