codeForces 198B.Jumping on Walls(DFS+技巧)

本文介绍了一款关于悬崖逃脱的游戏算法实现,玩家控制的角色需要在不断上升的水位威胁下,利用跳跃等动作从悬崖中逃脱。文章通过DFS深度优先搜索算法详细解析了角色可能的动作路径,最终判断是否能成功逃离。

D - Jumping on Walls

  CodeForces - 198B 

Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon.

The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areasdangerous.

Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions:

  • climb one area up;
  • climb one area down;
  • jump to the opposite wall. That gets the ninja to the area that is exactly kmeters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall.

If at some point of time the ninja tries to get to an area with a number larger thann, then we can assume that the ninja got out of the canyon.

The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" — first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on.

The level is considered completed if the ninja manages to get out of the canyon.

After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 105) — the height of the canyon and the height of ninja's jump, correspondingly.

The second line contains the description of the left wall — a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area.

The third line describes the right wall in the same format.

It is guaranteed that the first area of the left wall is not dangerous.

Output

Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes).

Example
Input
7 3
---X--X
-X--XX-
Output
YES
Input
6 2
--X-X-
X--XX-
Output
NO
Note

In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon.

In the second sample there's no way the ninja can get out of the canyon.


题意:悬崖有两条路,你现在站在第一条路开始的地方,你要离开这个悬崖,X不能走,-能走,你有三种移动方式,在同一条路向上向下走一步(在图中意思就是在同一行你可以往前走一步或者后退一步),或者是跳到对面那一条路上想对你所在位置往上三个位置的地方,同时你没移动一次水位就会上涨一个位置,你浸入水中就会死;问你能不能跳出这个悬崖;

思路:DFS;每次判断这三种状态如果可行就继续深搜,判断是否能够跳出这个图就行了;一开始一直错,后来参考一位学长的做法才过了;

#include <bits/stdc++.h>  
using namespace std;  
const int maxn=1e5+100;  
int n,m,water;  
char a[2][maxn];  
int v[2][maxn];  
int dfs(int pos,int j)
{  
    if(j>n)
	{
		return 1;
	}  
    if(a[pos][j]=='X'||j<water||v[pos][j])
	{
		return 0; 
	}
    v[pos][j]=1;  
    water++;
    int f=dfs(pos,j-1)||dfs(1-pos,j+m)||dfs(pos,j+1);  
    water--;
    return f;  
}  
int main()
{  
	while(~scanf("%d %d",&n,&m))
	{
		water=1; 
        scanf("%s %s",a[0]+1,a[1]+1);  
        if(dfs(0,1))
		{
			printf("YES\n");	
		}
        else
		{
			printf("NO\n");  
		}
	}    
    return 0;  
} 



Codeforces Round 1036 是一场同时面向 Div.1 和 Div.2 参赛者的比赛,通常这类比赛会包含多个具有挑战性的编程题目,涵盖算法、数据结构、数学等多个领域。比赛的题解和题目信息可以帮助参赛者回顾解题思路,提升编程能力。 ### 比赛基本信息 - **比赛名称**:Codeforces Round #1036 (Div. 1 and Div. 2) - **比赛时间**:具体时间为 UTC+X(根据实际举办日期和时间表) - **比赛链接**:[Codeforces 官方页面](https://codeforces.com/contest/1343) - **题解发布位置**:通常在比赛结束后不久,官方或社区成员会在 Codeforces 博客、GitHub 或其他技术平台上发布题解。 ### 题目类型与难度分布 该轮比赛通常包括 5 到 7 道题目,难度从简单实现到复杂算法不等。例如: - **A题**:通常是简单的模拟或数学问题。 - **B题**:可能涉及字符串处理或基础贪心策略。 - **C题**:中等难度,可能需要掌握基本的数据结构如数组、排序等。 - **D题及以后**:较高难度,可能涉及图论、动态规划、数论等高级算法。 ### 参赛情况与亮点 - **参与人数**:通常超过 10,000 名选手参加。 - **热门话题**:比赛中某些题目可能会引发广泛讨论,尤其是那些需要用到巧妙构造或优化技巧的问题。 - **知名选手表现**:顶尖选手如 tourist、Um_nik 等通常会以极快的速度完成所有题目,并占据排行榜前列。 ### 示例代码片段 以下是一个典型的 Codeforces 题目解法示例,适用于某道中等难度题目: ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { long long l, r; cin >> l >> r; // 假设 e 是一个预处理好的符合条件的数组 // 使用二分查找来统计区间 [l, r] 内的有效数字个数 long long ans = upper_bound(e.begin(), e.end(), r) - lower_bound(e.begin(), e.end(), l); cout << ans << endl; } return 0; } ``` ### 题解资源推荐 - **Codeforces 官方博客**:通常会有详细的题解和作者说明。 - **GitHub 仓库**:许多参赛者会将自己的解法上传至 GitHub,便于他人学习。 - **知乎专栏 / 优快云 / 博客园**:中文社区中也常有高质量的赛后总结与分析文章。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值