【作业】用栈模拟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;
}
上一篇:java maven项目 pom.xml plugin 报错, build path 找不到 jconsole-1.8.0.jar 和 tools-1.8.0.jar 包


下一篇:CSS元素 之 float