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

题解:

#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;
}

最新文章

  1. gdb调试PHP扩展错误
  2. easyui-datagrid 列单击事件
  3. java之线程
  4. Height Half Values
  5. 【Java EE 学习 21 上】【其它类型的监听器】【使用HttpSessionActivationListener监听session的活化和钝化】
  6. javabean实体类对象转为Map类型对象的方法(转发)
  7. hdu 4712
  8. VMware Network Adapter VMnet1和VMnet8 未识别的网络的解决方法
  9. 【android】下载文件至本应用程序的file文件夹或者sdcard
  10. [Android笔记1]Activity+Layout+Button
  11. Ubuntu16.04卸载opencv2.4.9并安装opencv3.2.0+contrib
  12. css垂直居中方法总结
  13. JavaScript第一回-来龙去脉
  14. SkylineGlobe 7.0.1 &amp; 7.0.2版本Web开发 如何实现对三维模型和地形的剖切展示
  15. python3 十六进制字符串进行分割并累加
  16. luoguP1850 换教室
  17. Ajax传递json数据简介和一个需要注意的小问题
  18. 【译】第22节---Fluent API - EntityTypeConfiguration类
  19. Sticks HDU - 1455 (未完成)
  20. Linux&#160;CentOS下Python+robot&#160;framework环境搭建

热门文章

  1. CorelCAD for Mac(绘图设计软件)破解版安装
  2. WEBAPI 的简单示例
  3. 4.翻译系列:EF 6 Code-First默认约定(EF 6 Code-First系列)
  4. KMP算法——从入门到懵逼到了解
  5. IOS项目目录结构和开发流程
  6. android studio build.gradle 中的dependencies 的 compile jar文件
  7. pandas数组(pandas Series)-(5)apply方法自定义函数
  8. C#学习笔记(31)——委托窗体传值
  9. 随机获取一个集合(List, Set,Map)中的元素&lt;转&gt;
  10. github新建repositories后import已有code 随后同步更新