【作业】用栈模拟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指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...
随机推荐
- SpringCloud服务间调用
SpringCloud服务间的调用有两种方式:RestTemplate和FeignClient.不管是什么方式,他都是通过REST接口调用服务的http接口,参数和结果默认都是通过jackson序列化 ...
- 【转】AlphaGo Zero 和强人工智能
AlphaGo Zero 和强人工智能 前段时间比较热门的是 AlphaGo(阿法狗)的升级版:AlphaGo Zero(阿法狗零).跟阿法狗不同,阿法狗零不依赖于任何人类对弈记录,完全从围棋的规则出 ...
- Spark 准备篇-环境搭建
本章内容: 待整理 参考文献: 学习Spark——环境搭建(Mac版) <深入理解SPARK:核心思想与源码分析>(前言及第1章) 搭建Spark源码研读和代码调试的开发环境 Readin ...
- 物联网架构成长之路(15)-Jenkins部署SpringBoot
1.前言 现在慢慢也在负责一些工作了.这段时间有空,就多了解了解软件多人开发的一些知识.以前项目都是我一个人做的,从数据库设计到后端再到前端,全部放在一个war包丢到tomcat里面然后运行,利用to ...
- vue模板的讲解
1.项目目录 2.入口文件index.html 3.index.html默认调用的main.js 3.调用的组件app.vue 4.调用组件hello.vue
- 如何在TextView类中创建超链接 Linkify
Linkify是一个辅助类,通过RegEx样式匹配,自动地在TextView类(和继承的类)中创建超链接.符合特定的RegEx样式的文本会被转变成可点击的超链接,这些超链接隐式的调用startActi ...
- linux每日命令(28):chgrp命令
在linux系统里,文件或目录的权限的掌控以拥有者及所属群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change group的 ...
- 使用Fidder将生成环境代码映射到本地(文件夹)
匹配生产URL正则表达式 regex:http://www.a.com/statics/js/(.*) 本地路径配置 D:\web\statics\js\$1
- Java反射,参数为数组
使用反射调用非公开的方法有时能解决许多问题,如果方法的参数是数组时类型该怎么传递呢?这里找到了一种方法记录一下 实例 比如: class A{ private void sayHello(String ...
- NoSuchMethodError: ... addOnCompleteCallback
问题描述: 使用ES 2.3.1和Spark streaming 2.1时,出现以上报错信息. 原因: addOnCompleteCallback方法在spark2.0中移除了 The addOnCo ...