题目链接:http://poj.org/problem?id=3984

Description

定义一个二维数组:

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

题解:

裸的BFS水题。

AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
const int dx[]={,,,-};
const int dy[]={,,-,}; int mp[][];
struct Node{
int x,y;
int cnt;
Node(){}
Node(int _x,int _y,int _cnt) {
x=_x, y=_y, cnt=_cnt;
}
inline bool out() {
return x< || x>= || y< || y>=;
}
};
queue<Node> Q;
bool vis[][];
Node pre[][];
stack<Node> ans; int main()
{
memset(mp,,sizeof(mp));
for(int i=;i<;i++) for(int j=;j<;j++) scanf("%d",&mp[i][j]); memset(vis,,sizeof(vis));
Q.push((Node){,,});
vis[][]=;
while(!Q.empty())
{
Node now=Q.front(); Q.pop();
if(now.x== && now.y==)
{
ans.push(now);
break;
}
for(int k=;k<;k++)
{
Node nxt=Node(now.x+dx[k],now.y+dy[k],now.cnt+);
if(nxt.out()) continue;
if(mp[nxt.x][nxt.y]) continue;
if(vis[nxt.x][nxt.y]) continue;
Q.push(nxt);
vis[nxt.x][nxt.y]=;
pre[nxt.x][nxt.y]=now;
}
} while(ans.top().cnt) ans.push(pre[ans.top().x][ans.top().y]);
while(!ans.empty())
{
printf("(%d, %d)\n",ans.top().x,ans.top().y);
ans.pop();
}
}

最新文章

  1. 返水bug-备用
  2. js实现手机号码和身份证号码校验
  3. thinkphp自动验证方法的使用
  4. Deep Learning: Assuming a deep neural network is properly regulated, can adding more layers actually make the performance degrade?
  5. WPF-控件-将ListBox条目水平排列
  6. spark二次排序
  7. 新学期的第一节Android课
  8. SEO的基本概念 和 提交SITEMAP到搜索引擎
  9. Linux下用户和组管理
  10. .NET ClrProfiler ILRewrite 商业级APM原理
  11. vue 组件传值
  12. springboot+spring security +oauth2.0 demo搭建(password模式)(认证授权端与资源服务端分离的形式)
  13. monent API详解
  14. [BZOJ3295] [Cqoi2011]动态逆序对(带修改主席树)
  15. 洛谷 P4656: LOJ 2484: [CEOI2017]Palindromic Partitions
  16. ApiPost的环境变量的定义和使用「ApiPost环境变量」
  17. DCHP是什么意思
  18. 30_数据库_第30天java_jdbc_(DBUtils)_讲义
  19. C#高级编程9-第1章.NET体系结构
  20. 细节取胜的javadoc

热门文章

  1. 每天一个linux命令:top
  2. Docker Mysql数据库双主同步配置方法
  3. Unix环境高级编程-阻塞访问原理——等待队列
  4. 【20170506】贝业新兄弟IT总监李济宏:第三方家居物流的IT架构探索
  5. 使用PHPExcel实现Excel文件的导入和导出(模板导出)
  6. jQuery - Detect value change on hidden input field
  7. pandas DataFrame(5)-合并DataFrame与Series
  8. (转)常用的 TCP KeepAlive 参数
  9. Spring钩子方法和钩子接口的使用详解
  10. Linux shell去除字符串中所有空格