【作业】用栈模拟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指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...
随机推荐
- Git 安装(分布式版本控制系统)
1.在 Windows 上安装 在 Windows 上安装 Git 也有几种安装方法. 官方版本可以在 Git 官方网站下载,打开下载会自动开始.要注意这是一个名为 Git for Windows 的 ...
- nginx做负载均衡时其中一台服务器挂掉宕机时响应速度慢的问题解决
nginx会根据预先设置的权重转发请求, 若给某一台服务器转发请求时,达到默认超时时间未响应,则再向另一台服务器转发请求. 默认超时时间1分钟. 修改默认超时时间为1s: server { liste ...
- Atitit 计算word ppt文档的页数
Atitit 计算word ppt文档的页数 http://localhost:8888/ http://git.oschina.net/attilax/ati_wordutil private vo ...
- android 推流解决方案
.LocalSocket + MediaRecorder + librtmp
- 菜鸟教程之工具使用(五)——JRebel与Windows服务的Tomcat集成
之前写过一篇Tomcat借助JRebel支持热部署的文章——<借助JRebel使Tomcat支持热部署>.介绍的是在开发.测试环境中的配置,但是正式的部署环境,我们不会通过命令行来启动To ...
- MXNET:监督学习
线性回归 给定一个数据点集合 X 和对应的目标值 y,线性模型的目标就是找到一条使用向量 w 和位移 b 描述的线,来尽可能地近似每个样本X[i] 和 y[i]. 数学公式表示为\(\hat{y}=X ...
- 【iCore4 双核心板】4.3寸液晶模块程序发布
一.说明 1.本资料包程序包含两部分,"CAPTURE"为液晶显示截图,"tft4.3"为ARM程序. 2.此程序只适合iCore4液晶模块. 3.iCore4 ...
- 如何解决安装VMware后郑广电宽带客户端不能登录的问题?
如何解决安装VMware后郑广电宽带客户端不能登录的问题? 问题:安装VMware后,郑广电宽带客户端不能登录,提示:“不允许代理上网”. 解决:将VMware的虚拟网卡(VMnet1和VMnet8) ...
- python进行数据分析
1. python进行数据分析----线性回归 2. python进行数据分析------相关分析 3. python进行数据分析---python3卡方 4. 多重响应分析,多选题二分法思路 5. ...
- centos7 修改root密码方式
1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/bin/sh 4 - 现在按下 Co ...