题目描述:

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. ef导航属性

    https://msdn.microsoft.com/en-us/data/jj574232.aspx  场景是   A表中有B,B表中又C.都是一堆多的关系.怎样Mapping是个问题啊.  var ...

  2. 【数据库-Azure SQL Database】SQL Server 如何将数据库备份到 Azure Storage

    打开本地的 SQL Server Management Studio.首先创建 Credentials.命令如下:   IF NOT EXISTS (SELECT * FROM sys.credent ...

  3. phantomas参数选项

    PhantomJS-based web performance metrics collector phantomas <url> [options] General options: - ...

  4. Fedora19添加和设置YUM源

    Fedora19添加和设置YUM源添加yum源前先安装fastestmirror/downloadonly插件和axelget插件: 1.安装fastestmirror/downloadonly插件 ...

  5. this+call、apply、bind的区别与使用

    http://www.ruanyifeng.com/blog/2018/06/javascript-this.html https://segmentfault.com/a/1190000018017 ...

  6. 读懂 Deployment YAML【转】

    既然要用 YAML 配置文件部署应用,现在就很有必要了解一下 Deployment 的配置格式,其他 Controller(比如 DaemonSet)非常类似. 还是以 nginx-deploymen ...

  7. Mybatis generator自动生成代码包括实体,dao,xml文件

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

  8. 苹果电脑macbook怎样强制关闭软件

    尝试快捷键Command+Q 选择当前处于界面最前端的应用,同时按住Command+Q退出程序,并不代表强制退出应用,主要用于一些假死的应用. 2 通过快捷键Command+option+Shift+ ...

  9. Cscope的使用(领略Vim + Cscope的强大魅力)

    文章出处:http://blog.csdn.net/dengxiayehu/article/details/6330200 Cscope的使用(领略Vim + Cscope的强大魅力) 1.Cscop ...

  10. 【贪心】bzoj1045: [HAOI2008] 糖果传递

    很妙的贪心思考过程 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数nn<=1'000'0 ...