Kattis - fire2 【BFS】
第二组样例:
题意
给出一个起始位置,然后要跑到这幢建筑物的外面,上下左右四个方向,只要是空地 就可以走 走一步 花费一秒 然后有若干串火苗,每一秒钟 会向上下左右 四个方向的空地 蔓延 但是 逃跑的优先级在先
比如
这个例子 @会先逃到右边,火苗再蔓延
思路
用BFS 记录能到的下一步的位置,然后记录火苗的本次蔓延位置,用双队列,然后先遍历下一步的位置 再遍历火苗蔓延的位置 最后 设置出口条件就可以了
两个出口
① 当没有下一步的位置 并且当前步没有一步是在边界 就是no
② 当前步在边界 直接更新答案 输出
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;
string Map[maxn];
int n, m;
int ans;
queue <pii> q, f;
int Move[4][2] =
{
-1, 0, //up
1, 0, //down
0,-1, //left
0, 1, //right
};
bool out_range(int x, int y)
{
if (x == 0 || y == 0 || x == (m - 1) || y == (n - 1))
return true;
return false;
}
bool in_range(int x, int y)
{
if (x >= 0 && x < m && y >= 0 && y < n)
return true;
return false;
}
void fire(int x, int y)
{
int next_x, next_y;
for (int i = 0; i < 4; i++)
{
next_x = x + Move[i][0];
next_y = y + Move[i][1];
if (in_range(next_x, next_y))
{
if (Map[next_x][next_y] == '.' || Map[next_x][next_y] == '$')
{
Map[next_x][next_y] = '*';
f.push(pii(next_x, next_y));
}
}
}
}
void dfs(int cur)
{
int x, y;
int next_x, next_y;
int len = q.size();
while (len--)
{
x = q.front().first;
y = q.front().second;
q.pop();
if (Map[x][y] == '*')
continue;
if (out_range(x, y))
{
ans = cur;
return;
}
for (int i = 0; i < 4; i++)
{
next_x = x + Move[i][0];
next_y = y + Move[i][1];
if (in_range(next_x, next_y) && Map[next_x][next_y] == '.')
{
Map[next_x][next_y] = '$';
q.push(pii(next_x, next_y));
}
}
}
if (q.size() == 0)
return;
else
{
int len = f.size();
int x, y;
while (len--)
{
x = f.front().first;
y = f.front().second;
fire(x, y);
f.pop();
}
dfs(cur + 1);
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
while (!q.empty())
q.pop();
while (!f.empty())
f.pop();
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++)
{
cin >> Map[i];
for (int j = 0; j < n; j++)
{
if (Map[i][j] == '@')
{
Map[i][j] = '$';
q.push(pii(i, j));
}
else if (Map[i][j] == '*')
f.push(pii(i, j));
}
}
ans = -1;
dfs(1);
if (ans == -1)
printf("IMPOSSIBLE\n");
else
printf("%d\n", ans);
}
}
Kattis - fire2 【BFS】的更多相关文章
- Kattis - virus【字符串】
Kattis - virus[字符串] 题意 有一个正常的DNA序列,然后被病毒破坏.病毒可以植入一段DNA序列,这段插入DNA序列是可以删除正常DNA序列中的一个连续片段的. 简单来说就是,给你一段 ...
- Kattis - honey【DP】
Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...
- Kattis - fence2【二分法】
Kattis - fence2[二分法] 题意 有一个农夫需要建造一个 N - 1 米长的篱笆,需要 N 根柱子,然后有 K 根 柱子 需要将这 K 根柱子 切成 N 段 然后 要尽量保证这 N 段柱 ...
- Kattis - cokolada【水】
Kattis - cokolada[水] 题意 有一个人想吃巧克力,但是巧克力都是按照 2 的幂次的数量包装的,然后他想吃一定数量块的巧克力,然后可以敲碎,每次敲碎都分成两半,比如四块装的分成两块就是 ...
- Kattis - glitchbot 【DFS】
Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...
- Kattis - amsterdamdistance【数学】
Kattis - amsterdamdistance[数学] 题意 给出两个点 算出从第一个点到第二个点的最短距离,只不过这里不是直角坐标系, 是雷达图 思路 因为内圈的圆的路径要比外圈的小,所以我们 ...
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- 【bfs】拯救少林神棍(poj1011)
Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...
- 【bfs】Knight Moves
[题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...
随机推荐
- cisco 为每个单独的人员设置不同的用户名和密码
cisco 为每个单独的人员设置不同的用户名和密码 2010-12-15 17:00:16 分类: 系统运维 Router1#configure terminalEnter configuration ...
- [linux]free命令详解-显示内存的使用情况
本文部分转载于https://blog.csdn.net/sunansheng/article/details/51942522 free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内 ...
- c#调用WinRAR软件压缩和解压文件
using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Linq ...
- Sql视图创建语句及修改视图
create view [dbo].[AllUsers] as select u.UserId, u.Firstname, u.Lastname, u.ts, am.Email, au.UserNam ...
- bean对应mapper.xml字段
在bean中set的时候最好写上这个,避免报空指针............... public void setImgAddress(String imgAddress) { this.imgAddr ...
- Django--登录认证
COOKIE 与 SESSION 概念 cookie的使用,不止局限于我们所使用的登录认证,cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此 ...
- RealProxy AOP过滤方法的参数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- CAP理论学习
CAP理论是对分布式系统的3个特性所下的一个定性的结论,可用于指导分布式系统的设计. CAP理论断言任何基于网络的数据共享系统,最多只能满足数据一致性.可用性.分区容忍性三要素中的两个要素. 在英语中 ...
- Mysql主从配置笔记
1.配置my.cnf无效,且mysql进程无法启动 从5.1.7版本开始,不再支持my.cnf直接配置master-host等主从相关配置选项(依然支持replicate-do-db).改为使用 CH ...
- Oracle中NVL、NVL2、NULLIF 三个函数的区别?
首先说明:NULL指的是空值,或者非法值. 1.NVL (expr1, expr2)expr1为NULL,返回expr2:不为NULL,返回expr1.注意两者的类型要一致 2.NVL2 (expr1 ...