【作业】用栈模拟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 + 5;
int dir[4][2] = { 1,0,0,1,-1,0,0,-1 };
struct node {
    int x, y;
    node(int x=0, int y=0) :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[15][15] = {
    { 0,0,0,0,0,0,0,0,0,0 },
    { 1,1,0,0,0,1,1,1,1,1 },
    { 1,1,1,0,0,1,1,1,1,1 },
    { 1,1,1,1,0,0,0,1,1,1 },
    { 1,1,1,1,1,0,1,1,1,1 },
    { 1,1,1,1,1,0,1,1,1,1 },
    { 1,1,1,1,1,0,0,0,0,1 },
    { 1,1,1,1,1,1,1,1,0,1 },
    { 1,1,1,0,1,1,1,1,0,1 },
    { 1,0,0,1,1,1,1,1,0,0 },
};

stack<prob> S, road;
int vis[1000][1000];
map<node, node>p;
int n, m;
bool ok(int x, int y) {
    if (mp[x][y] ==1 || x < 0 || x >= m || y < 0 || y >= n || vis[x][y])return 0;
    else return 1;
}
node nextpos(node n,int i) {
    return node(n.x + dir[i][0], n.y + dir[i][1]);
}
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=0;
    prob e= prob(nows, now, 0);
    do {
        if (ok(now.x, now.y)) {
            vis[now.x][now.y] = 1;
             e = prob(nows, now, 0);
            S.push(e);
            if (now== end)break;
            now = nextpos(now, 0);
            nows++;
        }
        else {
            if (!S.empty()) {
                e = S.top();
                S.pop();
                while (e.di == 3 && !S.empty()) {
                    vis[e.seat.x][e.seat.y] = 1;
                    e = S.top();
                    S.pop();
                }
                if (e.di < 3) {
                    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 + 5;
int dir[4][2] = { 1,0,0,1,-1,0,0,-1 };
struct node {
    int x, y;
    node(int x=0, int y=0) :x(x), y(y) {}
    bool operator < (const node &q) const { return  x < q.x; }
};
int mp[15][15] = {
    { 0,0,0,0,0,0,0,0,0,0 },
    { 1,1,0,0,0,1,1,1,1,1 },
    { 1,1,1,0,0,1,1,1,1,1 },
    { 1,1,1,1,0,0,0,1,1,1 },
    { 1,1,1,1,1,0,1,1,1,1 },
    { 1,1,1,1,1,0,1,1,1,1 },
    { 1,1,1,1,1,0,0,0,0,1 },
    { 1,1,1,1,1,1,1,1,0,1 },
    { 1,1,1,0,1,1,1,1,0,1 },
    { 1,0,0,1,1,1,1,1,0,0 },
};

stack<node> S, road;
int vis[1000][1000];
map<node, node>p;
int n, m;
bool illeg(int x, int y) {
    if (mp[x][y] == ‘1‘ || x < 0 || x >= m || y < 0 || y >= n || vis[x][y])return 1;
    else return 0;
}

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=0;i<4;i++){
                int dx = now.x + dir[i][0]; int dy = now.y + dir[i][1];
                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] = 1;
            }
        /*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;
}

原文地址:https://www.cnblogs.com/SuuT/p/8893195.html

时间: 2024-10-30 01:07:06

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

Code POJ - 1780(栈模拟dfs)

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

HDU ACM 4041 Eliminate Witches! 队列和栈模拟DFS

分析:直接模拟即可,这里用队列记录访问过的点,栈记录父节点.另外要注意的是在strlen(str)计算字符串的时候要预先计算出来保存在变量中,for直接用,如果for循环直接调用strlen,那么每次都会重新计算,该題字符串的数据量很大,就会存在大量的无用计算,还导致了一次TLE,唉!以前没注意到这里. #include<iostream> #include<vector> #include<queue> #include<stack> using name

hdu1515 dfs栈模拟

Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1513    Accepted Submission(s): 690 Problem Description How can anagrams result from sequences of stack operations? There are tw

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

将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main() {while(在图中能找到未访问过的点 k) Dfs(k);} 例题: POJ1164 The Castle Description 1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#-

用两个栈模拟实现一个队列

题目:如何用两个栈模拟实现一个队列?  如果这两个堆栈的容量分别是m和n(m>n),你的方法能保证的队列容量是多少?(这里讨论的是顺序栈,如果是链式栈的话完全没有必要考虑空间) 分析:栈的特点是“后进先出(LIFO)”,而队列的特点是“先进先出(FIFO)”.用两个栈模拟实现一个队列的基本思路是:用一个栈作为存储空间,另一个栈作为输出缓冲区,把元素按顺序压入两栈(模拟的队列),并按此顺序出队并输出即可. 如下图,用容量为m的栈作为存储空间,容量为n的栈作为输出缓冲区,一开始先将n个元素压入(pu

ACM学习历程——UVA 127 &quot;Accordian&quot; Patience(栈;模拟)

Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patience, the rules for which are as follows: Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate n

HDOJ 4699 Editor 栈 模拟

用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1913    Accepted Submission(s): 591 Problem Description Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2 Sample Output 2 3 Hint The fol

web前端面试系列 - 数据结构(两个栈模拟一个队列)

一. 用两个栈模拟一个队列 思路一: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时将数据压人s1 3. 出队时将s1弹出,并压人s2,然后弹出s2中的顶部数据,最后再将剩余数据弹出s2,并压人s1. 思路二: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时,判断s1, 是否为空,如果不为空,则将数据直接压入s1, 如果为空,则将s2中的数据全部倒入s1,在将数据压人s1. 3. 出队时,判断s2, 是否为空,如果不为空,则直接弹出s2

自定义栈的实现及使用两个栈模拟队列

一,使用单链表实现栈 ①栈需要一个栈顶指针 ②栈的基本操作有出栈和入栈,以及判断栈是否为空 ③单链表中每个结点表示一个栈元素,每个结点有指向下一个结点的指针.因此,在栈内部需要实现一个单链表.代码如下: public class Stack<T extends Comparable<? super T>>{ private class Node{ T ele; Node next; public Node(T ele) { this.ele = ele; } } Node top;