codeforces ice cave
///
/// 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎
/// DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了;2. 若两点相邻,
/// 那么要不就是踩一脚就破了或者踩一脚走开再走回来踩一脚破了;3. 普通的搜索看是否能到达,
/// 若能还是要讨论终点踩几脚的问题:)
/// DFS 耗时大,险些超时,可以用BFS来做
///
/// 自己写了一个能AC的,感觉太长比较挫,看到网上有比较短的,注释了下觉得应该自己以后应该写成这样
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
#include <iostream>
using namespace std;
const int MAXN = 5e2 + ;
const int INF = 0x3f3f3f3f;
int n, m;
int sx, sy, ex, ey;
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
int dx[] = {, -, , };
int dy[] = {, , -, }; bool BFS(void)
{
queue<pair<int, int> > Q;///2个变量时使用pair,不写结构体,bfs能缩短几行代码
Q.push (make_pair (sx, sy));
while (!Q.empty ())
{
int x = Q.front ().first;
int y = Q.front ().second;
Q.pop ();
for (int i=; i<; ++i)
{
int tx = x + dx[i];
int ty = y + dy[i]; if (tx == ex && ty == ey) return true; if (tx <= n && tx >= && ty <= m && ty >= && maze[tx][ty] == '.')
{
maze[tx][ty] = 'X';
Q.push (make_pair (tx, ty));
}
}
} return false;
} int main(void)
{
while (scanf ("%d%d", &n, &m) == )
{
memset (vis, , sizeof (vis));
for (int i=; i<=n; ++i)
{
scanf ("%s", maze[i]+);
}
scanf ("%d%d", &sx, &sy);
scanf ("%d%d", &ex, &ey); int cnt = ;
bool flag = false;
///终点四周'.'的个数
for (int i=; i<; ++i)///未BFS前就处理出来,免得bfs后处理同样要多几行代码且麻烦,而且这里是处理了所有的情况
{
int tx = ex + dx[i];
int ty = ey + dy[i];
if (tx == sx && ty == sy) flag = true;
if (tx <= n && tx >= && ty <= m && ty >= && maze[tx][ty] == '.') cnt++;
} if (sx == ex && sy == ey)///为'X’,四周需要至少一个'.'来做铺垫(走到‘.’,再走回来)
{
if (cnt >= ) puts ("YES");
else puts ("NO");
}
else if (flag)///起点和终点相邻,分终点是‘X’ 还是‘.’的情况
{
if (maze[ex][ey] == 'X') puts ("YES");
else
{
if (cnt >= ) puts ("YES");///是‘.’的话 同样需要周围至少有一个'.'来做铺垫
else puts ("NO");
}
}
else
{
///起点和终点不相邻的情况,BFS是否连通
if (BFS () == true)
{
if (maze[ex][ey] == 'X') puts ("YES");
else if (cnt >= ) puts ("YES");///终点和起点不相邻时,终点周围至少要有2个.才能满足要求
else puts ("NO");
}
else puts ("NO");
}
}
return ;
}
codeforces ice cave的更多相关文章
- 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 ...
- 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 ...
- CodeForces - 540C Ice Cave —— BFS
题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...
- ice cave
Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...
- CF#301 C:Ice Cave(简单BFS)
C:Ice Cave 有一个m*n的地图,里面包含'.'表示完整的冰块,'X'表示有裂痕的冰块,当游戏者到达完整的冰块时,这个位置的冰块会变成有裂痕的冰块,如果到达有裂痕的冰块时,游戏者会进入下一关 ...
- ICE CAVE(BFS搜索(模拟))
Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...
- 「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)
题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...
随机推荐
- 字符串替换For linux C
1.临时空间给了个1024,不需要可减少长度. 2.结果只用用strcpy了,没校验. bool Replace(char *str,const char *src, const char *des) ...
- oracle数据库 网页管理360浏览器登录不上
使用谷歌浏览器可以登陆,然后在使用360之类的浏览器 就可以登录了
- phonegap二维码扫描插件
原文出处:http://rensanning.iteye.com/blog/2034026 谈谈我使用这个的体会吧; git地址 https://github.com/wildabeast/Barco ...
- Teaching Is a Fruitful Way to Learn【教学是一种有效的学习方式】
Teaching Is a Fruitful Way to Learn For thousands of years, people have known that the best way to u ...
- 笨小熊 南阳acm62
笨小熊 时间限制:2000 ms | 内存限制:65535 KB 难度:2 描述 笨小熊的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项 ...
- linux-shell——03
mkdir 创建一个新目录格式: mkdir [选项-p][路径]目录名 -p 递归创建多级目录 mkdir -p b/c/e/f/g rmdir 删除一个空目录 touch 创建一个空文件,更新文件 ...
- [CodeForces948D]Perfect Security(01字典树)
Description 题目链接 Solution 01字典树模板题,删除操作用个数组记录下就行了 Code #include <cstdio> #include <algorith ...
- Json的用处一
今天,我们用到了json的的用处,其实也就是一个很简单的用处,就是点击一个按钮,触发一个事件,然后调用json, 之后我们就可以进行异步操作,其实只是针对于后台的操作,其实我们并没有对数据库进行刷新, ...
- Alter the structure of web pages with JavaScript
Most of the DOM methods you've seen so far are useful for identifying elements. Both getElementById ...
- Android 导出traces.txt 遇到的坑
我一直以为traces.txt 导出需要root .因为每当我 cd data ll 然后就会告诉我 Permission denied 后来我问同事,怎么导出traces.txt 文件.同事说很简单 ...