J题 游戏模拟
来源:互联网 发布:包装设计公司 知乎 编辑:程序博客网 时间:2024/04/28 15:39
J:Ostap and Grasshopper
//左右走次虫子
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).
Example
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.
note:
很简单的模拟题,大体看看题目和输入输出就大体知道题义了。
自己用while(1)实现的循环 不是题解上用的for 然后只过了5个样例。。。。
第6个奇怪的超时了应该是死循环了退不出来。
#include <iostream>#include <string>using namespace std;int main(){ string line; int n,t,lg,lt; bool cget=false;//判断是否能到达终点 cin>>n>>t; for(int i=0;i<n;i++){ cin>>line[i]; if(line[i]=='G') lg=i; if(line[i]=='T') lt=i; } if(lg<lt){ int pos=lg; while(1){ pos+=t; if(line[pos]=='T'){cget=true; break; } if(pos>lt||line[pos]=='#') break; } } else{ int pos=lg; while(1){ pos-=t; if(line[pos]=='T'){cget=true; break; } if(pos<lt||line[pos]=='#') break; } } if(cget) cout<<"YES"; else cout<<"NO"; return 0;}
题解代码 已AC:
#include <iostream>#include<cstdio>#include<cstring>using namespace std;int n,k;char str[110];int main(){ scanf("%d%d",&n,&k); scanf("%s",str); int s,e; for(int i=0;i<n;i++){ if(str[i]=='G') s=i; if(str[i]=='T') e=i; }//输入模块 int f=0;//标记是否到达终点 for(int i=s+k;i<n;i+=k){//如果g在t的右边 if(str[i]=='#') break; else{ if(str[i]=='T'){ f=1; break; } } } if(f) printf("YES\n"); else{//如果g在t的左边 for(int i=s-k;i>=0;i-=k) if(str[i]=='#') break; else if(str[i]=='T'){ f=1; break; } if(f) printf("YES\n"); else printf("NO\n"); } return 0;}
- J题 游戏模拟
- J - Candy Sharing Game(模拟题)
- 游戏棋-模拟题
- 模拟题 : 棋盘游戏
- 【WC模拟】J
- 问题 J: 六角形游戏
- nyoj1038纸牌游戏(模拟题)
- 【NOIP模拟题】日历游戏
- J题
- 阿里游戏市场分析师模拟题
- scu oj 4445 Right turn 2015年四川省赛J题(模拟题)
- HDU 3699 A hard Aoshu Problem 2010 Asia Fuzhou Regional Contest J题 模拟 + 搜索
- 数学+模拟-2013 ACM/ICPC Asia Regional Changsha Online J 题
- 12th浙江省省赛 J Convert QWERTY to Dvorak(细节模拟题)
- hdu5336&15年多校集训(4)j题(bfs+模拟)
- 【模拟】NEERC15 J Jump (Codeforces GYM 100851)
- 模拟赛 J跳(组合数)
- 模拟三张牌游戏
- 搜索引擎之阿堂Solr学习笔记系列1(基础篇:安装篇)
- Centos如何设置IP地址,LINUX怎么修改IP地址
- centos 安装oracle依赖包时提示"Trying other mirror."
- Python性能优化的20条建议
- slicer的插件开发:尿路结石密度分析软件
- J题 游戏模拟
- POJ 1850 Code (递推数学)
- 线程的挂起和唤醒
- MFC 获取除C盘外,其余磁盘的总空间,及空闲空间
- 剑指Offer系列-面试题3:二维数组中的查找
- 二 swift之switch condition evaluates to a constant
- Spring AOP(3)基于XML配置实现的示例
- 《PHP设计模式》之 单例模式
- 搜索引擎之阿堂Solr学习笔记系列2(基础篇:中文分词)