初学BFS接触到的第一个比较有难度的题,没想在网上找代码就自己吭哧了半天。

开始以为可以用map<自定义struct,int>,编完才发现不可以

后来想想也就是用map的一个count功能,就自己编了一个count结构体的函数,用了vector。然后TLE,想来也是判断结点是否已经存在过太浪费时间。然后想通了,用三位数组id[3][maxn][maxn]判断是否存在过这个结点。

后来WA是因为没有清空队列

最后用C++ TLE,但是G++ 过了,1969ms的极限时间(要求2000ms)。

  还有很多不完善的地方,日后如果看到再反思。
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
struct node
{int pose, r0, c0, r1, c1,step;
};
const int maxn = 501;
char status[maxn][maxn];
queue<node> q;
int r, c;
int id[3][maxn][maxn];
node walk1(node head,int way)
{node next;switch (way){case 0: next.c0 = head.c0 - 2; next.c1 = head.c0 - 1; next.r0 = head.r0; next.r1 = head.r0; next.pose = 2; break;//左case 1: next.c0 = head.c0; next.c1 = head.c0; next.r0 = head.r0 - 2; next.r1 = head.r0 - 1; next.pose = 1; break;//上case 2: next.c0 = head.c0 + 1; next.c1 = head.c0 + 2; next.r0 = head.r0; next.r1 = head.r0; next.pose = 2; break;//右case 3: next.c0 = head.c0; next.c1 = head.c0; next.r0 = head.r0 + 1; next.r1 = head.r0 + 2; next.pose = 1; break;//下}return next;
}
node walk2(node head, int way)
{node next;if (head.pose==2)switch (way){case 0: next.pose = 0; next.c0 = head.c0 - 1; next.c1 = -1; next.r0 = head.r0; next.r1 = -1; break;case 1: next.pose = 2; next.c0 = head.c0; next.c1 = head.c1; next.r0 = head.r0 - 1; next.r1 = head.r0 - 1; break;case 2: next.pose = 0; next.c0 = head.c1 + 1; next.c1 = -1; next.r0 = head.r0; next.r1 = -1; break;case 3: next.pose = 2; next.c0 = head.c0; next.c1 = head.c1; next.r0 = head.r0 + 1; next.r1 = head.r0 + 1; break;}elseswitch (way){case 0: next.pose = 1; next.c0 = head.c0 - 1; next.c1 = head.c0 - 1; next.r0 = head.r0; next.r1 = head.r1; break;case 1: next.pose = 0; next.c0 = head.c0; next.c1 = -1; next.r0 = head.r0 - 1; next.r1 = -1; break;case 2: next.pose = 1; next.c0 = head.c0 + 1; next.c1 = head.c0 + 1; next.r0 = head.r0; next.r1 = head.r1; break;case 3: next.pose = 0; next.c0 = head.c0; next.c1 = -1; next.r0 = head.r1 + 1; next.r1 = -1; break;}return next;
}
int bfs(node ini, node goal)
{if (ini.pose == 0 && ini.r0 == goal.r0 && ini.c0 == goal.c0) return 0;   ini.step = 0;q.push(ini);id[ini.pose][ini.r0][ini.c0] = 1;while (!q.empty()){node head = q.front(); q.pop();if (head.pose == 0){for (int i = 0; i < 4; i++){node next;next = walk1(head, i);if (id[next.pose][next.r0][next.c0] || next.c0<0 || next.c0>c || next.c1<0 || next.c1>c || next.r0<0 || next.r0>r || next.r1<0 || next.r1>r || status[next.r0][next.c0] == '#' || status[next.r1][next.c1] == '#') continue;next.step = head.step + 1;//printf("next.step=%d,(%d,%d)和(%d,%d)\n", next.step,next.r0,next.c0,next.r1,next.c1);q.push(next);id[next.pose][next.r0][next.c0] = 1;}}else if (head.pose == 1){for (int i = 0; i < 4; i++){node next;next = walk2(head, i);if (next.pose == 1) { if ( id[next.pose][next.r0][next.c0] || next.c0<0 || next.c0>c || next.c1<0 || next.c1>c || next.r0<0 || next.r0>r || next.r1<0 || next.r1>r || status[next.r0][next.c0] == '#' || status[next.r1][next.c1] == '#') continue; }else if ( id[next.pose][next.r0][next.c0] == 1 || next.c0<0 || next.c0>c || next.r0<0 || next.r0>r || status[next.r0][next.c0] == '#' || status[next.r0][next.c0] == 'E') continue;next.step = head.step + 1;//printf("next.step=%d,(%d,%d)和(%d,%d)状态:%c\n", next.step, next.r0, next.c0, next.r1, next.c1, status[next.r0][next.c0]);q.push(next);id[next.pose][next.r0][next.c0] = 1;if (next.pose == 0 && next.c0 == goal.c0 && next.r0 == goal.r0) return next.step;}}else{for (int i = 0; i < 4; i++){node next;next = walk2(head, i);if (next.pose == 2) { if (id[next.pose][next.r0][next.c0] || next.c0<0 || next.c0>c || next.c1<0 || next.c1>c || next.r0<0 || next.r0>r || next.r1<0 || next.r1>r || status[next.r0][next.c0] == '#' || status[next.r1][next.c1] == '#') continue; }else if ( id[next.pose][next.r0][next.c0] || next.c0<0 || next.c0>c || next.r0<0 || next.r0>r || status[next.r0][next.c0] == '#' || status[next.r0][next.c0] == 'E') continue;next.step = head.step + 1;//printf("next.step=%d,(%d,%d)和(%d,%d) 状态:%c 姿势:%d 是否有:%d\n", next.step, next.r0, next.c0, next.r1, next.c1, status[next.r0][next.c0], next.pose, id[next.pose][next.r0][next.c0]);q.push(next);id[next.pose][next.r0][next.c0] = 1;if (next.pose == 0 && next.c0 == goal.c0 && next.r0 == goal.r0) return next.step;}}}return -1;
}int main()
{while (scanf("%d%d", &r, &c) != EOF && r && c){node ini;node goal;int flag = 1;getchar();for (int i = 0; i < r; i++){for (int j = 0; j < c; j++){scanf("%c", &status[i][j]);if (status[i][j] == 'X'){if (flag){flag = 0;ini.pose = 0;ini.r0 = i;ini.c0 = j;ini.r1 = -1;ini.c1 = -1;}else{ini.r1 = i;ini.c1 = j;if (ini.c0 == ini.c1) ini.pose = 1;else ini.pose = 2;}}else if (status[i][j] == 'O'){goal.pose = 0;goal.r0 = i;goal.c0 = j;goal.r1 = -1;goal.c1 = -1;}}getchar();}memset(id, 0, sizeof(id));while (!q.empty()) q.pop();int res=bfs(ini,goal);if (res == -1) printf("Impossible\n");else printf("%d\n", res);}return 0;
}

POJ3322解题报告相关推荐

  1. uscao 线段树成段更新操作及Lazy思想(POJ3468解题报告)

    线段树成段更新操作及Lazy思想(POJ3468解题报告) 标签: treequerybuildn2cstruct 2011-11-03 20:37 5756人阅读 评论(0) 收藏 举报  分类: ...

  2. 解题报告(十八)数论题目泛做(Codeforces 难度:2000 ~ 3000 + )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  3. 【解题报告系列】超高质量题单 + 题解(ACM / OI)超高质量题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我新写的超高质量的题解和代码,题目难度不 ...

  4. 解题报告(三)多项式求值与插值(拉格朗日插值)(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  5. 解题报告(十三)中国剩余定理(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  6. 解题报告(四)生成函数(ACM/ OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  7. 解题报告(八) prufer 序列与 Cayley 公式(ACM / OI)超高质量题解

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

  8. 解题报告(一)E、(BZOJ4589)Hard Nim(博弈论 + FWT)

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

  9. 解题报告(五)组合计数(ACM / OI)超高质量题解

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

最新文章

  1. 下载最新版本Maven 3.3.9 ,检测安装是否成功时发现Java版本JDK却低于1.7时报错
  2. Ubuntu系统启动错误问题的解决
  3. cf1039D 分块
  4. 逻辑回归 python_深入研究Python的逻辑回归
  5. 学习pythonweb开发_Python学习--20 Web开发
  6. JavaScript 数据类型检测终极解决方案
  7. 在腾讯云主机上使用URLOS一键安装yoshop萤火虫小程序商城
  8. iphone6s读写速度测试软件,iPhone6S/7/8运行iOS13速度测试:结果有点失望
  9. 湘潭大学信息安全课作业答案4
  10. 如何选择云主机或者VPS挂EA?
  11. 网站优化相关理论概述
  12. 游戏建模入门教程:绝地求生—PUBG的游戏模型制作流程
  13. 类似搜狐新闻的栏目定制
  14. 华为策略路由加等价路由_华为策略路由配置实例
  15. cad2016中选择全图字体怎么操作_cad教程分享CAD中如何删除顽固图层?
  16. 东华大学 oj1——求长方形的面积和周长
  17. 区块链被中央点名了!腾讯研究院这份白皮书告诉你为什么
  18. vue之猫眼json数据的获取直接用于自己的vue项目中,swiper轮播插件的坑
  19. vscode 保存自动格式化代码
  20. catia 创成钣金设计_CATIA钣金设计实例教程

热门文章

  1. 主流芯片解决方案Ambarella的高清网络摄像机、德州仪器和控制海思
  2. java实现 腾讯人机验证 + 前端
  3. 简单计算机java程序_JAVA程序员需要知道的计算机底层基础10-操作系统引导程序的简单...
  4. POJ 1625 Censored!
  5. 微信表情符号 mysql_Emoji表情符号入MySQL数据库报错的解决方案
  6. 平均 15144 元、软件工程师占比最高,2021 年 2 月程序员工资最新出炉!
  7. 发送候选文字到光标所在位置
  8. OpenCV显示中文字体
  9. 使用AWK和XARGS为文件批量改名
  10. linux下7z压缩包解压