当前位置:首页 » 《资源分享》 » 正文

【题解】【洛谷P1126】 机器人搬重物_Fighter的博客_机器人搬重物

12 人参与  2021年09月25日 08:23  分类 : 《资源分享》  评论

点击全文阅读


P1126 机器人搬重物

传送门


这道题本来没啥好说的,但细节实在比较多,被坑了好多次。

  1. 首先输入的是格子图,需要转化成点图,具体操作是a[i][j]=a[i-1][j-1]=a[i][j-1]=a[i-1][j]=1
  2. 最坑的一个点在于,平时写宽搜的时候,遇到出边界或者不能访问的点时,都是直接进入下一层循环(continue),但在这道题中,由于可以走1~3步,那么当路径上出现障碍时,则不能进行下一轮循环,需要break。

代码:

#include <bits/stdc++.h>
#define MAX 55
using namespace std;

int mod(int x){
	return (x+4)%4;
}

struct pt{
	int x, y, dir, step;
	pt(){}
	pt(int a, int b, int c, int d):x(a), y(b), dir(c), step(d){}
};

const int movx[] = {0,1,0,-1}, movy[] = {1,0,-1,0};
int a[MAX][MAX];
bool vis[MAX][MAX][5];
int n, m;
pt st, ed;

void bfs(){
	queue<pt> q;
	bool flag = false;
	st.step = 0;
	q.push(st);
	vis[st.x][st.y][st.dir] = true;
	while(!q.empty()){
		pt t = q.front();
		q.pop();
		if(t.x == ed.x && t.y == ed.y){
			cout << t.step << endl;
			flag = true;
			break;
		}
		for(int i = 1; i <= 3; i++){
			int u, v;
			u = t.x + i*movx[t.dir];
			v = t.y + i*movy[t.dir];
			if(u<=0 || u>=n || v<=0 || v>=m || a[u][v] == 1){
				break;
			}
			if(vis[u][v][t.dir]){
				continue;
			}
			vis[u][v][t.dir] = true;
			q.push(pt(u, v, t.dir, t.step+1));
		}
		if(!vis[t.x][t.y][mod(t.dir+1)]){
			vis[t.x][t.y][mod(t.dir+1)] = true;
			q.push(pt(t.x, t.y, mod(t.dir+1), t.step+1));
		}
		if(!vis[t.x][t.y][mod(t.dir-1)]){
			vis[t.x][t.y][mod(t.dir-1)] = true;
			q.push(pt(t.x, t.y, mod(t.dir-1), t.step+1));
		}
	}
	if(!flag){
		cout << -1 << endl;
	}
}

int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			scanf("%d", &a[i][j]);
			if(a[i][j] == 1){
				a[i-1][j-1] = a[i-1][j] = a[i][j-1] = 1;
			}
		}
	}
	cin >> st.x >> st.y >> ed.x >> ed.y;
	char c;
	cin >> c;
	switch(c){
		case 'E':
			st.dir = 0; break;
		case 'S':
			st.dir = 1; break;
		case 'W':
			st.dir = 2; break;
		case 'N':
			st.dir = 3; break;
	}
	bfs();
	
	return 0;
}

 


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/28549.html

这道  的是  都是  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 家宴过后,我捉奸了庶妹和我老公后续+番外_庶妹老公侍卫读者推荐_小说后续在线阅读_无删减免费完结_
  • 首富老公为三姐妹点天灯挥金如土,我换嫁贺总轰动全国强推_胥淮岁岁过敏最新阅读_小说后续在线阅读_无删减免费完结_
  • 未婚夫和同事孕期领证,我转身嫁入豪门推荐_沈泽光安南拿泽光小编推荐_小说后续在线阅读_无删减免费完结_
  • 重回奥运种子选手陷害我的这天番茄热门_林子李梦冷笑在线看_小说后续在线阅读_无删减免费完结_
  • 知微向海完结全文_陆知衍纪瑶奶奶最新阅读_小说后续在线阅读_无删减免费完结_
  • 被污蔑送错外卖后,我杀疯了阅读_小徐胡闹阅读_小说后续在线阅读_无删减免费完结_
  • 我坐上评委席后,把我踢出乐队的男友悔疯了快手热门_乔枝姐阿媛程戬常读_小说后续在线阅读_无删减免费完结_
  • 当风吹落了雨TOP10_老公陈角鹰薇薇大反击_小说后续在线阅读_无删减免费完结_
  • 未婚夫逼我放弃继承权后,全家悔疯了一口气完结_沈雨雨柔养老校园甜文_小说后续在线阅读_无删减免费完结_
  • 手撕无耻老婆一家宝藏文_小姨子钟琳老公人气小说_小说后续在线阅读_无删减免费完结_
  • 愿得一人心常读_萧城蒋雪柔华冉优质全文_小说后续在线阅读_无删减免费完结_
  • 女士的玩具推文_杜小灵白月光杜雪必读文_小说后续在线阅读_无删减免费完结_

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1