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人,人能 ...
随机推荐
- java多线程获取返回结果--Callable和Future示例
package test.guyezhai.thread; import java.util.ArrayList; import java.util.Date; import java.util.Li ...
- XML学习(1)
什么是XML? XML是可拓展标记语言,类似HTML,它的设计宗旨是为了传输数据,而不是像HTML那样显示数据.XML标签没有被预定义,需要用户自定义标签. xml文档必须包含根元素,它是其他所有元素 ...
- 训练赛第一场A题 (ZOJ 2313)
解题报告:n个人围坐成一圈,并且将这n个人从1到n编号,然后编号为1 的人手上有一个物品,将这个物品往向左传递给第k个人,1<=k<=n/2,当这个物品再次传到编号为1 的人的手上时,游戏 ...
- TI的H264 SOC方案
TI的H264 SOC方案是目前常用的视讯解决方案,TI针对视频会议,视频监控,视频存储等场景细化需求并优化了H264技术. 1. TI H.264背景 如今视频压缩技术在视频领域有非常多的应用需求. ...
- Shell基础-Bash命令类型
Bash命令大体可以分为两类: 第一类是可执行文件,例如ls等 第二类是Bash内建命令,常见echo,cd等 bash命令优先级表:1别名 由alias指定 2关键字 3函数 由function ...
- Print Numbers by Recursion
Print numbers from 1 to the largest number with N digits by recursion. Notice It's pretty easy to do ...
- Add Two Numbers I & II
Add Two Numbers I You have two numbers represented by a linked list, where each node contains a sing ...
- mini2440的程序下载
mini2440拿到手有四天了,抱着很大的兴趣看韦东山老师的视频,但是因为电脑是win7 64bit的系统,dnw的驱动没有,经查询可以使用supervivi,就查找相关的资料.但是始终弄不好,后来使 ...
- linux编译警告 will be initialized after
http://blog.chinaunix.net/uid-17019762-id-3152012.html 作为一个有强迫症的人,实在是受不了 warning 的存在 这个warning是由于初始化 ...
- Operfire 安装指南
http://www.cnblogs.com/hoojo/archive/2012/05/13/2498151.html 本文的英文原文来自 http://www.igniterealtime.org ...