博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
洛谷 P1126 机器人搬重物
阅读量:6537 次
发布时间:2019-06-24

本文共 2837 字,大约阅读时间需要 9 分钟。

坑点极多的一道bfs好题!

#include 
#include
#include
#include
#include
using namespace std;//Mystery_Sky//#define INF 0x3f3f3f3fconst int M = 100;struct node{ int x, y, step, dirt, turn;//dirt取值为1, 2, 3, 4时别表示东,西,南,北。 node (int xx, int yy, int ss, int dd, int tt) : x(xx), y(yy), step(ss), dirt(dd), turn(tt){}};queue
q;int n, m, ans, x1, x2, y1, y2;char to;int dis[M][M], map[M][M];bool vis[M][M];inline void bfs(int x1, int y1, int x2, int y2, char to){ int drt; if(to == 'E') drt = 1; else if(to == 'W') drt = 2; else if(to == 'S') drt = 3; else drt = 4; q.push(node(x1, y1, 0, drt, 0)); while(!q.empty()) { node top = q.front(); q.pop(); int dx, dy; for(int i = 1; i <= 5; i++) {//i=4表示左转, i=5表示右转 if(i != 4 && i != 5) { drt = top.dirt; if(drt == 1) { dx = top.x; dy = top.y + i; } else if(drt == 2) { dx = top.x; dy = top.y - i; } else if(drt == 3) { dx = top.x + i; dy = top.y; } else { dx = top.x - i; dy = top.y; } if(map[dx][dy] == 1) i = 3; if(dx <= 0 || dy <= 0 || dx > n || dy > n) { i = 3; continue; } if(vis[dx][dy]) { continue; } if(dx == x2 && dy == y2) { dis[x2][y2] = top.step + 1; return; } dis[dx][dy] = min(top.step + 1, dis[dx][dy]); vis[dx][dy] = 1; q.push(node(dx, dy, top.step+1, drt, 0)); } else if(i == 4){ int t = top.turn; if(t > 2) continue; dx = top.x, dy = top.y; drt = top.dirt; if(drt == 1) drt = 4; else if(drt == 2) drt = 3; else if(drt == 3) drt = 1; else drt = 2; dis[dx][dy] = min(top.step + 1, dis[dx][dy]); vis[dx][dy] = 1; q.push(node(dx, dy, top.step + 1, drt, t+1)); } else { int t = top.turn; if(t > 2) continue; dx = top.x, dy = top.y; drt = top.dirt; if(drt == 1) drt = 3; else if(drt == 2) drt = 4; else if(drt == 3) drt = 2; else drt = 1; dis[dx][dy] = min(top.step + 1, dis[dx][dy]); vis[dx][dy] = 1; q.push(node(dx, dy, top.step + 1, drt, t+1)); } } } return;}int main() { int a; scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { scanf("%d", &a); if(a == 1) { vis[i][j] = vis[i][j+1] = vis[i+1][j] = vis[i+1][j+1] = 1; map[i][j] = map[i][j+1] = map[i+1][j] = map[i+1][j+1] = 1; } } n++, m++; for(int i = 1; i <= m; i++) vis[1][i] = vis[n][i] = 1; for(int i = 1; i <= n; i++) vis[i][1] = vis[i][m] = 1; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); getchar(); x1++, x2++, y1++, y2++; scanf("%c", &to); memset(dis, INF, sizeof(dis)); if(vis[x1][y1] || vis[x2][y2]) return 0 * printf("-1\n"); if(x1 == x2 && y1 == y2) return 0 * printf("0\n"); bfs(x1, y1, x2, y2, to);// for(int i = 1; i <= n; i++){// for(int j = 1; j <= m; j++){// if(dis[i][j] == INF) printf("-1 ");// else printf("%d ", dis[i][j]);// }// printf("\n");// } if(dis[x2][y2] == INF) printf("-1\n"); else printf("%d\n", dis[x2][y2]); return 0;}

转载于:https://www.cnblogs.com/Benjamin-cpp/p/10816065.html

你可能感兴趣的文章
手机WAP前端开发标准
查看>>
vue-cli脚手架一些插件安装elementui和axios
查看>>
[Gradle] 在 Eclipse 下利用 gradle 构建系统
查看>>
JAVAWEB 一一 Hibernate(框架)
查看>>
与、或、异或、取反、左移和右移
查看>>
jQuery根据元素值删除数组元素的方法
查看>>
Linux基础学习(14)--日志管理
查看>>
vue常用的指令
查看>>
matlab练习程序(随机游走图像)
查看>>
Linux命令行下运行java.class文件
查看>>
input文本框实现宽度自适应代码实例
查看>>
C#基本数据类型 <思维导图>
查看>>
POJ3321 Apple Tree (树状数组)
查看>>
一个程序员的自白(延迟满足)
查看>>
protocol buffers的编码原理
查看>>
行为型设计模式之命令模式(Command)
查看>>
减少死锁的几个常用方法
查看>>
HDFS 核心原理
查看>>
正确配置jstl的maven依赖,jar包冲突的问题终于解决啦
查看>>
利用KMP算法解决串的模式匹配问题(c++) -- 数据结构
查看>>