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

题解:

#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的更多相关文章

  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. Gitbook 命令行工具

    1.Gitbook 简介 1.1 Gitbook GitBook 是一个基于 Node.js 开发的命令行工具,使用它可以很方便的管理电子书,GitBook 是目前最流行的开源书籍写作方案. 使用 G ...

  2. C#中DataTable删除多条数据

    //一般情况下我们会这么删除 DataTable dt = new DataTable(); for (int i = 0; i < dt.Rows.Count; i++) { if (99 % ...

  3. keras 文本分类 LSTM

    首先,对需要导入的库进行导入,读入数据后,用jieba来进行中文分词 # encoding: utf-8 #载入接下来分析用的库 import pandas as pd import numpy as ...

  4. loadrunner 关联匹配多个值

    loadrunner 关联获取从服务器返回相关值,如果需要把所有匹配的值都获取并且把这些值打印出来,怎么做呢? 1.首先要把把所有的匹配值都保存起来,需要在关联函数里面多传递一个参数:"Or ...

  5. win8使用技巧

    windows 8操作系统相信大家已经不再陌生了,虽然正式版本还未发布,但不少朋友已经在使用微软事先推出的windows 消费者预览版,直白的说就是公测版,预览版是免费的,但仅可以使用一年,但其功能与 ...

  6. [k8s]kube-dns/dashboard排错历险记(含sa加载用法/集群搭建)

    kube-dns原理 参考: 组件架构看这个就够了 http://cizixs.com/2017/04/11/kubernetes-intro-kube-dns 设置细节看这个就够了 http://b ...

  7. 【系统移植】uboot详细分析

    uboot使用 uboot控制台,倒计时    命令: 调试,操作一些硬件 setenv printenv saveenv  nand erase  nand write  tftp zImage h ...

  8. 清理configure脚本生成的文件

    今天在修改dovecot的代码时遇到一个问题,需要重新执行configure脚本,想先把之前configure生成的文件删除掉. 结果看了configure --help没有找到可以用的命令,最后搜了 ...

  9. 解剖 Elasticsearch 集群 - 之一

    解剖 Elasticsearch 集群 - 之一 本篇文章是一系列涵盖 Elasticsearch 底层架构和原型示例的其中一篇.在本篇文章中,我们会讨论底层的存储模型以及 CRUD(创建.读取.更新 ...

  10. 教你一招:[转载]使用 Easy Sysprep v4 封装 Windows 7 精品

    (一) 安装与备份系统 1. 安装 Windows 7 先使用第三方分区工具(DiskGenius分区)在虚拟机中分区,然后将封装的母盘文件安装写入指定的安装盘,写入完成后重启系统开始部署. 2. 进 ...