题目描述:

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. Linux下环境搭建(一)——java、tomcat配置

    通过2个周末小憩的时间,终究是把linux环境下的jenkins+gitlab+jmeter框架给弄好了.jenkins的配置系列文章,可以翻看我以前的博文.此次,就将在linux下搭建环境的过程以博 ...

  2. vue+element ui项目总结点(四)零散细节概念巩固如vue父组件调用子组件的方法、拷贝数据、数组置空问题 等

    vue config下面的index.js配置host: '0.0.0.0',共享ip (假设你的电脑启动了这个服务我电脑一样可以启动)-------------------------------- ...

  3. SnowKiting 2017/1/24

    原文 Let's go fly a kite...in the snow Your snowkiting checklist To snowkite safely,you'll need a litt ...

  4. Freemarker入门小案例(生成静态网页的其中一种方式)

    其实生成静态网页的方式有好多种,我昨天看了一下,Freemarker是其中一种,但是Freemarker现在我们都用得比较少了,现在用得ActiveMQ用来发送信息到静态页面,不过想了一下这个小东西, ...

  5. vue父组件获取子组件页面的数组 以城市三级联动为例

    父组件调用子组件 <Cselect ref="registerAddress"></Cselect> import Cselect from '../../ ...

  6. 牛客网NOIP赛前集训营-提高组(第三场)A 管道维修

    https://www.nowcoder.com/acm/contest/174/A 这个的话  一个位置被清理的时间就是它到空白格子/边界的最短路对吧qww 然后求期望的话 假设它在第i步被清理掉的 ...

  7. python多进程与多线程编程

    进程(process)和线程(thread)是非常抽象的概念.多线程与多进程编程对于代码的并发执行,提升代码运行效率和缩短运行时间至关重要.下面介绍一下python的multiprocess和thre ...

  8. MySQL 上移/下移/置顶

    在编写网站系统时,难免会用到上移.下移.置顶的功能,今天小编就介绍一下我的思路. 首先,需要一张数据表: CREATE TABLE `a` ( `id` ) NOT NULL AUTO_INCREME ...

  9. golang 实现冒泡排序

    package main import ( "fmt" ) func main(){ a := [...] int{2,5,9,6,8} fmt.Println(a) num := ...

  10. docker系列之网络配置

    docker 网络配置 docker 安装后, 会自动在系统做一个网桥配置 docker0 . 其容器都会分配到此网桥配置下的独立, 私有 IP 地址. 如果你要自己配置桥接, 也可以把 docker ...