Codeforces-199D:Jumping on Walls(DFS+思维)
2018-01-05 11:03
519 查看
D. Jumping on Walls
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 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 k meters 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 than n, 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).
Examples
input
output
input
output
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.
思路:因为每个格子最多经过一次,直接DFS从起点开始遍历。但是要注意DFS的顺序:由于有洪水的限制,所以应该优先选择“jump to the opposite wall”这个操作,这样就能保证在洪水淹没之前到达目标位置。
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6;
char a[MAX],b[MAX];
int v[2][MAX];
int n,m;
int dfs(int tag,int k,int h)
{
if(k<=h||k<=0)return 0;
if(k>n)return 1;
if(tag==0&&a[k]=='X')return 0;
if(tag==1&&b[k]=='X')return 0;
if(v[tag][k])return 0;
v[tag][k]=1;
if(dfs(tag^1,k+m,h+1))return 1;//优先选择"jump to the opposite wall"
if(dfs(tag,k+1,h+1))return 1;
if(dfs(tag,k-1,h+1))return 1;
return 0;
}
int main()
{
cin>>n>>m;
scanf("%s%s",a+1,b+1);
memset(v,0,sizeof v);
puts(dfs(0,1,0)?"YES":"NO");
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 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 k meters 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 than n, 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).
Examples
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.
思路:因为每个格子最多经过一次,直接DFS从起点开始遍历。但是要注意DFS的顺序:由于有洪水的限制,所以应该优先选择“jump to the opposite wall”这个操作,这样就能保证在洪水淹没之前到达目标位置。
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6;
char a[MAX],b[MAX];
int v[2][MAX];
int n,m;
int dfs(int tag,int k,int h)
{
if(k<=h||k<=0)return 0;
if(k>n)return 1;
if(tag==0&&a[k]=='X')return 0;
if(tag==1&&b[k]=='X')return 0;
if(v[tag][k])return 0;
v[tag][k]=1;
if(dfs(tag^1,k+m,h+1))return 1;//优先选择"jump to the opposite wall"
if(dfs(tag,k+1,h+1))return 1;
if(dfs(tag,k-1,h+1))return 1;
return 0;
}
int main()
{
cin>>n>>m;
scanf("%s%s",a+1,b+1);
memset(v,0,sizeof v);
puts(dfs(0,1,0)?"YES":"NO");
return 0;
}
相关文章推荐
- 【CodeForces 525D】【dfs+思维】 Arthur and Walls 【只包含点和星的矩阵,需要将部分星变成点使点能组成矩形 】
- Codeforces 29D Ant on the Tree 树的遍历 dfs序
- codeforces-Fox And Two Dots【DFS】(思维)
- Codeforces 711D Directed Roads【Dfs+思维】
- Codeforces 638C Road Improvement【思维+Dfs】
- Codeforces 681D Gifts by the List【思维+Dfs】
- Codeforces 659E New Reform【思维+Dfs】
- Codeforces 732F Tourist Reform【思维+边双联通+Dfs处理后继问题】好题!
- Jumping on Walls
- Codeforces 618D Hamiltonian Spanning Tree【思维+Dfs】
- Codeforces 525D Arthur and Walls 【Dfs+思维】
- Codeforces 747D Persistent Bookcase【Dfs+思维】好题!好题!
- Codeforces 456 div2 A B ***D(思维题) ***E(二分/DFS/数论)
- Codeforces 813C The Tag Game【思维+Dfs】
- CodeForces - 841D Leha and another game about graph 构造(dfs + 思维)
- Codeforces 682C Alyona and the Tree【Dfs+YY思维】好题
- codeforces 395 D <思维,DFS想了半天超时>
- Codeforces 782E Underground Lab【极限思维+Dfs】
- Codeforces--525D--Arthur and Walls(DFS)
- Codeforces 622C Not Equal on a Segment【思维预处理+二分】