【作业】用栈模拟dfs
题意:一个迷宫,起点到终点的路径,不用递归。
题解:
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<stdlib.h>
- #include<string>
- #include<string.h>
- #include<stack>
- #include<iostream>
- #include<map>
- using namespace std;
- const int maxn = 1e5 + ;
- int dir[][] = { ,,,,-,,,- };
- struct node {
- int x, y;
- node(int x=, int y=) :x(x), y(y) {}
- bool operator < (const node &q) const { return x < q.x; }
- bool operator ==(const node &a)const { return x == a.x&&y == a.y; }
- };
- struct prob {
- int ord;
- node seat;
- int di;
- prob(int x , node y, int z ) :ord(x), seat(y), di(z) {}
- };
- int mp[][] = {
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- };
- stack<prob> S, road;
- int vis[][];
- map<node, node>p;
- int n, m;
- bool ok(int x, int y) {
- if (mp[x][y] == || x < || x >= m || y < || y >= n || vis[x][y])return ;
- else return ;
- }
- node nextpos(node n,int i) {
- return node(n.x + dir[i][], n.y + dir[i][]);
- }
- int main() {
- cin >> n >> m;
- int sr, sc; cin >> sr >> sc;
- int er, ec; cin >> er >> ec;
- node end = node(er, ec);
- node start = node(sr, sc);
- //S.push(0,node(sr, sc),0);
- node now=start;
- int nows=;
- prob e= prob(nows, now, );
- do {
- if (ok(now.x, now.y)) {
- vis[now.x][now.y] = ;
- e = prob(nows, now, );
- S.push(e);
- if (now== end)break;
- now = nextpos(now, );
- nows++;
- }
- else {
- if (!S.empty()) {
- e = S.top();
- S.pop();
- while (e.di == && !S.empty()) {
- vis[e.seat.x][e.seat.y] = ;
- e = S.top();
- S.pop();
- }
- if (e.di < ) {
- e.di++; S.push(e);
- now = nextpos(now, e.di);
- }///
- }
- }
- } while (!S.empty());
- stack<prob>ans;
- while (!(S.empty()) ){
- ans.push(S.top());
- S.pop();
- }
- while (!(ans.empty())) {
- cout << ans.top().seat.x << ' ' << ans.top().seat.y << endl;
- ans.pop();
- }
- cin >> n;
- }
附:之前模仿bfs写的,不知道怎么存路径。。
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<stdlib.h>
- #include<string>
- #include<string.h>
- #include<stack>
- #include<iostream>
- #include<map>
- using namespace std;
- const int maxn = 1e5 + ;
- int dir[][] = { ,,,,-,,,- };
- struct node {
- int x, y;
- node(int x=, int y=) :x(x), y(y) {}
- bool operator < (const node &q) const { return x < q.x; }
- };
- int mp[][] = {
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- { ,,,,,,,,, },
- };
- stack<node> S, road;
- int vis[][];
- map<node, node>p;
- int n, m;
- bool illeg(int x, int y) {
- if (mp[x][y] == '' || x < || x >= m || y < || y >= n || vis[x][y])return ;
- else return ;
- }
- int main() {
- cin >> n >> m;
- int sr, sc; cin >> sr >> sc;
- int er, ec; cin >> er >> ec;
- S.push(node(sr, sc));
- while (!S.empty()) {
- node now = S.top(); S.pop();
- road.push(now);
- //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;
- //S.push(now);
- if (now.x == er&&now.y == ec) break;
- for(int i=;i<;i++){
- int dx = now.x + dir[i][]; int dy = now.y + dir[i][];
- if(illeg(dx,dy))continue;
- if (vis[dx][dy])continue;
- S.push(node(dx, dy));
- node x = node(dx, dy);
- p[x] = now;
- vis[dx][dy] = ;
- }
- /*for (int i = 0; i < m; i++){
- for (int j = 0; j < n; j++) {
- cout << vis[i][j];
- }
- cout<<endl;
- }
- cout << endl;*/
- }
- node now=node(er,ec);
- node x = node(sr, sc);
- while (!(now.x==sr&&now.y==sc) ){
- cout << now.x << ' ' << now.y << endl;
- now = p[now];
- }
- cin >> n;
- }
【作业】用栈模拟dfs的更多相关文章
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- Code POJ - 1780(栈模拟dfs)
题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...
- 【栈模拟dfs】Cells UVALive - 3486
题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...
- LA 3486 Cells(判祖先+栈模拟dfs)
https://vjudge.net/problem/UVALive-3486 题意: 判断u是否是v的祖先. 思路: 很简单,dfs遍历,记录每个节点第一次访问时的时间戳 in[i] 和第二次访问时 ...
- 深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)
将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...
- 百炼3752:走迷宫--栈实现dfs
3752:走迷宫 总时间限制: 1000ms 内存限制: 65536kB 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走.给定一个迷宫,求从左上角走到右下角最 ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- UVALive 7454 Parentheses (栈+模拟)
Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...
- poj1363Rails(栈模拟)
主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...
随机推荐
- 100个MySQL 的调节和优化的提示
100个MySQL 的调节和优化的提示 MySQL是一个功能强大的开源数据库.随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它的极限.这里是101条调节和优化MySQL安装的技巧. ...
- 在SharePoint 2013 场中移除服务器,提示 cacheHostInfo is null 错误
Problem 在SharePoint 2013 场中移除服务器,提示 cacheHostInfo is null 错误 Resolution 这是由于SharePoint 2013中分布式缓存实例( ...
- FFmpeg: AVFormatContext 结构体分析
AVFormatContext 结构体分析这个结构体描述了一个媒体文件或媒体流的构成和基本信息.这是FFMpeg中最为基本的一个结构,是其他所有结构的根,是一个多媒体文件或流的根本抽象.主要成员释义: ...
- LeetCode: Subsets 解题报告
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 小米手机安装mitmproxy证书
[本文出自天外归云的博客园] 问题描述 小米手机在连接mitmproxy代理后通过浏览器访问mitm.it下载android证书后无法成功安装证书 设备:Redmi Note 2(红米手机) 解决方法 ...
- 【javascript】console 让 js 调试更简单
浏览器的控制台(console)是最重要的面板,主要作用是显示网页加载过程中产生各类信息. 显示信息 console.log('hello world'); console.debug('debug' ...
- 有趣的JavaScript原生数组函数
本文由 伯乐在线 - yanhaijing 翻译.未经许可,禁止转载!英文出处:flippinawesome.欢迎加入翻译小组. 在JavaScript中,可以通过两种方式创建数组,Array构造函数 ...
- C# 验证给定的字符串形式的日期是否合法
用于验证日期的有效性,对于用户输入的不规则日期也作了简单处理,比如用户输入了“今天”,则代码会认为用户要返回的是今天的日期,另外可以对纯数字的日期进行解析,比如:20130906 /// <su ...
- sublime text plugins
Sublime Text 插件,HTML+CSS+JAVASCRIPT+JSON快速格式化: htmlpretty 快捷键:Ctrl+Shift+H Essential Sublime Text 2 ...
- localhost兼容js不能用