Codeforces 301_div.2_Ice Cave(BFS走冰块)
Ice Cave
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
Hint(看下面)
In the first sample test one possible path is:
After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
BFS搜索!!很明显了!
要注意的是冰块踩过会变成X,下一次踩就会掉下去。
题解:pair是一种模板类型,其中包含两个数据值,两个数据的类型可以相同可以不同 ,由于pair类型的使用比较繁琐,因为如果要定义多个形同的pair类型的时候,可以时候typedef简化声明:
typedef pair<int, int>p;p中含有2个整型数
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> p;
int dx[]= { , -, , };
int dy[]= { , , , - };
int n,m;
int r1,c1,r2,c2;
char a[][];
int bfs()
{
queue<p> q;
q.push(p(r1,c1));
a[r1][c1]='X'; //第一步上去冰块已碎
while(!q.empty())
{
r1=q.front().first;
c1=q.front().second;
q.pop();
for(int i=; i<; i++)
{
int xx=r1+dx[i];
int yy=c1+dy[i];
if( xx<||xx>=n||yy<||yy>=m)
continue;
if(a[xx][yy]=='X')
{
//到达点也会碎
if(xx==r2 && yy==c2)
return ;
continue;
}
a[xx][yy]='X'; //走了之后就变成易碎的冰块
q.push(p(xx,yy));
}
}
return ;
}
int main()
{
int ans;
while(cin>>n>>m)
{
for(int i=; i<n; i++)
cin>>a[i];
cin>>r1>>c1>>r2>>c2;
r1--; //用的二维数组是从0~n-1,而输入的是1~n,所以都要减1
c1--;
r2--;
c2--;
ans=bfs();
if(ans)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}
Codeforces 301_div.2_Ice Cave(BFS走冰块)的更多相关文章
- 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 540C Ice Cave (BFS)
题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去. 析:这个题,在比赛时就是没做出来,其实是一个水题 ...
- CodeForces - 540C Ice Cave —— BFS
题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...
- 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 ...
- Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)
D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Fair CodeForces - 987D(巧妙bfs)
题意: 有n个城市 m条边,每条边的权值为1,每个城市生产一种商品(可以相同,一共k种),求出分别从每个城市出发获得s种商品时所走过路的最小权值 解析: 我们倒过来想,不用城市找商品,而是商品找城市, ...
- codeforces 1072D Minimum path bfs+剪枝 好题
题目传送门 题目大意: 给出一幅n*n的字符,从1,1位置走到n,n,会得到一个字符串,你有k次机会改变某一个字符(变成a),求字典序最小的路径. 题解: (先吐槽一句,cf 标签是dfs题????) ...
- Knight Moves(BFS,走’日‘字)
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- 在Apache+php中使用json来通讯
示例代码: <?php // 获取输入的内容 $request = http_get_request_body(); // 按json格式解析成一个 php对象 $json_obj = json ...
- web.xml基本配置描述
先加载一段写好的web.xml: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2. ...
- 必胜宅急送Web app设计背后的思考
O2O模式是餐饮业在移动消费趋势下主动拥抱互联网的方向,迎合餐饮消费者从以往经验判断为主转变为依靠移动设备.lbs.社交网络进行立体决策的过程.继App客户端之后,手机web app也逐渐成为O2O中 ...
- 关于Form窗体的StartPosition 属性如何设置的问题
1.让窗体在启动时在指定位置出现 form1.StartPosition Manual CenterScreen WindowsDefaultLocation (default) WindowsDef ...
- CentOS7搭建SAMBA服务器实现与WIN10匿名共享文件
1.安装SAMBA yum -y install samba samba-client samba-common 2.修改文件打开数 vi /etc/security/limits.conf 最后添加 ...
- POJ 1986(LCA and RMQ)
题意:给定一棵树,求任意两点之间的距离. 思路:由于树的特殊性,所以任意两点之间的路径是唯一的.u到v的距离等于dis(u) + dis(v) - 2 * dis(lca(u, v)); 其中dis( ...
- Windows Azure上的Odoo(OpenERP)-2.在Ubuntu虚拟机上部署Odoo(OpenERP)
创建虚拟机的步骤在这里就不再赘述了,请参考上一篇博文. 首先用下述命令将Ubuntu系统进行更新: 1. sudo apt-get update 2. sudo apt-get upgrade 3. ...
- HDU3853
题意:给R*C的迷宫,起点为1,1 终点为R,C 且给定方格所走方向的概率,分别为原地,下边,右边,求到终点的期望. 思路:既然是求到终点的期望,那么DP代表期望,所以DP[i][j]=原地的概率*D ...
- editplus双击单词语法高亮显示设置
view=>Word Highlighting
- DbProviderFactories.GetFactory Oracle.ManagedDataAccess.Client
因为最近项目,要使用微软的EF框架不安装Oracle客户端的情况下,访问Oracle数据库.调用如下代码的时候会报错. System.Data.Common.DbProviderFactories.G ...