题目传送门

 /*
题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎
DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了;2. 若两点相邻,
那么要不就是踩一脚就破了或者踩一脚走开再走回来踩一脚破了;3. 普通的搜索看是否能到达,
若能还是要讨论终点踩几脚的问题:)
DFS 耗时大,险些超时,可以用BFS来做
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#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[] = {, , -, }; void DFS(int x, int y)
{
vis[x][y] = true;
if (maze[x][y] == 'X') return ; for (int i=; i<; ++i)
{
int tx = x + dx[i];
int ty = y + dy[i]; if (tx <= n && tx >= && ty <= m && ty >= && !vis[tx][ty])
{
DFS (tx, ty);
}
} return ;
} int main(void) //Codeforces Round #301 (Div. 2) C. Ice Cave
{
//freopen ("C.in", "r", stdin); 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)
{
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)
{
if (cnt >= ) puts ("YES");
else puts ("NO");
}
else if (flag)
{
if (maze[ex][ey] == 'X') puts ("YES");
else
{
if (cnt >= ) puts ("YES");
else puts ("NO");
}
}
else
{
maze[sx][sy] = '.';
DFS (sx, sy);
if (vis[ex][ey])
{
if (maze[ex][ey] == 'X') puts ("YES");
else if (cnt >= ) puts ("YES");
else puts ("NO");
}
else puts ("NO");
}
} return ;
}

 #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;
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) //Codeforces Round #301 (Div. 2) C. Ice Cave
{
//freopen ("C.in", "r", stdin); 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)
{
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)
{
if (cnt >= ) puts ("YES");
else puts ("NO");
}
else if (flag)
{
if (maze[ex][ey] == 'X') puts ("YES");
else
{
if (cnt >= ) puts ("YES");
else puts ("NO");
}
}
else
{
maze[sx][sy] = '.';
//DFS (sx, sy);
if (BFS () == true)
{
if (maze[ex][ey] == 'X') puts ("YES");
else if (cnt >= ) puts ("YES");
else puts ("NO");
}
else puts ("NO");
}
} return ;
}

BFS做法

DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave的更多相关文章

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

  2. 贪心 Codeforces Round #301 (Div. 2) B. School Marks

    题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...

  3. 贪心 Codeforces Round #301 (Div. 2) A. Combination Lock

    题目传送门 /* 贪心水题:累加到目标数字的距离,两头找取最小值 */ #include <cstdio> #include <iostream> #include <a ...

  4. BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

    题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...

  5. Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)

    A. Combination Lock time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  6. Codeforces Round #301 (Div. 2)A B C D 水 模拟 bfs 概率dp

    A. Combination Lock time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. CodeForces Round #301 Div.2

    今天唯一的成果就是把上次几个人一起开房打的那场cf补一下. A. Combination Lock 此等水题看一眼样例加上那个配图我就明白题意了,可是手抽没有注释掉freopen,WA了一发. #in ...

  8. 【解题报告】Codeforces Round #301 (Div. 2) 之ABCD

    A. Combination Lock 拨密码..最少次数..密码最多有1000位. 用字符串存起来,然后每位大的减小的和小的+10减大的,再取较小值加起来就可以了... #include<st ...

  9. dfs + 最小公倍数 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/C 题目大意:从x出发,从x->f[x] - > f[f[x]] -> f[f[f[x]]] -& ...

随机推荐

  1. 修改php.ini以达到 屏蔽错误信息

    那是因为php.ini中关闭了错误显示,将错误写成了文件,这是人为设置的结果,display_errors =on就好了. 不过不显示错误倒安全点,建议调试时打开,然后提供服务时关闭. 提供一点资料给 ...

  2. loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination

    1.问题 loadrunner跑场景的时候出现:Abnormal termination, caused by mdrv process termination. 备注:我使用的是RTE协议录制的脚本 ...

  3. table表头标题th浮动提示-MyTable.js

    /* $(document).ready(function () { var maxH = ($(window).height() - $("#divParent").positi ...

  4. (转)WPF控件开源资源

    (转)WPF控件开源资源 Textbox Drag/Drop in WPFhttp://www.codeproject.com/Articles/42696/Textbox-Drag-Drop-in- ...

  5. nginx 反向代理 google

    nginx的反向代理,google一直都是不容易打开的,如果你有一台位于国外的vps或者服务器,就可以轻松解决这个问题,这次的主角是nginx,nginx的反向代理现在已经发展很强大了,很多时候拿他来 ...

  6. 关于Linux下进程间使用共享内存和信号量通信的时的编译问题

    今天在编译一个使用信号量实现进程同步时,出现了库函数不存在的问题.如下图 编译结果实际上是说,没include相应的头文件,或是头文件不存在(即系统不支持该库函数) 但我man shm_open是可以 ...

  7. Java for LeetCode 044 Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  8. [Android Pro] fragment中嵌套viewpager,vierpager中有多个fragment,不显示

    referece to :  http://blog.csdn.net/mybook1122/article/details/24003343 现在好多应用流行一种布局.底部几个工具栏选项,上面也有类 ...

  9. git merge和个git rebase的区别

    http://stackoverflow.com/questions/16666089/whats-the-difference-between-git-merge-and-git-rebase/16 ...

  10. iftop安装

    安装方法1.编译安装 如果采用编译安装可以到iftop官网下载最新的源码包. 安装前需要已经安装好基本的编译所需的环境,比如make.gcc.autoconf等.安装iftop还需要安装libpcap ...