CF198B Jumping on Walls (DFS+剪枝)

今天写了一道题,RE了两次,WA了两次,改了很多遍,最后一遍修改的时候找到了问题;

难度:CF B;

重点:DFS+剪枝

题目

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 nn meters. Let‘s imagine that we split these walls into 11 meter-long areas and number them with positive integers from 11 to nn 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 areas dangerous.

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 kk meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area xx of one wall, then after the jump he is located at area x+kx+k of the other wall.

If at some point of time the ninja tries to get to an area with a number larger than nn , 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.

输入输出格式

输入格式:

The first line contains two integers nn and kk ( 1<=n,k<=10^{5}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 nn characters. The ii-th character represents the state of the ii -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.

输出格式:

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

输入输出样例

输入样例#1: 复制

7 3
---X--X
-X--XX-

输出样例#1: 复制

YES

输入样例#2: 复制

6 2
--X-X-
X--XX-

输出样例#2: 复制

NO

说明

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.

DFS模型不必多说,重点是调用dfs时的顺序;

先是顺序

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 const int N=1e5+200;
 5
 6 int n,k;
 7 char a[2][N];
 8 bool b[2][N];
 9
10 bool dfs(bool side,int h,int step){//side代表左右两边墙
11     if (h>n)//jump out of canyon
12         return 1;
13     if (b[side][h]||a[side][h]==‘X‘||h<step)//h<step 从大峡谷(ノ`Д)ノ粗去了
14         return 0;
15
16     b[side][h]=1;//走过了的点
17     return dfs(side^1,h+k,step+1)||dfs(side,h-1,step+1)||dfs(side,h+1,step+1);//顺序很重要,先h+k
18 }
19 int main()
20 {
21
22     ios_base::sync_with_stdio(0);
23     cin.tie(0);
24     cout.tie(0);//为什么对这三行情有独钟呢?当然是~~~(省略)
25     cin>>n>>k;
26
27     scanf("%s %s",a[0]+1,a[1]+1);//注意这个输入方法;用于输入两行二维字符数组很合适,
28     //或者:
29     //for(int i=0;i<2;i++){
30     //  for(int j=1;j<=n;j++){
31     //      a[i][j]=getchar()==‘X‘;
32     //}
33     //for(;getchar()^‘/n‘;)
34     //}
35
36     if(dfs(0,1,1))//
37         cout<<"YES";
38     else
39         cout<<"NO";
40
41     return 0;
42 }

我们的宗旨是!dfs+思维~~

原文地址:https://www.cnblogs.com/guaguastandup/p/guaguatsandup.html

时间: 2024-08-30 18:32:59

CF198B Jumping on Walls (DFS+剪枝)的相关文章

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

UVA 10318 - Security Panel dfs 剪枝

UVA 10318 - Security Panel dfs 剪枝 ACM 题目地址:UVA 10318 - Security Panel 题意: 这题跟点灯的题目很像,点灯游戏选择一盏灯时会让它以及四周的灯改变状态. 但是我们有特殊的开开关技巧,它给出了改变状态的位置,而不是四周都改变. 问你从全部关着变成全部开着的最小开关步骤. 分析: 很明显,在一个位置上点两次或更多次是没有必要的,所以一个位置只有选择与不选择,用dfs即可,但如果暴力所有可能,复杂度是2^25,会超时,所以要剪枝. 由于

Cubes(DFS+剪枝)

题意:给一个数N,求N最少由多少个数的立方构成,并输出这些数. 做法:DFS + 剪枝,剪枝的边界很很很重要! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <stdio.h> int cub[400]; int ans[300]; int tp[300]; int n; int sum = 0x3f3f3f3f; //个数 void dfs(int le

hdu 4109 dfs+剪枝优化

求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己增加的)出发,0到1~n个节点之间的距离为1,mt[i]表示从0点到第i个节点目前所得的最长路径 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4

EOJ1981 || POJ1011 经典dfs+剪枝+奇怪的数据

题目:EOJ1981 || POJ1011   经典dfs+剪枝+奇怪的数据 Description George took sticks of the same length and cut them randomly until all partsbecame at most 50 units long. Now he wants to return sticks to the originalstate, but he forgot how many sticks he had origi

HDOJ 5113 Black And White DFS+剪枝

DFS+剪枝... 在每次DFS前,当前棋盘的格子数量的一半小于一种颜色的数量时就剪掉 Black And White Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 50 Special Judge Problem Description In mathematics,

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c

DFS + 剪枝策略

一:简介 (1)相信做过ACM的人,都很熟悉图和树的深度优先搜索:算法里面有蛮力法 -- 就是暴力搜索(不加任何剪枝的搜索): (2)蛮力搜搜需要优化时,就是需要不停的剪枝,提前减少不必要的搜索路径,提前发现判断的过滤条件: (3)剪枝的核心问题就是设计剪枝判断方法,哪些搜索路径应当舍弃,哪些搜索路径不能舍弃(保留): (4)高效的剪枝过滤条件需要从局部和全局来考虑问题,发现内在的规律. (5)详细的剪枝算法,请见剪枝算法(算法优化) 二:示例验证 (1)题目来源于 poj 1011 Stick