DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave
/*
题意:告诉起点终点,踩一次, '.'变成'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的更多相关文章
- 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 Round #301 (Div. 2) B. School Marks
题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...
- 贪心 Codeforces Round #301 (Div. 2) A. Combination Lock
题目传送门 /* 贪心水题:累加到目标数字的距离,两头找取最小值 */ #include <cstdio> #include <iostream> #include <a ...
- BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls
题目传送门 /* 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 在2*2的方格里,若只有一个是'*',那么它一定要 ...
- 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 ...
- 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 ...
- CodeForces Round #301 Div.2
今天唯一的成果就是把上次几个人一起开房打的那场cf补一下. A. Combination Lock 此等水题看一眼样例加上那个配图我就明白题意了,可是手抽没有注释掉freopen,WA了一发. #in ...
- 【解题报告】Codeforces Round #301 (Div. 2) 之ABCD
A. Combination Lock 拨密码..最少次数..密码最多有1000位. 用字符串存起来,然后每位大的减小的和小的+10减大的,再取较小值加起来就可以了... #include<st ...
- dfs + 最小公倍数 Codeforces Round #383 (Div. 2)
http://codeforces.com/contest/742/problem/C 题目大意:从x出发,从x->f[x] - > f[f[x]] -> f[f[f[x]]] -& ...
随机推荐
- poj.2419.Forests (枚举 + set用法)
Forests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5782 Accepted: 2218 Descripti ...
- show processlist 其中status详解(适用于所有概况)
mysql show processlist分析 2011-04-11 16:13:00 分类: Mysql/postgreSQL mysql> show processlist; +—–+—— ...
- Windows性能计数器
LogicalDisk\% Free Space 它测量选定逻辑磁盘上的可用空间百分比.请注意,如果此值低于 15%,则表示可用空间不足,操作系统无法存储关键文件.一个最直接的解决方案是增加更多的磁盘 ...
- [Effective JavaScript 笔记]第28条:不要信赖函数对象的toString方法
js函数有一个非凡的特性,即将其源代码重现为字符串的能力. (function(x){ return x+1 }).toString();//"function (x){ return x+ ...
- scp 命令
复制文件: (1)将本地文件拷贝到远程 scp 文件名 用户名@计算机IP或者计算机名称:远程路径 (2)从远程将文件拷回本地 ...
- 每天一个脚本解析day1==》《service xxxxx status》之service脚本解析
vim /sbin/service #!/bin/sh . /etc/init.d/functions #读取环境变量. VERSION="$(basename $0) ver. 0. ...
- sass的视频教程
http://www.w3ci.com/video/715.html http://koala-app.com/index-zh.html /***************三角形的应用******** ...
- Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- 【leetcode】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- JDBC的基本步骤
JDBC全名是Java Data Base Connectivity就是Java数据库连接,这是Java用于向数据库执行SQL语句的API,JDBC可以为多种关系型数据库提供统一的访问,而不用考虑细节 ...