题目描述:

sun所在学校每年都要举行电脑节,今年电脑节有一个新的趣味比赛项目叫做闯迷宫。
sun的室友在帮电脑节设计迷宫,所以室友就请sun帮忙计算下走出迷宫的最少步数。
知道了最少步数就可以辅助控制比赛难度以及去掉一些没有路径到达终点的map。
比赛规则是:从原点(0,0)开始走到终点(n-1,n-1),只能上下左右4个方向走,只能在给定的矩阵里走。

输入:

输入有多组数据。
每组数据输入n(0<n<=100),然后输入n*n的01矩阵,0代表该格子没有障碍,为1表示有障碍物。
注意:如果输入中的原点和终点为1则这个迷宫是不可达的。

输出:

对每组输入输出该迷宫的最短步数,若不能到达则输出-1。

样例输入:
2
0 1
0 0
5
0 0 0 0 0
1 0 1 0 1
0 0 0 0 0
0 1 1 1 0
1 0 1 0 0
样例输出:
2
8 开始用dfs做的,结果超时了
代码如下:
 #include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define MAX 102
#define inf 100002 int dir[][] = {{,},{,-},{-,},{,}};
int map[MAX][MAX];
int flag[MAX][MAX];
int n;
int min; void dfs(int x, int y, int cnt) {
if(x == n- && y == n-) {
if(min > cnt) {
min = cnt;
}
return;
}
if(cnt > min) {
return;
}
for(int i = ; i < ; i++) {
int tempx = x + dir[i][];
int tempy = y + dir[i][];
if(tempx >= && tempx < n && tempy >= && tempy < n && flag[tempx][tempy] == &&map[tempx][tempy] == ) {
flag[tempx][tempy] = ;
dfs(tempx, tempy, cnt+);
flag[tempx][tempy] = ;
}
}
} int main(int argc, char const *argv[])
{
freopen("input.txt","r",stdin);
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; i++) {
for(int j = ; j < n; j++) {
scanf("%d",&map[i][j]);
}
}
if(map[][] == || map[n-][n-] == ) {
puts("-1");
continue;
}
min = inf;
memset(flag,,sizeof(flag));
dfs(, , );
if(min != inf) {
printf("%d\n", min);
}
else {
puts("-1");
} }
return ;
}

后来改成bfs,代码如下

 #include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAX 102
#define inf 100002 using namespace std; int dir[][] = {{,},{,-},{-,},{,}};
int map[MAX][MAX];
int cnt[MAX][MAX];
int n;
int ans; struct Point{
int x;
int y;
}; queue <Point> temp; void bfs() {
Point source;
source.x = , source.y = ;
cnt[][] = ;
temp.push(source);
while(!temp.empty()) {
Point tmp = temp.front();
temp.pop();
for(int i = ; i < ; i++) {
int tempx = tmp.x + dir[i][];
int tempy = tmp.y + dir[i][];
if(tempx >= && tempx < n && tempy >= && tempy < n && map[tempx][tempy] == ) {
int tcnt = cnt[tmp.x][tmp.y] + ;
if(tcnt < cnt[tempx][tempy]) {
Point p;
p.x = tempx;
p.y = tempy;
cnt[tempx][tempy] = tcnt;
temp.push(p);
}
}
}
}
} int main(int argc, char const *argv[])
{
//freopen("input.txt","r",stdin);
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; i++) {
for(int j = ; j < n; j++) {
scanf("%d",&map[i][j]);
cnt[i][j] = inf;
}
}
if(map[][] == || map[n-][n-] == ) {
puts("-1");
continue;
}
bfs();
ans = cnt[n-][n-];
if(ans != inf) {
printf("%d\n", ans);
}
else {
puts("-1");
} }
return ;
}

竟然一次通过,不错不错

九度oj 题目1335:闯迷宫的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

随机推荐

  1. 关于HTML5中Video标签无法播放mp4的解决办法

    1.首先先排除掉代码问题.路径问题.浏览器不支持问题等常规问题,这些问题另行百度. <video width="500px" height="300px" ...

  2. UESTC cdoj 619 吴神,人类的希望 (组合数学)

    枚举盒子的个数,先把总数n减去掉box*k保证每个盒子至少有k个小球,剩下的小球放入盒子中可以为空, 加入box个小球保证每个盒子至少有一个小球,问题转化成不可区分小球放入不可区分盒子非空的方案数. ...

  3. 图像处理框架 Core Image 介绍

    这篇文章会为初学者介绍一下 Core Image,一个 OS X 和 iOS 的图像处理框架. 如果你想跟着本文中的代码学习,你可以在 GitHub 上下载示例工程.示例工程是一个 iOS 应用程序, ...

  4. core 下使用 autofac

    依赖注入小伙伴们比较常了,这里只说core 下autofac依赖注入的使用 ,不多费话,直接代码. 在 Startup.cs里 public void ConfigureServices(IServi ...

  5. Airbnb:别抵制我,宝宝要过 10 岁生日

    今日导读 喜欢旅游的你,一定听说或使用过 airbnb(爱彼迎),在出发前打开它,总是能通过它开始一段奇妙的旅行.可是最近,就在这个打着“共享家”概念的服务型网站正迎来十周年之际,它却遭到了很多国家的 ...

  6. OAuth授权之回调accessToken

    具体说明见新浪官方文档 http://open.weibo.com/wiki/Oauth2/access_token   具体实现 第一步 打开回调页面 // 宏定义client_id #define ...

  7. CSS规范(OOCSS SMACSS BEM)

    Css规范 OOCSS SMACSS BEM OOCSS(Object Oriented CSS)面向对象的css 主要分成四个部分 Template :模板 Grids :栅格布局 Module : ...

  8. 下载PhantomJS

    PhantomJS新手?阅读并学习快速入门指南. 视窗 下载phantomjs-2.1.1-windows.zip(17.4 MB)并解压缩(解压缩)内容. 可执行文件phantomjs.exe已准备 ...

  9. 使用dmidecode在Linux下获取硬件信息

    dmidecode命令可以让你在Linux系统下获取有关硬件方面的信息.dmidecode的作用是将DMI数据库中的信息解码,以可读的文本方式显示.由于DMI信息可以人为修改,因此里面的信息不一定是系 ...

  10. 【git】不检查特定文件的更改情况

    .gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的.正确的做法是在每个clone下来的仓库中手动设置不要检查特定文件的更 ...