1671: [Usaco2005 Dec]Knights of Ni 骑士
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 384 Solved: 244
[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行,离骑士们不远.
贝茜可以按这样的路线完成骑士的任务:北,西,北,南,东,东,北,东,东,南,南.她在森林的西北角得到一株她需要的灌木,然后绕过障碍把它交给在东南方的骑士.
听alone_wolf说是分层图最短路(还不会。。。发现有问题的可以评论)
把图贴一份在旁边,两个图中相同的灌木丛之间连边
起点在原图,终点在贴的图
因为边权都是0
所以可以01bfs求最短路
附上我的MLE代码
时间效率高一些,但是空间过不去,想要AC代码。。去粘一个吧。。
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f*x;
}
const int N=1000010;
int n,m,ecnt,last[N<<1],dis[N<<1],q[N],a[1010][1010],S,ans;
bool book[N<<2];
struct EDGE{int to,nt,val;}e[N<<3];
inline void add(int u,int v,int val)
{e[++ecnt]=(EDGE){v,last[u],val};last[u]=ecnt;}
void bfs()
{
int head=0,tail=1;q[0]=S;
while(head!=tail)
{
int u=q[head++];if(head==N-10)head=0;
for(int i=last[u];i;i=e[i].nt)
if(!dis[e[i].to]&&e[i].to!=S)
{
q[tail++]=e[i].to;if(tail==N-10)tail=0;
dis[e[i].to]=dis[u]+e[i].val;
if(book[e[i].to]){ans=e[i].to;return ;}
}
}
}
int main()
{
m=read();n=read();
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)a[i][j]=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(a[i][j]==1)continue;
if(i>1&&a[i-1][j]!=1)add((i-1)*m+j,(i-2)*m+j,1),add((i-1)*m+j+n*m,(i-2)*m+j+n*m,1);
if(i<n&&a[i+1][j]!=1)add((i-1)*m+j,i*m+j,1),add((i-1)*m+j+n*m,i*m+j+n*m,1);
if(j>1&&a[i][j-1]!=1)add((i-1)*m+j,(i-1)*m+j-1,1),add((i-1)*m+j+n*m,(i-1)*m+j-1+n*m,1);
if(j<m&&a[i][j+1]!=1)add((i-1)*m+j,(i-1)*m+j+1,1),add((i-1)*m+j+n*m,(i-1)*m+j+1+n*m,1);
if(a[i][j]==4)add((i-1)*m+j,(i-1)*m+j+n*m,0);
if(a[i][j]==3)book[(i-1)*m+j+n*m]=1;
if(a[i][j]==2)S=(i-1)*m+j;
}
bfs();
printf("%d\n",dis[ans]);
return 0;
}