作业用栈模拟dfs
Posted COLORFUL
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了作业用栈模拟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; }
以上是关于作业用栈模拟dfs的主要内容,如果未能解决你的问题,请参考以下文章