题意:一个迷宫,起点到终点的路径,不用递归。

题解:

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string>
  5. #include<string.h>
  6. #include<stack>
  7. #include<iostream>
  8. #include<map>
  9. using namespace std;
  10. const int maxn = 1e5 + ;
  11. int dir[][] = { ,,,,-,,,- };
  12. struct node {
  13. int x, y;
  14. node(int x=, int y=) :x(x), y(y) {}
  15. bool operator < (const node &q) const { return x < q.x; }
  16. bool operator ==(const node &a)const { return x == a.x&&y == a.y; }
  17. };
  18. struct prob {
  19. int ord;
  20. node seat;
  21. int di;
  22. prob(int x , node y, int z ) :ord(x), seat(y), di(z) {}
  23. };
  24. int mp[][] = {
  25. { ,,,,,,,,, },
  26. { ,,,,,,,,, },
  27. { ,,,,,,,,, },
  28. { ,,,,,,,,, },
  29. { ,,,,,,,,, },
  30. { ,,,,,,,,, },
  31. { ,,,,,,,,, },
  32. { ,,,,,,,,, },
  33. { ,,,,,,,,, },
  34. { ,,,,,,,,, },
  35. };
  36.  
  37. stack<prob> S, road;
  38. int vis[][];
  39. map<node, node>p;
  40. int n, m;
  41. bool ok(int x, int y) {
  42. if (mp[x][y] == || x < || x >= m || y < || y >= n || vis[x][y])return ;
  43. else return ;
  44. }
  45. node nextpos(node n,int i) {
  46. return node(n.x + dir[i][], n.y + dir[i][]);
  47. }
  48. int main() {
  49.  
  50. cin >> n >> m;
  51.  
  52. int sr, sc; cin >> sr >> sc;
  53. int er, ec; cin >> er >> ec;
  54. node end = node(er, ec);
  55. node start = node(sr, sc);
  56. //S.push(0,node(sr, sc),0);
  57. node now=start;
  58. int nows=;
  59. prob e= prob(nows, now, );
  60. do {
  61. if (ok(now.x, now.y)) {
  62. vis[now.x][now.y] = ;
  63. e = prob(nows, now, );
  64. S.push(e);
  65. if (now== end)break;
  66. now = nextpos(now, );
  67. nows++;
  68. }
  69. else {
  70. if (!S.empty()) {
  71. e = S.top();
  72. S.pop();
  73. while (e.di == && !S.empty()) {
  74. vis[e.seat.x][e.seat.y] = ;
  75. e = S.top();
  76. S.pop();
  77. }
  78. if (e.di < ) {
  79. e.di++; S.push(e);
  80. now = nextpos(now, e.di);
  81. }///
  82.  
  83. }
  84. }
  85. } while (!S.empty());
  86.  
  87. stack<prob>ans;
  88. while (!(S.empty()) ){
  89. ans.push(S.top());
  90. S.pop();
  91. }
  92. while (!(ans.empty())) {
  93. cout << ans.top().seat.x << ' ' << ans.top().seat.y << endl;
  94. ans.pop();
  95. }
  96. cin >> n;
  97. }

附:之前模仿bfs写的,不知道怎么存路径。。

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string>
  5. #include<string.h>
  6. #include<stack>
  7. #include<iostream>
  8. #include<map>
  9. using namespace std;
  10. const int maxn = 1e5 + ;
  11. int dir[][] = { ,,,,-,,,- };
  12. struct node {
  13. int x, y;
  14. node(int x=, int y=) :x(x), y(y) {}
  15. bool operator < (const node &q) const { return x < q.x; }
  16. };
  17. int mp[][] = {
  18. { ,,,,,,,,, },
  19. { ,,,,,,,,, },
  20. { ,,,,,,,,, },
  21. { ,,,,,,,,, },
  22. { ,,,,,,,,, },
  23. { ,,,,,,,,, },
  24. { ,,,,,,,,, },
  25. { ,,,,,,,,, },
  26. { ,,,,,,,,, },
  27. { ,,,,,,,,, },
  28. };
  29.  
  30. stack<node> S, road;
  31. int vis[][];
  32. map<node, node>p;
  33. int n, m;
  34. bool illeg(int x, int y) {
  35. if (mp[x][y] == '' || x < || x >= m || y < || y >= n || vis[x][y])return ;
  36. else return ;
  37. }
  38.  
  39. int main() {
  40.  
  41. cin >> n >> m;
  42.  
  43. int sr, sc; cin >> sr >> sc;
  44. int er, ec; cin >> er >> ec;
  45. S.push(node(sr, sc));
  46. while (!S.empty()) {
  47. node now = S.top(); S.pop();
  48. road.push(now);
  49. //if (mp[now.x][now.y] == '1' || now.x < 0 || now.x >= m || now.y < 0 || now.y >= n || vis[now.x][now.y])continue;
  50. //S.push(now);
  51. if (now.x == er&&now.y == ec) break;
  52. for(int i=;i<;i++){
  53. int dx = now.x + dir[i][]; int dy = now.y + dir[i][];
  54. if(illeg(dx,dy))continue;
  55. if (vis[dx][dy])continue;
  56. S.push(node(dx, dy));
  57. node x = node(dx, dy);
  58. p[x] = now;
  59. vis[dx][dy] = ;
  60. }
  61. /*for (int i = 0; i < m; i++){
  62. for (int j = 0; j < n; j++) {
  63. cout << vis[i][j];
  64. }
  65. cout<<endl;
  66. }
  67. cout << endl;*/
  68. }
  69. node now=node(er,ec);
  70. node x = node(sr, sc);
  71. while (!(now.x==sr&&now.y==sc) ){
  72. cout << now.x << ' ' << now.y << endl;
  73. now = p[now];
  74. }
  75. cin >> n;
  76. }

【作业】用栈模拟dfs的更多相关文章

  1. UVALive 3486/zoj 2615 Cells(栈模拟dfs)

    这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...

  2. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

  3. 【栈模拟dfs】Cells UVALive - 3486

    题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...

  4. LA 3486 Cells(判祖先+栈模拟dfs)

    https://vjudge.net/problem/UVALive-3486 题意: 判断u是否是v的祖先. 思路: 很简单,dfs遍历,记录每个节点第一次访问时的时间戳 in[i] 和第二次访问时 ...

  5. 深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)

    将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...

  6. 百炼3752:走迷宫--栈实现dfs

    3752:走迷宫 总时间限制:  1000ms 内存限制:  65536kB 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走.给定一个迷宫,求从左上角走到右下角最 ...

  7. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  8. UVALive 7454 Parentheses (栈+模拟)

    Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...

  9. poj1363Rails(栈模拟)

    主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...

随机推荐

  1. 100个MySQL 的调节和优化的提示

    100个MySQL 的调节和优化的提示 MySQL是一个功能强大的开源数据库.随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它的极限.这里是101条调节和优化MySQL安装的技巧. ...

  2. 在SharePoint 2013 场中移除服务器,提示 cacheHostInfo is null 错误

    Problem 在SharePoint 2013 场中移除服务器,提示 cacheHostInfo is null 错误 Resolution 这是由于SharePoint 2013中分布式缓存实例( ...

  3. FFmpeg: AVFormatContext 结构体分析

    AVFormatContext 结构体分析这个结构体描述了一个媒体文件或媒体流的构成和基本信息.这是FFMpeg中最为基本的一个结构,是其他所有结构的根,是一个多媒体文件或流的根本抽象.主要成员释义: ...

  4. LeetCode: Subsets 解题报告

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  5. 小米手机安装mitmproxy证书

    [本文出自天外归云的博客园] 问题描述 小米手机在连接mitmproxy代理后通过浏览器访问mitm.it下载android证书后无法成功安装证书 设备:Redmi Note 2(红米手机) 解决方法 ...

  6. 【javascript】console 让 js 调试更简单

    浏览器的控制台(console)是最重要的面板,主要作用是显示网页加载过程中产生各类信息. 显示信息 console.log('hello world'); console.debug('debug' ...

  7. 有趣的JavaScript原生数组函数

    本文由 伯乐在线 - yanhaijing 翻译.未经许可,禁止转载!英文出处:flippinawesome.欢迎加入翻译小组. 在JavaScript中,可以通过两种方式创建数组,Array构造函数 ...

  8. C# 验证给定的字符串形式的日期是否合法

    用于验证日期的有效性,对于用户输入的不规则日期也作了简单处理,比如用户输入了“今天”,则代码会认为用户要返回的是今天的日期,另外可以对纯数字的日期进行解析,比如:20130906 /// <su ...

  9. sublime text plugins

    Sublime Text 插件,HTML+CSS+JAVASCRIPT+JSON快速格式化:  htmlpretty 快捷键:Ctrl+Shift+H Essential Sublime Text 2 ...

  10. localhost兼容js不能用