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 ...
随机推荐
- iOS __block用法
没有__block qualifier的primitive c types会直接在创建block的时候被capture到block里.有__block qualifier的话,会在调用block的时候 ...
- 关于 NoSQL 数据库你应该了解的 10 件事
四分之一个世纪以来,关系型数据库(RDBMS)一直是主流数据库模型.但是现在非关系型数据库,“云”或者“NoSQL”数据库,正在作为一种替代数据库模型获得越来越多的占有率.本文中我们将关注非关系型 N ...
- maven clean 报错
eclipse在使用maven的tomcat控件编译java程序时,报错 Failed to execute goal org.apache.maven.plugins:maven-clean-plu ...
- lantern蓝灯导致IE和一些软件不能上网解决方法
@echo offREG DELETE "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Setti ...
- Servlet中获取JSP内置对象
方便自己查询,嫌低级的勿喷.... 1.request 在servlet的doGet和doPost的参数中就有HttpServletRequest req参数,而JSP内置request对象就是Htt ...
- cocos2dx内存管理的一些看法
今年年初进入一家游戏公司,正式开始游戏引擎的学习,之前的ios学习,对现在的游戏引擎学习还是有很大的帮助的,虽然使用c++,但却能时刻感受到ios框架对于cocos2dx的巨大影响. 由于之前一直使用 ...
- [置顶] android 自定义ListView实现动画特效
通过自定义ListView实现动画特效,被点击元素A向前移,A之前元素往后移动. 重点在于动画的实现: 具体代码如下: package com.open.widget; import java.uti ...
- java.lang.Math中的基本方法
java.lang.Math类提供的方法都是static的,“静态引入 ”使得不必每次在调用类方法时都在方法前写上类名: import static java.lang.Mat ...
- discuz, 使用同一数据库, 只是换个环境, 数据就不一样了
如题, 本以为是由于某些冲突导致, 细查之后, 发现是开了缓存了, 把缓存关掉或是在后台清理缓存就OK了 后台清理缓存, 全局--性能优化--内存优化 清理缓存 关闭缓存, 修改全局配置文件, co ...
- Android 环境下编译FFmpeg
Android 环境下编译FFmpeg 开发环境:Ubuntu 12.04.2 LTS , android-sdk-linux, android-ndk-r8e 一 .X264 编译 1. X2 ...