Maze

Description

 

You are given a special Maze described as an n*m matrix, please find the shortest path to reach (n,m) from (1,1);

 

Input

 

The first line of the input is an integer T (T <= 100), which stands for the number of test cases you need to solve.

Each test case begins with two integers n, m (1 <= n, m <= 250) in the first line indicating the size of the board. Then n lines follow, each line contains m numbers either 0 or 1 which are:
0 : represents a grid on which you can step.
1 : represents a wall.

 

Output

 

For every test case, you should output "Case #k: " first, where k indicates the case number and starts at 1. If it’s impossible to reach the end position, just output “-1”. Otherwise, output the minimum number of steps to solve this problem.

 

Sample Input

 

1
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

 

Sample Output

Case #1: 8

这题据说是2011 UESTC Training for Search Algorithm——A,不过我找不到原题,所以不知道自己写的能不能过。觉得该题是最好的BFS入门题啦。

这是我第一次写BFS,纪念一下^_^

题目意思就是找出从点(1, 1) 到 点(n, m) 的距离最短是多少。注意格子是 0 的点才能走,1代表墙,是不能走的。

因为BFS 是逐层拓展的,不像DFS 一直往深处找,所以能保证当达到点(n, m) 时即找到最短的距离。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std; const int maxn = + ;
struct node
{
int x;
int y;
int step;
};
int maze[maxn][maxn];
int dx[] = {, , , -};
int dy[] = {, , -, }; int main()
{
int T, n, m;
while (scanf("%d", &T) != EOF)
{
for (int cas = ; cas <= T; cas++)
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
scanf("%d", &maze[i][j]);
}
int ans = ;
queue<node> q;
q.push({, , }); // 代表从坐标点(1, 1)出发
while (!q.empty())
{
node cur = q.front(); // 取出队首元素
q.pop();
int cur_x = cur.x;
int cur_y = cur.y;
if (cur_x == n && cur_y == m) // 到达目标节点
{
ans = cur.step; // 得到最短步数
break;
}
for (int i = ; i < ; i++)
{
int tx = cur_x + dx[i];
int ty = cur_y + dy[i];
if (tx >= && tx <= n && ty >= && ty <= m && !maze[tx][ty]) // 可以走且没有走过
{
maze[tx][ty] = ;
q.push({tx, ty, cur.step+});
}
}
}
printf("Case #%d: %d\n", cas, ans);
}
}
return ;
}

Maze 解题报告的更多相关文章

  1. 【LeetCode】490. The Maze 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  2. 2020.6.16 night 解题报告

    2020.6.16 night 解题报告 link 标签(空格分隔): 题解 概率与期望 T1 : Crossing Rivers UVA - 12230 SB题. 很唬人的一个连续期望. 很明显,在 ...

  3. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  4. 二模13day1解题报告

    二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...

  5. BZOJ 1051 最受欢迎的牛 解题报告

    题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4438  Solved: 2353[S ...

  6. 习题:codevs 2822 爱在心中 解题报告

    这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...

  7. 习题:codevs 1035 火车停留解题报告

    本蒟蒻又来写解题报告了.这次的题目是codevs 1035 火车停留. 题目大意就是给m个火车的到达时间.停留时间和车载货物的价值,车站有n个车道,而火车停留一次车站就会从车载货物价值中获得1%的利润 ...

  8. 习题: codevs 2492 上帝造题的七分钟2 解题报告

    这道题是受到大犇MagHSK的启发我才得以想出来的,蒟蒻觉得自己的代码跟MagHSK大犇的代码完全比不上,所以这里蒟蒻就套用了MagHSK大犇的代码(大家可以关注下我的博客,友情链接就是大犇MagHS ...

  9. 习题:codevs 1519 过路费 解题报告

    今天拿了这道题目练练手,感觉自己代码能力又增强了不少: 我的思路跟别人可能不一样. 首先我们很容易就能看出,我们需要的边就是最小生成树算法kruskal算法求出来的边,其余的边都可以删掉,于是就有了这 ...

随机推荐

  1. DP的序--Codeforces626F. Group Projects

    $n \leq 200$个数,$ \leq 500$,$K \leq 1000$代价内的数字分组有多少?一个分组的代价是分成的每个小组的总代价:一个小组的代价是极差. 问的极差那就从极入手嘛.一个小组 ...

  2. Python入门--8--字符串

    一.创建.修改字符串 str1='呆呆 槑槑 木木 林林' str1[1] #输出呆 str1[2] #输出' ',也就是空值 str1=str[:5]+'插入乖呆 '+str1[5:] #修改字符串 ...

  3. Netty构建游戏服务器(一)--基本概念与原理

    一,Netty是什么 1,Netty是由 JBOSS提供的一个 java开源框架. 2,Netty是JAR包,一般使用ALL-IN-ONE的JAR包就可以开发了. 3,Netty不需要运行在Tomca ...

  4. HDU 5676 ztr loves lucky numbers【DFS】

    题目链接; http://acm.hdu.edu.cn/showproblem.php?pid=5676 题意: 由4和7组成的且4和7出现次数相同的数称为幸运数字,给定n,求不大于n的最大幸运数字. ...

  5. jfinal使用idea启动 访问报404 action not found

    公司一个项目,在eclipse里面启动正常,换到idea里面启动后,启动没有报错,但是访问的时候会提示404 action not found. 百度了很多种解决方法 都没有解决. 今天脑子一转,想到 ...

  6. JavaSE的static、final、abstract修饰符

    static :静态常量,静态方法,静态代码块     静态变量:  静态变量属于类的,使用类名来访问,非静态变量是属于对象的,"必须"使用对象来访问.           注意: ...

  7. 9.Java web—JSP内置对象

    容器内置了9大对象,这些对象在jsp页无需实例化,可以直接使用. 分别为request. response .session. application .out. pageContext .confi ...

  8. ,java反射机制实现拦截器

    实现一个拦截器必须要实现一下几个类: 1 目标类接口:目标类要实现的接口. package com.lanvis.reflect; public interface ITarget {     pub ...

  9. 【Todo】ssh的原理和实践

    有空的时候补充,可以参考 http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html http://www.ruanyifeng.com/ ...

  10. 梯度下降和EM算法,kmeans的em推导

    I. 牛顿迭代法给定一个复杂的非线性函数f(x),希望求它的最小值,我们一般可以这样做,假定它足够光滑,那么它的最小值也就是它的极小值点,满足f′(x0)=0,然后可以转化为求方程f′(x)=0的根了 ...