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

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

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 579   Solved: 280
[ Submit][ Status][ Discuss]

. . B x .
. x x A .
. . . x .
. x . . .
. . x . .

## Input

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

3
.xA
...
Bx.

2

## Source

Silver

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<fstream>
#define N 101
#define M 100001
using namespace std;
int n,m;
char mp[N][N];
int ip,id[N][N];
int vis[N][N][4];
struct node{int x,y,dir,c;}u,ed,v;
int s,t;
queue<node>Q;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",mp[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(mp[i][j]=='A')
u.x=i,u.y=j,u.c=0;
else if(mp[i][j]=='B')
ed.x=i,ed.y=j;
u.dir=0;Q.push(u);u.dir=1;Q.push(u);
u.dir=2;Q.push(u);u.dir=3;Q.push(u);
memset(vis,0x3f3f3f3f,sizeof(vis));
while(!Q.empty())
{
u=Q.front();Q.pop();
if(vis[u.x][u.y][u.dir]<u.c)continue;
int x=u.x,y=u.y;
for(int i=0;i<4;i++)
{
int xx=x+dx[i],yy=y+dy[i];
if(xx<1||xx>n||yy<1||yy>n||mp[xx][yy]=='x')continue;
bool f=!(u.dir==i);
v.x=xx,v.y=yy,v.c=u.c+f,v.dir=i;
if(vis[xx][yy][i]<=v.c)continue;
vis[xx][yy][i]=v.c;
Q.push(v);
}
}int ans=0x3f3f3f3f;
for(int i=0;i<4;i++)ans=min(ans,vis[ed.x][ed.y][i]);
printf("%d\n",ans);
}


10-30 268
06-05 9938
12-31