///
/// 题意:告诉起点终点,踩一次, '.'变成'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的更多相关文章

  1. DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

    题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...

  2. (简单广搜) Ice Cave -- codeforces -- 540C

    http://codeforces.com/problemset/problem/540/C You play a computer game. Your character stands on so ...

  3. CodeForces 540C Ice Cave (BFS)

    http://codeforces.com/problemset/problem/540/C       Ice Cave Time Limit:2000MS     Memory Limit:262 ...

  4. 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 ...

  5. CodeForces - 540C Ice Cave —— BFS

    题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...

  6. ice cave

    Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...

  7. CF#301 C:Ice Cave(简单BFS)

    C:Ice Cave 有一个m*n的地图,里面包含'.'表示完整的冰块,'X'表示有裂痕的冰块,当游戏者到达完整的冰块时,这个位置的冰块会变成有裂痕的冰块,如果到达有裂痕的冰块时,游戏者会进入下一关 ...

  8. ICE CAVE(BFS搜索(模拟))

    Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...

  9. 「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)

    题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...

随机推荐

  1. LOJ#515. 「LibreOJ β Round #2」贪心只能过样例(bitset)

    内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: nzhtl1477 提交提交记录统计讨论测试数据   题目描述 一共有 nnn个数,第 iii ...

  2. Redis连接工具类

    Redis连接工具类 导包 测试一下(junit) package com.test; import org.junit.Test; import redis.clients.jedis.Jedis; ...

  3. 神经网络系列学习笔记(一)——神经网络之ANN学习资料汇总

    ANN tutorial: http://adventuresinmachinelearning.com/neural-networks-tutorial/ https://www.cs.toront ...

  4. Android和IOS网页不一致汇总

    1.input type=text 内容输入框的不一致,ios会默认给输入框添加自己的样式,导致在横向的输入框长度精准控制的时候,ios的输入框一般都比android上要长一点,还有内部阴影 解决此问 ...

  5. 根据html页面模板动态生成html页面(c#类)

    本文转载自:http://www.cnblogs.com/yuanbao/archive/2008/01/06/1027985.html点击打开链接 一直以为动态生成静态页面不好做,昨天在网上找了下, ...

  6. 基于THINKPHP+layui+Ajax无刷新实现图片上传预览

    <fieldset class="layui-elem-field" style="width:500px;margin:50px 0 0 300px;" ...

  7. 02 mysql 基础二 (进阶)

    mysql 基础二 阶段一 表约束 1.not null 非空约束 例子: create table tb1( id int, name varchar(20) not null ); 注意 空字符不 ...

  8. ERROR 1005 (HY000): Can't create table 'students.#sql-d9

    今天在创建外键的时候出现以下错误        ERROR 1005 (HY000): Can't create table 'students.#sql-d99_3' (errno: 150) 格式 ...

  9. Hacker Cups and Balls Gym - 101234A 二分+线段树

    题目:题目链接 题意:有编号从1到n的n个球和n个杯子. 每一个杯子里有一个球, 进行m次排序操作,每次操作给出l,r. 如果l<r,将[l,r]范围内的球按升序排序, 否则降序排, 问中间位置 ...

  10. IE浏览器调用ActiveX获取U盘中的文件

    <p> <script type="text/javascript" language="javaScript">// <![CD ...