CodeForces - 540C Ice Cave —— BFS
题目链接:https://vjudge.net/contest/226823#problem/C
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 mcolumns. 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'.
Examples
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
题意:
在一个n*m的冰面上:当跳到‘.’上时,此位置变为‘X’,即碎裂;当跳到‘X’时,冰面穿了,人会掉到下一层去。问是否存在一条路径:使得人从起始点(必定为‘X’)跳到终点,然后恰好从终点掉到下一层。人可以上下左右跳。
题解:
1.当终点为‘X’时,直接bfs找到一条从起点到终点的路径即可。
2.当终点为‘.’时,终点必须在同一条路径上出现两次。第一次时,终点变为‘X’,之后,为了跳回到终点,可选择跳到终点四侧可跳的位置,然后又调回到终点,即可满足要求。
3.总和第1、2点,可直接bfs,当遇到终点为‘X’或者已经vis过时,即可满足条件。对于终点为‘X’,当然无可厚非,但对于终点为‘.’时,为什么vis过也可满足条件呢?要知道bfs是不确定路径上有谁的。答:对于能访问被vis过的终点,无非只有两种:1是路径上有终点,然后又跳了回来,这样当然可以;第二种是:路径上无终点,但既然它能跳到终点,就证明终点可以从这条路径上兜个圈再回来,所以同样满足条件。……太难表述啦
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e3+; int dir[][] = {,,,,-,,,-};
char M[MAXN][MAXN];
int n, m; struct node
{
int x, y;
}; queue<node> q;
int vis[MAXN][MAXN];
bool bfs(node st, node en)
{
memset(vis, , sizeof(vis));
while(!q.empty()) q.pop(); q.push(st);
vis[st.x][st.y] = ; node now, e;
while(!q.empty())
{
now = q.front();
q.pop(); for(int i = ; i<; i++)
{
e.x = now.x + dir[i][];
e.y = now.y + dir[i][];
if(e.x<||e.x>n||e.y<||e.y>m) continue;
if(e.x==en.x&&e.y==en.y&&(M[e.x][e.y]=='X'||vis[e.x][e.y]))
return true; if(vis[e.x][e.y]||M[e.x][e.y]=='X') continue;
vis[e.x][e.y] = ;
q.push(e);
}
}
return false;
} int main()
{
while(cin>>n>>m)
{
for(int i = ; i<=n; i++)
scanf("%s",M[i]+);
node st, en;
cin>>st.x>>st.y>>en.x>>en.y;
if(bfs(st,en)) puts("YES");
else puts("NO");
}
}
CodeForces - 540C Ice Cave —— BFS的更多相关文章
- CodeForces 540C Ice Cave (BFS)
题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去. 析:这个题,在比赛时就是没做出来,其实是一个水题 ...
- CodeForces 540C Ice Cave (BFS)
http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS Memory Limit:262 ...
- Codeforces Round #301 (Div. 2) C. Ice Cave BFS
C. Ice Cave Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/problem/C ...
- DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave
题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...
- (简单广搜) Ice Cave -- codeforces -- 540C
http://codeforces.com/problemset/problem/540/C You play a computer game. Your character stands on so ...
- BFS学习 Codeforces 301_div.2_Ice Cave
C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces 301_div.2_Ice Cave(BFS走冰块)
Ice Cave Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descripti ...
- CF#301 C:Ice Cave(简单BFS)
C:Ice Cave 有一个m*n的地图,里面包含'.'表示完整的冰块,'X'表示有裂痕的冰块,当游戏者到达完整的冰块时,这个位置的冰块会变成有裂痕的冰块,如果到达有裂痕的冰块时,游戏者会进入下一关 ...
- 「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)
题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...
随机推荐
- JAVA Eclipse创建的Android程序如何不显示标题栏
在manifest.xml文件的application节点添加下面一行即可 android:theme="@android:style/Theme.NoTitleBar" 如果要恢 ...
- base64加密PHP脚本的解码方法
转自:http://yoursunny.com/t/2009/PHP-decode/ PHP是网站服务端最流行的编程语言之一.PHP运行环境本身是开源的,服务器不加载插件时PHP脚本也无法加密.但是, ...
- Skia构建系统与编译脚本分析
分析下Skia的构建系统,详细编译过程參看Windows下从源代码编译Skia.这里以ninja为例来分析.运行以下三条命令就能够完毕编译: SET "GYP_GENERATORS=ninj ...
- iOS多线程与网络开发之小文件上传
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. /** 取得本地文件的MIMEType */ 2 - (void) getMIMEType { 3 // Socket 实现断点上传 4 5 //apa ...
- maven打包时无法加载lib下的jar
© 版权声明:本文为博主原创文章,转载请注明出处 问题描述: 项目在本地部署没有问题,但是使用maven打包时报错: ***(引用jar中某个类的的路径) 不存在 ***(某个java类中的某行某列) ...
- A和B是好友,他们经常在空闲时间聊天,A的空闲时间为[a1 ,b1 ],[a2 ,b2 ]..[ap ,bp ]。B的空闲时间是[c1 +t,d1 +t]..[cq +t,dq +t],这里t为B的起床时间。这些时间包括了边界点。B的起床时间为[l,r]的一个时刻。若一个起床时间能使两人在任意时刻聊天,那么这个时间就是合适的,问有多少个合适的起床时间?
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...
- 如何利用JQuery获取iframe内联框架对象?
parent.$("#iframeID").get(0).contentWindow; 父.$("选择器").get(0).contentWindow; get ...
- 【转】Android7.0版本以上的手机Eclipse无法打出LogCat
本来想用Eclipse连下手机看下log的,结果LogCat没打出来任何信息,起初怀疑是我的DDMS有问题,结果连了下我老大的手机,完美打出log,看了下Android系统,老大的是6.0的,我的7. ...
- UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position xxx ordinal not in range(12
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't deco ...
- Git --更改远程分支名
git更新远程分支名字 git checkout old_branch git branch -m old_branch new_branch git push --delete origin old ...