初学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. Java程序员从笨鸟到菜鸟之(五)java开发常用类(包装,数字处理集合等)(下)...
  2. struts2 action之间参数的传递
  3. 计算机更新80072f76,windows update 80072f76错误
  4. flexible.js 移动端自适应方案
  5. 如何处理会话等待事件与ORA-21780故障
  6. 用域控制禁止本地存盘禁止使用移动磁盘以防止图纸泄密的解决方案
  7. 为什么大部分人会碌碌无为?
  8. Sublime Text 3 汉化小技巧
  9. 第二章:09流程控制[2switch]
  10. 【银联支付】php接入银联支付
  11. catalina.log localhost.log localhost_access_log.txt manager.log tomcat-stdout.log 文件占用过大,处理方式
  12. 期货计算机撮合成交的原则,期货ABC之行情及基本术语:八、价格优先、时间优先及撮合成交价的确定...
  13. 什么是冲激函数、时域卷积、冲激响应以及频响曲线
  14. U盘做成系统盘后如何恢复成普通U盘?
  15. 计算机科学二审需要多久,寒冬展风采,文体两开花——记计算机科学学院迎新晚会节目二审暨素美部“计体两开花”品牌活动...
  16. 问卷小程序php,问卷调查小程序(tp后台)
  17. 利用 clip-path 绘制不规则的图形
  18. 智学网登录不了java_智学网常见问题解答
  19. 【CC2530的点对点无线通讯开发(包含BasicRf协议栈、双击、长按、呼吸灯、跑马灯)】
  20. 中国电信中兴F412光猫——IPTV与网络单线复用

热门文章

  1. 计算机系女生电脑买什么牌子,适合女生的笔记本电脑 大学女生买什么电脑好...
  2. CSU_WF-中南大学网络服务队2020招新培训-硬件知识
  3. angular2 mysql_零起步学习angular2_angularjs教程_汇智网
  4. 笔记7.28 数据库经典问题
  5. 技嘉的UEFI修复windows与Ubuntu双系统引导+老毛桃修复引导失败+No EFI system partition was found.
  6. 吐槽一下typora
  7. [魔方]魔方七步初级教程
  8. VPP协议栈学习一:snd_wnd
  9. mac-python包安装错误 [Errno 1] Operation not permitted: '/var/folders/5n/vbm997m56xg3kw67y6bccn2m0000gn/T
  10. 前端体系认识和我的技术栈——wsdchong