题目描述
机器人移动学会(RMI)现在正尝试用机器人搬运物品。机器人的形状是一个直径$1.6米的球。在试验阶段,机器人被用于在一个储藏室中搬运货物。储藏室是一个N \times MN×M的网格,有些格子为不可移动的障碍。机器人的中心总是在格点上,当然,机器人必须在最短的时间内把物品搬运到指定的地方。机器人接受的指令有:向前移动11步(Creep);向前移动2步(Walk);向前移动33步(Run);向左转(Left);向右转(Right)。每个指令所需要的时间为11秒。请你计算一下机器人完成任务所需的最少时间。
输入输出格式
输入格式:
第一行为两个正整数N,M(N,M \le 50)N,M(N,M≤50),下面NN行是储藏室的构造,00表示无障碍,11表示有障碍,数字之间用一个空格隔开。接着一行有44个整数和11个大写字母,分别为起始点和目标点左上角网格的行与列,起始时的面对方向(东EE,南SS,西WW,北NN),数与数,数与字母之间均用一个空格隔开。终点的面向方向是任意的。
输出格式:
一个整数,表示机器人完成任务所需的最少时间。如果无法到达,输出-1−1。
输入输出样例
输入样例#1:
9 10
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
7 2 2 7 S
输出样例#1: 12
被卡了两天,太惨了,做题一定要想清楚细节再动手,否则就是无穷无尽的改错。
机器人有直径1.6,这样的话建图的时候要注意,而且边界一定想清楚,还有行数列数和下标的关系,都是比较容易出错的地方。
#include<bits/stdc++.h>
#define N 66
using namespace std;
int Map[N][N],vis[N][N],go[][2]={-1,0,0,1,1,0,0,-1},m,n,sm,sn,em,en,to,res=0x3f3f3f3f;
struct Node{
int m,n,time,to;
};
void BFS()
{
Node nn;
nn.m=sm,nn.n=sn,nn.time=0,nn.to=to;
queue<Node> q;
q.push(nn);
while(q.size())
{
nn=q.front();
q.pop();
Node tem;
for(int k=-1;k<3;k++)//方向
{
tem.to=(nn.to+k+4)%4;
for(int i=1;i<=3;i++)//步长
{
tem.m=nn.m+go[tem.to][0]*i;
tem.n=nn.n+go[tem.to][1]*i;
tem.time=nn.time+abs(k)+1;
if(tem.m<=0 || tem.m>=m || tem.n<=0 || tem.n>=n || Map[tem.m][tem.n]!=0) break;
if( vis[tem.m][tem.n]>tem.time )
vis[tem.m][tem.n]=tem.time,q.push(tem);
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>m>>n;
memset(vis,0x3f,sizeof(vis));
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
int tem;
cin>>tem;
if(tem)
Map[i][j]=Map[i][j+1]=Map[i+1][j]=Map[i+1][j+1]=tem;
}
for(int j=0;j<=n;j++) Map[0][j]=Map[m][j]=1;
for(int j=0;j<=m;j++) Map[j][0]=Map[j][n]=1;
char ch;
cin>>sm>>sn>>em>>en>>ch;
if(ch=='N') to=0;
else if(ch=='E') to=1;
else if(ch=='S') to=2;
else if(ch=='W') to=3;
if(sn==en&&sm==em){
cout<<0<<endl;
return 0;
}
BFS();
for(int i=0;i<4;i++)
if(vis[em][en]!=0x3f3f3f3f)
res=min(res,vis[em][en]);
if(res!=0x3f3f3f3f)
cout<<res<<endl;
else cout<<-1<<endl;
return 0;
}