#301 (div.2) C. Ice Cave

1.题目描述:点击打开链接

2.解题思路:本题利用BFS解决。由于行走的时候有两种情况,当遇到‘X‘时会掉到下一层,当遇到’.‘时还在本层,只不过’.‘要变为‘X‘。那么直接用BFS进行搜索即可。如果遇到了’X‘,只需要判断是不是终点即可,否则跳过,如果遇到了‘.‘,那么将它改为‘X‘,并入队列即可。比赛时我一直在DFS和BFS之间徘徊不定,但其实不难发现,如果用DFS的话,可能有走不动的情况,需要往回撤。时间复杂度会比较高,因此自然会想到BFS。

3.代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

#define N 500+5
int n, m;
int s, t, e, d;
int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0, 0, 1, -1 };
char g[N][N];

typedef pair<int, int>P;
bool bfs()
{
	queue<P>q;
	q.push(P(s,t));
	g[s][t] = 'X';
	while (!q.empty())
	{
		s = q.front().first, t = q.front().second; q.pop();
		for (int i = 0; i < 4; i++)
		{
			int xx = s + dx[i];
			int yy = t + dy[i];
			if (xx < 0 || xx >= n || yy < 0 || yy >= m)continue;
			if (g[xx][yy] == 'X')
			{
				if (xx == e&&yy == d)return true;
				continue;
			}
			g[xx][yy] = 'X';
			q.push(P(xx, yy));
		}
	}
	return false;
}
int main()
{
	//freopen("t.txt", "r", stdin);
	while (~scanf("%d%d", &n, &m))
	{
		for (int i = 0; i < n; i++)
			scanf("%s", g[i]);
		scanf("%d%d%d%d", &s, &t, &e, &d);
		s--, t--, e--, d--;
		printf("%s\n", bfs() ? "YES" : "NO");
	}
	return 0;
}
时间: 2024-11-03 00:13:45

#301 (div.2) C. Ice Cave的相关文章

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

题目传送门 1 /* 2 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 3 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两点相邻, 4 那么要不就是踩一脚就破了或者踩一脚走开再走回来踩一脚破了:3. 普通的搜索看是否能到达, 5 若能还是要讨论终点踩几脚的问题:) 6 DFS 耗时大,险些超时,可以用BFS来做 7 */ 8 #include <cstdio> 9 #include <algorit

C. Ice Cave (CF #301 (Div. 2) 搜索bfs)

C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to

CF #301 504C C. Ice Cave BFS

C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to

Codeforces Round #301 (Div. 2) -- (A,B,C,D)

题目传送:Codeforces Round #301 (Div. 2) A. Combination Lock 水题,求最小移动次数,简单贪心一下即可 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #i

ICE CAVE(BFS搜索(模拟))

Description You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where y

CodeForces Round #301 Div.2

今天唯一的成果就是把上次几个人一起开房打的那场cf补一下. A. Combination Lock 此等水题看一眼样例加上那个配图我就明白题意了,可是手抽没有注释掉freopen,WA了一发. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 1000 + 10; 5 6 char s1[maxn], s2[maxn]; 7 8 int main() 9 { 10 int n; cin >>

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

题目传送门 1 /* 2 贪心水题:累加到目标数字的距离,两头找取最小值 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e3 + 10; 11 const int INF = 0x3f3f3f3f; 12 char s[MAXN],

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

题目传送门 1 /* 2 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 3 num1是输出的1的个数,numy是除此之外的数都为y,此时的numy是最少需要的,这样才可能中位数大于等于y 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 using na

ice cave

Description You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where y