P1126 机器人搬重物

#include<bits/stdc++.h>
using namespace std;
const int maxn=55;
int n,m,sx,sy,tx,ty;
bool mp[maxn][maxn];
bool vis[maxn][maxn][4];
int xx[]= {-1,0,1,0};
int yy[]= {0,1,0,-1};//移动
struct Rob {
	int x,y,s,d;
};
int main() {
	cin>>n>>m;
	for(int i=1; i<=n; i++ ) {
		for(int j=1; j<=m; j++) {
			int t;
			cin>>t;
			if(t) {
				mp[i][j]=mp[i-1][j]=mp[i][j-1]=mp[i-1][j-1]=1;//四个角都不能走
			}
		}
	}
	queue<Rob>Q;
	char tt;
cin>>sx>>sy>>tx>>ty>>tt;
	Rob t1;
	t1.x=sx;
	t1.y=sy;
	t1.s=0;
	if(tt=='N') t1.d=0;//数字来存方向
	if(tt=='E') t1.d=1;
	if(tt=='S') t1.d=2;
	if(tt=='W') t1.d=3;
	Q.push(t1);
	while(!Q.empty()) {
		t1=Q.front();
		vis[t1.x][t1.y][t1.d]=1;//标记
		if(t1.x==tx&&t1.y==ty) {
			cout<<t1.s;
			return 0;
		}
		Rob t=t1;
		t.s++;
		t.d=(t.d+1)%4;//方向1步
		if(!vis[t.x][t.y][t.d]) {
			Q.push(t);
		}
		t.d=(t.d+2)%4;//方向2步
		if(!vis[t.x][t.y][t.d]) {
			Q.push(t);
		}
		t.d=t1.d;
		t.x+=xx[t.d];//移动
		t.y+=yy[t.d];
		if(t.x>=1&&t.x<n&&t.y>=1&&t.y<m&&!vis[t.x][t.y][t.d]&&!mp[t.x][t.y]) {//一步
			Q.push(t);
			t.x+=xx[t.d];
			t.y+=yy[t.d];
			if(t.x>=1&&t.x<n&&t.y>=1&&t.y<m&&!vis[t.x][t.y][t.d]&&!mp[t.x][t.y]) {//两步
				Q.push(t);
				t.x+=xx[t.d];
				t.y+=yy[t.d];
				if(t.x>=1&&t.x<n&&t.y>=1&&t.y<m&&!vis[t.x][t.y][t.d]&&!mp[t.x][t.y]) {三步
					Q.push(t);
				}
			}
		}
		Q.pop();//弹出
	}
	cout<<-1;//否则不行
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值