Codeforces Round #382 (Div. 2) A. Ostap and Grasshopper
2016-12-05 13:23
393 查看
A. Ostap and Grasshopper
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
On the way to Rio de Janeiro Ostap kills time playing with a grasshopper he took with him in a special box. Ostap builds a line of length
n such that some cells of this line are empty and some contain obstacles. Then, he places his grasshopper to one of the empty cells and a small insect in another empty cell. The grasshopper wants to eat the insect.
Ostap knows that grasshopper is able to jump to any empty cell that is
exactly k cells away from the current (to the left or to the right). Note that it doesn't matter whether intermediate cells are empty or not as the grasshopper makes a jump over them. For example, if
k = 1 the grasshopper can jump to a neighboring cell only, and if
k = 2 the grasshopper can jump over a single cell.
Your goal is to determine whether there is a sequence of jumps such that grasshopper will get from his initial position to the cell with an insect.
Input
The first line of the input contains two integers n and
k (2 ≤ n ≤ 100,
1 ≤ k ≤ n - 1) — the number of cells in the line and the length of one grasshopper's jump.
The second line contains a string of length n consisting of characters '.', '#', 'G' and 'T'.
Character '.' means that the corresponding cell is empty, character '#' means that the corresponding cell contains an obstacle and grasshopper can't jump there. Character 'G'
means that the grasshopper starts at this position and, finally, 'T' means that the target insect is located at this cell. It's guaranteed that characters 'G' and 'T'
appear in this line exactly once.
Output
If there exists a sequence of jumps (each jump of length
k), such that the grasshopper can get from his initial position to the cell with the insect, print "YES" (without quotes) in the only line of the input. Otherwise, print "NO"
(without quotes).
Examples
Input
Output
Input
Output
Input
Output
Input
Output
Note
In the first sample, the grasshopper can make one jump to the right in order to get from cell
2 to cell 4.
In the second sample, the grasshopper is only able to jump to neighboring cells but the way to the insect is free — he can get there by jumping left
5 times.
In the third sample, the grasshopper can't make a single jump.
In the fourth sample, the grasshopper can only jump to the cells with odd indices, thus he won't be able to reach the insect.
//简单题,一水就过了,直接上代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
char s[110];
int main(){
int n, k;
scanf("%d %d", &n, &k);
getchar();
scanf("%s", s);
int x, y;
for (int i = 0; i < n; i++){
if (s[i] == 'G'){
x = i;
}
if (s[i] == 'T'){
y = i;
}
}
if (abs(x - y) % k == 0){
if (x > y){
int flag = 1;
for (int i = x - k; i >= y; i -= k){
if (s[i] == '#'){
flag = 0;
break;
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
}
else{
int flag = 1;
for (int i = x + k; i <= y; i += k){
if (s[i] == '#'){
flag = 0;
break;
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
}
}
else{
printf("NO\n");
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
On the way to Rio de Janeiro Ostap kills time playing with a grasshopper he took with him in a special box. Ostap builds a line of length
n such that some cells of this line are empty and some contain obstacles. Then, he places his grasshopper to one of the empty cells and a small insect in another empty cell. The grasshopper wants to eat the insect.
Ostap knows that grasshopper is able to jump to any empty cell that is
exactly k cells away from the current (to the left or to the right). Note that it doesn't matter whether intermediate cells are empty or not as the grasshopper makes a jump over them. For example, if
k = 1 the grasshopper can jump to a neighboring cell only, and if
k = 2 the grasshopper can jump over a single cell.
Your goal is to determine whether there is a sequence of jumps such that grasshopper will get from his initial position to the cell with an insect.
Input
The first line of the input contains two integers n and
k (2 ≤ n ≤ 100,
1 ≤ k ≤ n - 1) — the number of cells in the line and the length of one grasshopper's jump.
The second line contains a string of length n consisting of characters '.', '#', 'G' and 'T'.
Character '.' means that the corresponding cell is empty, character '#' means that the corresponding cell contains an obstacle and grasshopper can't jump there. Character 'G'
means that the grasshopper starts at this position and, finally, 'T' means that the target insect is located at this cell. It's guaranteed that characters 'G' and 'T'
appear in this line exactly once.
Output
If there exists a sequence of jumps (each jump of length
k), such that the grasshopper can get from his initial position to the cell with the insect, print "YES" (without quotes) in the only line of the input. Otherwise, print "NO"
(without quotes).
Examples
Input
5 2 #G#T#
Output
YES
Input
6 1 T....G
Output
YES
Input
7 3 T..#..G
Output
NO
Input
6 2 ..GT..
Output
NO
Note
In the first sample, the grasshopper can make one jump to the right in order to get from cell
2 to cell 4.
In the second sample, the grasshopper is only able to jump to neighboring cells but the way to the insect is free — he can get there by jumping left
5 times.
In the third sample, the grasshopper can't make a single jump.
In the fourth sample, the grasshopper can only jump to the cells with odd indices, thus he won't be able to reach the insect.
//简单题,一水就过了,直接上代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
char s[110];
int main(){
int n, k;
scanf("%d %d", &n, &k);
getchar();
scanf("%s", s);
int x, y;
for (int i = 0; i < n; i++){
if (s[i] == 'G'){
x = i;
}
if (s[i] == 'T'){
y = i;
}
}
if (abs(x - y) % k == 0){
if (x > y){
int flag = 1;
for (int i = x - k; i >= y; i -= k){
if (s[i] == '#'){
flag = 0;
break;
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
}
else{
int flag = 1;
for (int i = x + k; i <= y; i += k){
if (s[i] == '#'){
flag = 0;
break;
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
}
}
else{
printf("NO\n");
}
return 0;
}
相关文章推荐
- Codeforces Round #382 (Div. 2) A. Ostap and Grasshopper
- Codeforces Round #382 (Div. 2) A. Ostap and Grasshopper
- Codeforces Round #382 (Div. 2) A. Ostap and Grasshopper bfs
- Codeforces Round #382 (Div. 2) 735A - Ostap and Grasshopper
- CodeForces 735A - Ostap and Grasshopper(思维)
- Codeforces Round #382 (Div. 2) E. Ostap and Tree
- F - Ostap and Grasshopper模拟
- CF Round #348 Div. 2 A. Little Artem and Present B. Little Artem and Grasshopper C. Little Artem and
- codeforces 735 A. Ostap and Grasshopper (简单题)
- Codeforces Round #378 (Div. 2) A. Grasshopper And the String
- Codeforces Round #382 (Div. 1) C. Ostap and Tree(树形DP)
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition)-B. Little Artem and Grasshopper(模拟)
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) B. Little Artem and Grasshopper 模拟题
- Ostap and Grasshopper
- codeforces 735 A Ostap and Grasshopper
- Codeforces Round #378 (Div. 2) A. Grasshopper And the String(水题求跳跃距离)
- Codeforces Round #382 (Div. 2)A到D题 A. Ostap and //B. Urbanization//C. Tennis Championship//D. Taxes
- A. Ostap and Grasshopper
- Codeforces Round #378 (Div. 2)A.Grasshopper And the String【水题】模拟
- CodeForces 735 A.Ostap and Grasshopper(水~)