1671: [Usaco2005 Dec]Knights of Ni 骑士
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 281 Solved: 180
[Submit][Status][Discuss]
Description
Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible. Bessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000). The map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery. In order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block. It is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.
Input
Output
Sample Input
4 1 0 0 0 0 1 0
0 0 0 1 0 1 0 0
0 2 1 1 3 0 4 0
0 0 0 4 1 1 1 0
INPUT DETAILS:
Width=8, height=4. Bessie starts on the third row, only a few squares away
from the Knights.
Sample Output
HINT
这片森林的长为8,宽为4.贝茜的起始位置在第3行,离骑士们不远.
贝茜可以按这样的路线完成骑士的任务:北,西,北,南,东,东,北,东,东,南,南.她在森林的西北角得到一株她需要的灌木,然后绕过障碍把它交给在东南方的骑士.
Source
题解:分别从贝西和骑士所在位置bfs,枚举灌木位置,找最小值。
<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll long long
#define N 1005
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m,dis[N][N][2],a[N][N],nx,ny,mx,my,hx[N*N],hy[N*N],tot=0,ans;
int x[4]={-1,0,1,0};
int y[4]={0,-1,0,1};
bool vis[N][N];
queue< pair<int,int> > q;
void bfs(int xx,int yy,int k)
{
vis[xx][yy]=1;dis[xx][yy][k]=0;q.push(pair<int,int>(xx,yy));
while(!q.empty())
{
int sx=q.front().first,sy=q.front().second;
// cout<<sx<<' '<<sy<<endl;
q.pop();
for(int i=0;i<4;i++)
if(!vis[x[i]+sx][y[i]+sy]&&(a[x[i]+sx][y[i]+sy]==0||a[x[i]+sx][y[i]+sy]==4))
{
// cout<<x[i]+sx<<' '<<y[i]+sy<<"dd"<<endl;
dis[x[i]+sx][y[i]+sy][k]=dis[sx][sy][k]+1;
vis[x[i]+sx][y[i]+sy]=1;
q.push(pair<int,int>(x[i]+sx,y[i]+sy));
}
}
}
int main()
{
n=read();m=read();
memset(a,-1,sizeof(a));
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
a[i][j]=read();
if(a[i][j]==2)nx=i,ny=j;
if(a[i][j]==3)mx=i,my=j;
if(a[i][j]==4)hx[++tot]=i,hy[tot]=j;
}
bfs(nx,ny,0);
memset(vis,0,sizeof(vis));
bfs(mx,my,1);
ans=0x7fffffff;
for(int i=1;i<=tot;i++)
{
if(dis[hx[i]][hy[i]][0]!=0&&dis[hx[i]][hy[i]][1]!=0&&dis[hx[i]][hy[i]][0]+dis[hx[i]][hy[i]][1]<ans)
ans=dis[hx[i]][hy[i]][0]+dis[hx[i]][hy[i]][1];
}
cout<<ans<<endl;
}
</span>