码迷,mamicode.com
首页 > 其他好文 > 详细

CF198B Jumping on Walls (DFS+剪枝)

时间:2019-01-30 00:17:40      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:bsp   tchar   gets   sam   locate   a star   c++   put   lin   

今天写了一道题,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+思维~~

CF198B Jumping on Walls (DFS+剪枝)

标签:bsp   tchar   gets   sam   locate   a star   c++   put   lin   

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

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!