BFS简单题套路_Codevs 1215 迷宫
BFS 简单题套路
1. 遇到迷宫之类的简单题,有什么行走方向的,先写下面的 声明
const int maxn = ;
struct Status {
int r, c;
Status(int r = , int c = ) : r(r), c(c) {}
// int DIR;
};
int N; //迷宫数量
int W; //迷宫宽度
char map[maxn][maxn]; //地图
//方向 : 分别代表 上、右、下、左向量
int dir[][] = { {-, }, {, }, {, }, {, -} };
bool vis[maxn][maxn];
//队列
queue<Status> que; void input(); //输入数据
bool judge(int r, int c);//判断是否越界, 是否是路
bool check(int r, int c);//判断当前是否到达终点
//移动一步
void Move(const Status& now, int r, int c, int k);
void BFS(); //BFS搜索
void solve();
2. 随后再逐个函数的实现
//完整代码
/*
* Codevs 1215 迷宫
*/
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std; const int maxn = ;
struct Status {
int r, c;
Status(int r = , int c = ) : r(r), c(c) {}
// int DIR;
};
int N; //迷宫数量
int W; //迷宫宽度
char map[maxn][maxn]; //地图
//方向 : 分别代表 上、右、下、左向量
int dir[][] = { {-, }, {, }, {, }, {, -} };
bool vis[maxn][maxn];
//队列
queue<Status> que; void input(); //输入数据
bool judge(int r, int c);//判断是否越界, 是否是路
bool check(int r, int c);//判断当前是否到达终点
//移动一步
void Move(const Status& now, int r, int c, int k);
void BFS(); //BFS搜索
void solve(); //启动 void input()
{
memset(map, , sizeof(map));
memset(vis, , sizeof(vis));
while (!que.empty()) que.pop();
scanf("%d", &W);
getchar();
for (int i = ; i < W; i++) {
scanf("%s", map[i]);
}
} bool judge(int r, int c)
{
return (r >= && r < W) && (c >= && c < W)
&& map[r][c] != '#';
} bool check(int r, int c)
{
return (r == W- && c == W-) && map[r][c] == 'e';
} void Move(const Status& now, int r, int c, int k)
{
Status tmp = now;
int tmpx = tmp.r;
int tmpy = tmp.c; tmpx += dir[k][];
tmpy += dir[k][]; //判断是否越界, 是否是路, 是否走过
if (!judge(tmpx, tmpy) || vis[tmpx][tmpy]) return; if (check(tmpx, tmpy)) {
printf("YES\n");
exit();
}
vis[tmpx][tmpy] = true;
que.push(Status(tmpx, tmpy));
} void BFS()
{
que.push(Status(, ));
while (!que.empty())
{
Status now = que.front(); que.pop();
int r = now.r, c = now.c;
for (int k = ; k < ; k++) {
Move(now, r, c, k);
}
}
printf("NO\n");
} void solve()
{
scanf("%d", &N);
while (N--) {
input();
BFS();
}
} int main()
{
solve();
return ;
}
BFS简单题套路_Codevs 1215 迷宫的更多相关文章
- bfs简单题-poj2251
宽搜基础题 思路很简单,注意细节. 走过的节点一定要打上标记//tag数组 三维字符串输入一定要注意 #include <stdio.h> #include <iostream> ...
- 胜利大逃亡(杭电hdu1253)bfs简单题
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)
深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析
转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- 【BZOJ-1176&2683】Mokia&简单题 CDQ分治
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- Bzoj4066 简单题
Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 2185 Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...
- Bzoj2683 简单题
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1071 Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...
随机推荐
- redis安装启动和数据操作
redis安装和启动 1.安装包下载地址 >> redis基本数据类型 string(字符串和数值) .list(列表/队列).hashmap(哈希表[键唯一]). set(集合[值唯一] ...
- SpringBoot日记——编码配置篇
插入一个小篇章,有人在编写代码的时候,要么控制台乱码,要么页面乱码等等, 我这里有个配置,可以解决各种乱码问题,直接来看. # ==================== 编码配置 ========== ...
- JQ_开发经验
1. 把你的代码全部放在闭包里面 这是我用的最多的一条.但是有时候在闭包外面的方法会不能调用.不过你的插件的代码只为你自己的插件服务,所以不存在这个问题,你可以把所有的代码都放在闭包里面.而方法可能应 ...
- C# List left join
public class Test1 { public int ID { get; set; } public string Name { get; set; } } public class Tes ...
- Linux使用expect实现免手动密码输入,linux免密码登陆
使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写却不知其然.本文用一个最短的例子说明脚本的原理. 脚本代码如下: ###### ...
- Unity 2D相机公式换算(从其他博客上抄的)
2d camera, unit坐标,单位换算 2d游戏可以使用平行投影的camera,这种camera需要设置size (orthographicSize),size的含义为屏幕高度的一半,不过单位不 ...
- 20135119_涂文斌 实验二 Java面向对象程序设计
北京电子科技学院(BESTI) 实 验 报 告 课程: Java 班级:1351 姓名:涂文斌 学号:20135119 成绩: ...
- How to delete deployed process definition in activiti?
https://community.alfresco.com/thread/219767-how-to-delete-deployed-process
- EntityFramework异常The specified cast from a materialized 'System.Double' type to the 'System.Single' type is not valid.
实体类: public class ReportEntity { public string FactorName { get; set; } public double MaxVal { get; ...
- Before NOIP2017
明天就比赛了呢! 说起来,这是我第二次,可能也是最后一次正式参加提高组的比赛了. 虽然是从初中就有参加信息学的学习,但是认真学习信息竞赛还是去年七月开始的.NOIP2016 中,我凭着两天的简单题和一 ...