机器人搬重物
题目描述
机器人移动学会(RMI
)现在正尝试用机器人搬运物品。机器人的形状是一个直径
1.6
1.6
1.6米的球。在试验阶段,机器人被用于在一个储藏室中搬运货物。储藏室是一个
N
×
M
N \times M
N×M 的网格,有些格子为不可移动的障碍。机器人的中心总是在格点上,当然,机器人必须在最短的时间内把物品搬运到指定的地方。机器人接受的指令有:向前移动
1
1
1步(Creep
);向前移动2步(Walk
);向前移动
3
3
3 步(Run
);向左转(Left
);向右转(Right
)。每个指令所需要的时间为
1
1
1 秒。请你计算一下机器人完成任务所需的最少时间。
输入格式
第一行为两个正整数 N , M ( N , M ≤ 50 ) N,M(N,M \le 50) N,M(N,M≤50),下面 N N N行是储藏室的构造, 0 0 0表示无障碍, 1 1 1表示有障碍,数字之间用一个空格隔开。接着一行有 4 4 4个整数和 1 1 1个大写字母,分别为起始点和目标点左上角网格的行与列,起始时的面对方向(东 E E E,南 S S S,西 W W W,北 N N N),数与数,数与字母之间均用一个空格隔开。终点的面向方向是任意的。
输出格式
一个整数,表示机器人完成任务所需的最少时间。如果无法到达,输出 − 1 -1 −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
思路:
其实刚开始我以为只需要用常规的广度优先搜索就可以了,码完交上是70分
后来看题解和洛谷评论区发现,对于某一个点,先到的有可能比后到的花费的步骤更少(玄学,这点我一直没有理解)
所以,如果按照这种思路的话,只需要把标记数组改为在出队后进行标记即可(类似于Dijkstra),具体细节见以下代码
#include <bits/stdc++.h>
using namespace std;
#define de(x) cout<<x<<" ";
#define Pu puts("");
#define sf(x) scanf("%d",&x);
typedef long long ll;
const int N=5e1+10,mod=100003;
const int inf=0x3f3f3f3f;
bool st[N][N][5];//标记位置数组
int n,m,ans;
struct E{
int x,y,to,u;
};
bool volid(int x,int y,int to){
if(x<2||y<2||x>n||y>m) return 0;
if(st[x][y][to]==1) return 0;
return 1;
}
void bfs(){
int sx,sy,ex,ey;char ch;
cin>>sx>>sy>>ex>>ey>>ch;
sx++;sy++;ex++;ey++;
E nw,nx;
nw.x=sx,nw.y=sy;nw.u=0;
if(ch=='N') nw.to=0;
else if(ch=='E') nw.to=1;
else if(ch=='S') nw.to=2;
else nw.to=3;
//st[nw.x][nw.y][nw.to]=1;
queue<E>q;
q.push(nw);
int x,y,to,u;
while(!q.empty()){
nw=q.front();q.pop();
x=nw.x;y=nw.y;to=nw.to;u=nw.u;
if(x==ex&&y==ey){//已经找到
ans=min(ans,u);
continue;
}
if(st[x][y][to]==1) continue;
st[x][y][to]=1;
if(st[x][y][(to+1)%4]==0){//右转
nx.x=x;nx.y=y;nx.to=(to+1)%4;nx.u=u+1;
//st[x][y][(to+1)%4]=1;
q.push(nx);
}
if(st[x][y][(to-1+4)%4]==0){//左转
nx.x=x;nx.y=y;nx.to=(to-1+4)%4;nx.u=u+1;
//st[x][y][(to-1+4)%4]=1;
q.push(nx);
}
if(to==0){
if(volid(x-1,y,to)&&volid(x-2,y,to)&&volid(x-3,y,to)) {q.push({x-3,y,to,u+1});}
if(volid(x-1,y,to)&&volid(x-2,y,to)) {q.push({x-2,y,to,u+1});}
if(volid(x-1,y,to)) {q.push({x-1,y,to,u+1});}
}
if(to==1){
if(volid(x,y+3,to)&&volid(x,y+2,to)&&volid(x,y+1,to)) {q.push({x,y+3,to,u+1});}
if(volid(x,y+2,to)&&volid(x,y+1,to)) {q.push({x,y+2,to,u+1});}
if(volid(x,y+1,to)) {q.push({x,y+1,to,u+1});}
}
if(to==2){
if(volid(x+1,y,to)&&volid(x+2,y,to)&&volid(x+3,y,to)) {q.push({x+3,y,to,u+1});}
if(volid(x+1,y,to)&&volid(x+2,y,to)) {q.push({x+2,y,to,u+1});}
if(volid(x+1,y,to)) {q.push({x+1,y,to,u+1});}
}
if(to==3){
if(volid(x,y-1,to)&&volid(x,y-2,to)&&volid(x,y-3,to)) {q.push({x,y-3,to,u+1});}
if(volid(x,y-1,to)&&volid(x,y-2,to)) {q.push({x,y-2,to,u+1});}
if(volid(x,y-1,to)) {q.push({x,y-1,to,u+1});}
}
}
}
int main(){
cin>>n>>m;
int x;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
sf(x)
if(x==1){
for(int k=0;k<4;k++){
st[i][j][k]=1;st[i+1][j][k]=1;
st[i][j+1][k]=1;st[i+1][j+1][k]=1;
}
}
}
}
ans=inf;
bfs();
if(ans==inf) printf("-1\n");
else printf("%d\n",ans);
return 0;
}