CodeForces 540C Ice Cave (BFS)
题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去。
析:这个题,在比赛时就是没做出来,其实是一个水题,但是我理解错了意思,让下面提示的第一组样例给搞乱。
思路应该是这样的,从开始坐标BFS末坐标,把经过的都标成'X',如果直到末坐标是'.',接着走,如果能直到末坐标是'X',就是可以,否则就是不可以。
代码如下:
#include <bits/stdc++.h> using namespace std;
typedef long long LL;
const int maxn = 500 + 5;
const int INF = 0x3f3f3f3f;
const int dr[] = {0, 0, 1, -1};
const int dc[] = {1, -1, 0, 0};
struct node{
int r, c;
node(int rr = 0, int cc = 0) : r(rr), c(cc) { }
};
int n, m;
char s[maxn][maxn];
int r2, c2; bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} bool bfs(int r, int c){
queue<node> q;
q.push(node(r, c));
s[r][c] = 'X';
while(!q.empty()){
node u = q.front(); q.pop();
for(int i = 0; i < 4; ++i){
int x = u.r + dr[i];
int y = u.c + dc[i];
if(x == r2 && y == c2 && s[x][y] == 'X') return true;
if(is_in(x, y) && s[x][y] == '.'){
s[x][y] = 'X';
q.push(node(x, y));
}
}
}
return false;
} int main(){
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%s", s[i]);
int r1, c1;
cin >> r1 >> c1 >> r2 >> c2;
--r1, --r2, --c1, --c2;
if(bfs(r1, c1)) puts("YES");
else puts("NO"); return 0;
}
CodeForces 540C Ice Cave (BFS)的更多相关文章
- 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 ...
- 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题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...
随机推荐
- 3D角色渲染到2D界面上
using UnityEngine; using System.Collections; using System.Collections.Generic; using Carrie.Net; usi ...
- 重载(overload),覆盖(override),隐藏(hide)的区别
写正题之前,先给出几个关键字的中英文对照,重载(overload),覆盖(override),隐藏(hide).在早期的C++书籍中,可能翻译的人不熟悉专业用语(也不能怪他们,他们不是搞计算机编程的, ...
- Linux常用命令英文缩写
命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户 (ubuntu 使用管理员权限 是 sudo) rpm ...
- canvas之太阳系效果
星球 变量名 公转周期 光色 暗色 水星 Mercury 87.70 #a69697 #5c3e40 金星 Venus 224.701.70 #c4bbac #1f1315 地球 Earth 365. ...
- (文件名.JAVA)的文件名只能与该文件中的public类的名称一致
1.如果类Yuangong 被声明为公共的(public),那么必须将类Yuangong 保存在名为Yuangong.java的文件中:2.反之,在一个文件中最多包含一个顶级的公共类,并且该公共类的名 ...
- gil基本介绍
一 引子 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...
- 新版台式机安装win7操作系统
1.进入bios设置-----authentication选项中的secure boot设置为disabled2.在boot options选项中launch csm设置为always 3.在boot ...
- Linux6系统安装
- Oracle的Spool导出数据
出自:http://wallimn.iteye.com/blog/472182 实践 只能在一个终端上的一个窗口中进行操作 第一步:连接oracle数据库 sqlplus qkp/mm_eft ...
- hibernate多个主键
在hibernate中同一张表里面存在多个主键,必须要实现序列化接口(Serializable )