九度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,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- 2017 清北学堂 Day 6终极考试报告
预计分数: 100+70+70 = 240 实际假分数 : 40+80+70= 190 in cena(好吧不得不承认这个分数,,,,,,=.=) 实际真分数 : 100+80+100 = 280 ...
- 能挣钱的微信JSSDK+H5混合开发
H5喊了那么久,有些人都说不实用,有些人却利用在微信中开发H5应用赚得盆满钵满.微信JSSDK + HTML 5,让移动Web开发与微信结合轻而易举!跨平台.零成本,让大众创业变得更方便. 我觉得现在 ...
- MFC命令行及CCommandLineInfo类
获取命令行的方法: 1.GetCommandLine() 获取输入的所有信息,包括程序所在路径及参数 2.AfxGetApp()->m_lpCmdLine 只包含参数 一般情况下,获取到命令行后 ...
- [windows]桌面中添加我的电脑,我的文档和网上邻居图标
xp系统: 操作步骤:桌面任意位置--〉右键--〉属性--〉桌面选项卡--〉自定义桌面--〉常规:勾选相关图标确定即可. win7系统: 操作步骤:桌面任意位置--〉右键--〉个性化--〉(右侧)更改 ...
- jenkins+phantomjs环境搭建及使用
#jenkins+phantomjs 前端性能自动化测试的安装和使用#gcc GNU编译器套件 https://gcc.gnu.org/ #nginx 高性能的HTTP和反向代理服务器 http:// ...
- pb2.text_format.Merge(f.read(), self.solver_param) AttributeError: 'module' object has no attribute 'text_format'
http://blog.csdn.net/qq_33202928/article/details/72526710
- JAVA web项目转客户端(nativefier)
1.环境:windows 2.下载node.js 3.安装mode.js;记住安装目录 4.命令行进入安装目录 5.执行语句: npm install nativefier –g 进行安装 6.新建空 ...
- 获得Java中System对应一些属性值
public static void main(String[] args){ System.out.println("Java运行时环境版本:\n"+System.getProp ...
- javascript设计模式(张容铭) 第14章 超值午餐-组合模式 学习笔记
JS 组合模式更常用于创建表单上,比如注册页面可能有不同的表单提交模块.对于这些需求我们只需要有基本的个体,然后通过一定的组合即可实现,比如下面这个页面样式(如图14-2所示),我们来用组合模式实现. ...
- node爬虫(简版)
做node爬虫,首先像如何的去做这个爬虫,首先先想下思路,我这里要爬取一个页面的数据,要调取网页的数据,转换成页面格式(html+div)格式,然后提取里面独特的属性值,再把你提取的值,传送给你的页面 ...