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. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  2. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

  3. CentOS 7 使用iptables 开放端口

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service system ...

  4. linux 安裝jdk

    參考博客:http://www.cnblogs.com/wuqianling/p/5381895.html http://www.cnblogs.com/CuteNet/p/3947193.html ...

  5. 同时在windows和linux环境开发时换行符的处理

    Git 的 core.autocrlf 參數默认为true,即每次 checkin 時,Git 會將純文字類型的檔案中的所有 CRLF 字元轉換為 LF,也就是版本庫中的換行符號一律存成 LF:在 c ...

  6. 2017 ACM/ICPC Asia Regional Beijing Online 记录

    题目链接  Beijing

  7. HDU - 5973 Game of Taking Stones (威佐夫博弈 高精度)

    题目描述: Two people face two piles of stones and make a game. They take turns to take stones. As game r ...

  8. nginx源码学习资源(不断更新)转

    原文地址:http://www.cnblogs.com/yjf512/archive/2012/06/13/2548515.html nginx源码学习是一个痛苦又快乐的过程,下面列出了一些nginx ...

  9. win7配置java环境变量

    http://jingyan.baidu.com/article/9f63fb91d87fb0c8400f0e93.html 安装JDK,从Oracel官方网站上下载,也可以通过搜索,进入链接.下载完 ...

  10. vim调用python格式化json数据

    vim调用python格式化json数据 November 30, 2013GNU/Linuxpython3, Vimopenwares python有个标准模块叫json,用于编码/解码,序列化/按 ...