CSUOJ 1726 你经历过绝望吗?两次!BFS+优先队列
Description
Input
Output
一个整数,为最少拆毁的障碍物数量,如果不能逃离废墟,输出-1。
Sample Input
3
3 3
###
#@*
***
3 4
####
#@.*
**.*
3 3
.#.
#@#
.#.
Sample Output
1
0
-1
Hint
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 110
typedef long long ll;
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int dir[4][2] = { 1, 0 ,0, 1, -1, 0, 0, -1 };
struct node{
int x, y, sum;
friend bool operator<(node a, node b)//sum值小的排在队列队首
{
return a.sum>b.sum;
}
};
int N, M;
priority_queue<node>q;
bool check(int x,int y)
{
if (x >= 0 && y >= 0 && x < N&&y < M&&!vis[x][y] && map[x][y] != '#')
return true;
return false;
}
int bfs()
{
node u, v;
while (!q.empty())
{
u = q.top();
q.pop();
if (u.x == 0 || u.y == 0 || u.x == N - 1 || u.y == M - 1)
return u.sum;//到达边缘获救
for (int i = 0; i < 4; i++)
{
v.x = u.x + dir[i][0];
v.y = u.y + dir[i][1];
if (check(v.x, v.y))
{
if (map[v.x][v.y] == '.')
v.sum = u.sum;
else
v.sum = u.sum + 1;
vis[v.x][v.y] = 1;
q.push(v); }
}
}
return - 1;
}
int main()
{
int T;
//printf("%d %d", '.', '*');
while (~scanf("%d", &T))
{
while (T--)
{
node u;
memset(vis, 0, sizeof(vis));
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++)
{
scanf("%s", map[i]);
}
int flag = 0;
while (!q.empty())
q.pop();
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (map[i][j] == '@')
{
u.x = i; u.y = j; u.sum = 0;
vis[u.x][u.y] = 1;
q.push(u);
flag = 1;
break;
}
}
if (flag)
break;
}
int t = bfs();
printf("%d\n", t); }
}
return 0;
} /**********************************************************************
Problem: 1726
User: leo6033
Language: C++
Result: AC
Time:12 ms
Memory:2080 kb
**********************************************************************/
CSUOJ 1726 你经历过绝望吗?两次!BFS+优先队列的更多相关文章
- CSU 1726: 你经历过绝望吗?两次!(bfs+优先队列)
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1726 1726: 你经历过绝望吗?两次! Submit Page Summar ...
- CSU - 2031 Barareh on Fire (两层bfs)
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...
- POJ 1475 Pushing Boxes 搜索- 两重BFS
题目地址: http://poj.org/problem?id=1475 两重BFS就行了,第一重是搜索箱子,第二重搜索人能不能到达推箱子的地方. AC代码: #include <iostrea ...
- HDU2612---(两次BFS)
Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...
- UVa 1599 Ideal Path (两次BFS)
题意:给出n个点,m条边的无向图,每条边有一种颜色,求从结点1到结点n颜色字典序最小的最短路径. 析:首先这是一个最短路径问题,应该是BFS,因为要保证是路径最短,还要考虑字典序,感觉挺麻烦的,并不好 ...
- UVA 11624 Fire!(两次BFS+记录最小着火时间)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- hihocoder#1050 : 树中的最长路(树中最长路算法 两次BFS找根节点求最长+BFS标记路径长度+bfs不容易超时,用dfs做TLE了)
#1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...
- Fire! UVA - 11624 (两步bfs)
题目链接 题意 人要从迷宫走出去,火会向四个方向同时扩散 分析 两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能 ...
随机推荐
- React - 环境准备
1. 下载 node.js,在 https://nodejs.org/en/download/ 选择合适的版本. 2. 检查 node.js 是否安装成功. hueyMB:~ zhisenzheng$ ...
- Spring Enable*高级应用及原理
Enable* 之前的文章用到了一些Enable*开头的注解,比如EnableAsync.EnableScheduling.EnableAspectJAutoProxy.EnableCaching等, ...
- C++ Rule of Three
Rule of Three The rule of three (also known as the Law of The Big Three or The Big Three) is a rule ...
- R6—单变量正态性检验
方法不唯一 单变量正态检验主要的话包括以下这些 shapiro.test();#Shapiro-Wilk检验 library("nortest"); lillie.test() # ...
- Eclipse升级后导入插件的方法
昨天把eclipse 从 indigo(3.7) 升级到了 juno(4.2). 启动 eclipse 发现从前装的 android 和 maven 插件都不见了. $ ls .eclipse/ or ...
- HDU 6096 String (AC自动机)
题目链接 Problem Description Bob has a dictionary with N words in it. Now there is a list of words in wh ...
- vue_router 动态路由
配置子路由: 路由的视图都需要使用view-router 子路由也可以嵌套路由使用: children来做嵌套如上图 使用location.页面name就可以做页面跳转 mounted:挂载,延迟跳转 ...
- A - ACM Computer Factory(网络流)
题目链接:https://cn.vjudge.net/contest/68128#problem/A 反思:注意拆点,否则的话节点就没用了,还有注意源点和汇点的赋值. AC代码: #include&l ...
- 【矩阵】RQ/QR 分解
Multiple View Geometry in Computer Vision A.4.1.1 (page 579) 将一个 3x3 矩阵 $ A $ 进行 RQ 分解是将其分解成为一个上三角阵 ...
- AopProxyUtils.getSingletonTarget(Ljava/lang/Object;)Ljava/lang/Object;大坑
这个问题太坑了,试了好多个版本,都是依赖冲突导致的, https://blog.csdn.net/qq_15003505/article/details/78430595 最后找到这一篇博客解决了,就 ...