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. 洛谷 P 1330 封锁阳光大学

    题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构成的无向图,N个点之间由M ...

  2. MBP清除NVRAM和PRAM

    Mac 会将某些设置存储在特定的存储区中,即使关机这些设置也不会丢失.在基于 Intel 的 Mac 上,存储位置是称为 NVRAM 的内存:而在基于 PowerPC 的 Mac 上,存储位置则是称为 ...

  3. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  4. Struts2 文件上传和下载

    首先我们写一个单文件长传的fileupload.jsp: <body> <s:fielderror></s:fielderror> <!-- 报错信息 --& ...

  5. T2038 香甜的黄油 codevs

    http://codevs.cn/problem/2038/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond   题目描述 Description 农夫John ...

  6. python多线程(一)

    原文:http://www.pythonclub.org/python-basic/threading 一.python多线程thread和threading实现 python是支持多线程的,并且是n ...

  7. 转:HtmlCxx用户手册

    1.1 简介 使用由KasperPeeters编写的强大的tree.h库文件,可以实现类似STL的DOM树遍历和导航. 打包好的Css解析器. 看似很像C++代码的C++代码(其实已不再是C++了) ...

  8. linux 系统命令----修改主机名

    1. hostname    ***** 2.修改/etc/sysconfig/network 3./etc/hosts 最后第三步没有必要!

  9. 碰撞检測之OBB-OBB的SweepTest

    提要 当物体在运动的时候.普通的每帧进行碰撞检測已经无法满足要求,比方子弹的运动 两帧的位置已经直接将中间的板子穿过了,所以 t 时刻和 t +1 时刻的检測都是失效的.这时候须要用到的就是sweep ...

  10. photoshop 前端常用技巧

    1.将图片转换成 png 格式 并且 使背景透明 (1)用矩形选框工具选取一块区域 (2)右键 选择 ‘变换选区’ 进行微调 F8 查看尺寸 (3)复制图层(ctrl+c) ->新建文件(ctr ...