九度oj 题目1335:闯迷宫
- 题目描述:
-
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:闯迷宫的更多相关文章
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- 浅谈Hibernate中的三种数据状态
Hibernate中的三种数据状态:临时.持久.游离 1.临时态(瞬时态) 不存在于session中,也不存在于数据库中的数据,被称为临时态. 数据库中没有数据与之对应,超过作用域会被JVM垃圾回收器 ...
- LOJ#111. 后缀排序(二分 hash)
题意 给出一个字符串,求出排名为$i$个字符串在原串中的开始位置 Sol 纪念一下这伟大的时刻qwq. 我用二分+hash把这题水过去了qwq. #include<cstdio> #inc ...
- 制作 Favicon.ico 图标教程
摘要:有一些站长认为 favicon.ico 图标对于一个网站没有什么作用.甚至有一些站长认为,少加载一个图片,页面设计展现的速度更快些.的确,理论上是对的,但乐猪认为这种想法是肤浅的!有这种想法的站 ...
- PeopleSoft FSCM Production Support 案例分析
PeopleSoft FSCM Production Support 案例分析 2010年的时候曾建言博客园开辟Oracle ERP模块供大家交流,博客园如约开辟Oracle ERP 模块,而我后来却 ...
- ios has denied the launch request.
ios has denied the launch request. You can choose either of the two ways. Solution 1: Open System Pr ...
- android 日期 时间
/** * 给定一个日期型字符串,返回加减n天后的日期型字符串 * * @param basicDate * @param nDays * @return */ public static Strin ...
- Android项目中包名的修改
通常修改包名时会造成R文件错误,并且有时带有原因不明的Manifest文件中多处文本混乱. 所以,将目前认为最为简洁方便的修改包名流程记录如下: 假设我们目前的包名为com.pepper.util,我 ...
- LR中下载文件的脚本
#include "web_api.h" Action(){ int iflen; //文件大小 long lfbody; //响应数据内容大小 web_url("xxx ...
- 后台安装 SQL Server 无人值守 安装
(开头闲淡)项目需要必须安装SQL的,查了很久,断断续续用了各种方法,今天终于用了正确的姿(xia)势(mo)弄成了. 最开始用的方法是调用Win的API模拟鼠标操作安装的,嗯,虽然勉强可以,就是有些 ...
- 使用python查询天气
python主代码 weather.py import urllib2 import json from city import city cityname = raw_input('你想查哪个城市的 ...