CodeForces 540C Ice Cave (BFS)
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.
The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.
Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c).
You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this?
Input
The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description.
Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice).
The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked.
The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.
Output
If you can reach the destination, print 'YES', otherwise print 'NO'.
Sample Input
4 6
X...XX
...XX.
.X..X.
......
1 6
2 2
YES
5 4
.X..
...X
X.X.
....
.XX.
5 3
1 1
NO
4 7
..X.XX.
.XX..X.
X...X..
X......
2 2
1 6
YES 题目大意: m*n的冰宫,'X'代表破损,'.'代完好,你初始的位置为(r1, c1),因为你是从上一层掉下来的,所以初始位置为'X'是破损的,你要从(r2, c2)的位置穿到下一层 你走过的地方冰块都会破损,所以处理(r2, c2)外是'X'的你都不能走,但(r2,c2)这个点你需要走两次,第一次为了破损他让他变为'X',第二次到他是通过他到下一层 BFS搜索,DFS会TLE
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<stack>
#include<algorithm> using namespace std;
const int N = ;
typedef __int64 ll; char maps[N][N];
int d[][] = {{, }, {-, }, {, }, {, -}};
int m, n, f, r2, c2; struct node
{
int x, y;
}; bool BFS(int x, int y)
{
queue<node>Q;
node now, next;
now.x = x;
now.y = y;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
for(int i = ; i < ; i++)
{
int a = next.x = now.x + d[i][];
int b = next.y = now.y + d[i][];
if(a == r2 && b == c2 && maps[a][b] == 'X')
return true;
if(a >= && a < m && b >= && b < n && maps[a][b] == '.')
{
if(maps[a][b] == '.')
maps[a][b] = 'X';//走过的地方,冰块会破损,将其变为'X'
Q.push(next);
}
}
}
return false;
} int main()
{
int r1, c1;
while(~scanf("%d%d", &m, &n))
{
f = ;
for(int i = ; i < m ; i++)
scanf("%s", maps[i]);
scanf("%d%d", &r1, &c1);
scanf("%d%d", &r2, &c2);
r1--;
c1--;
r2--;
c2--;
if(BFS(r1, c1))
printf("YES\n");
else
printf("NO\n");
}
return ;
}
CodeForces 540C Ice Cave (BFS)的更多相关文章
- Codeforces 301_div.2_Ice Cave(BFS走冰块)
Ice Cave Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descripti ...
- CodeForces - 540C Ice Cave —— BFS
题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...
- ICE CAVE(BFS搜索(模拟))
Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...
- CodeForces 540C Ice Cave (BFS)
题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去. 析:这个题,在比赛时就是没做出来,其实是一个水题 ...
- codeforces 1283D. Christmas Trees(bfs)
链接: https://codeforces.com/contest/1283/problem/D 题意:给定n个不同的整数点,让你找m个不同的整数点,使得这m个点到到这n个点最小距离之和最小. 思路 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
随机推荐
- Python反转
1切片 s="svdfbffdbdf" a=s[::-1] 2入栈出栈 入栈之后再出栈正好就是了 3reverse 这个函数是列表的....你要先把str转成list list-& ...
- OpenStack概念架构简述
什么是OpenStack OpenStack既是一个社区,也是一个项目和一个开源软件,它提供了一个部署云的操作平台或工具集.其宗旨在于,帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云,也为大云 ...
- 10-能不能组成m
/* 找数达人 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 小明最近做出了一道题:如何在一组数 ...
- chrome,opera..通过file协议浏览html代码时,发送的ajax请求本地文件,会报跨域错误
XMLHttpRequest cannot loadfile:///E:/webs/extJS/ext-3.3.0/examples/csdn/combobox.txt?_dc=14147389739 ...
- php 事务处理,ActiveMQ的发送消息,与处理消息
可以通过链式发送->处理->发送...的方式处理类似事务型业务逻辑 比如 发送一个注册消息,消息队列处理完注册以后,紧接着发送一个新手优惠券赠送,赠送完再发一个其它后续逻辑处理的消息等待后 ...
- 2018.09.17 atcoder Digit Sum(数论)
传送门 数论好题啊. 首先对于b<=sqrt(n)b<=sqrt(n)b<=sqrt(n)的情况直接枚举b判断一下就行了. 下面谈一谈如何解决b>sqrt(n)b>sqr ...
- phalApi数据库操作
在很多时候,我们会遇到数据库表里面的某个值需要+1操作,我们不能简单地在update的时候写入array('key' => 'key+1'),因为在解析sql的时候,key+1 会带上引号作为一 ...
- 自学如何去学习jQuery
学习JQ第一个demo: 制作一个轮播图,制作方法我前面写了一篇博客,传送门-->http://www.cnblogs.com/yewenxiang/p/6100206.html 需要的JQ知识 ...
- nexus 下载及安装
一.下载 nexus maven http://www.sonatype.org/ http://www.sonatype.org/nexus/ http://www.sonatype.org/nex ...
- HDU1459 非常可乐(BFS) 2016-07-24 15:00 165人阅读 评论(0) 收藏
非常可乐 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶 ...