博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 4452 Running Rabbits 模拟
阅读量:6501 次
发布时间:2019-06-24

本文共 4171 字,大约阅读时间需要 13 分钟。

Running Rabbits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1344    Accepted Submission(s): 925
Problem Description
Rabbit Tom and rabbit Jerry are running in a field. The field is an N×N grid. Tom starts from the up-left cell and Jerry starts from the down-right cell. The coordinate of the up-left cell is (1,1) and the coordinate of the down-right cell is (N,N)。A 4×4 field and some coordinates of its cells are shown below:
The rabbits can run in four directions (north, south, west and east) and they run at certain speed measured by cells per hour. The rabbits can't get outside of the field. If a rabbit can't run ahead any more, it will turn around and keep running. For example, in a 5×5 grid, if a rabbit is heading west with a speed of 3 cells per hour, and it is in the (3, 2) cell now, then one hour later it will get to cell (3,3) and keep heading east. For example again, if a rabbit is in the (1,3) cell and it is heading north by speed 2,then a hour latter it will get to (3,3). The rabbits start running at 0 o'clock. If two rabbits meet in the same cell at k o'clock sharp( k can be any positive integer ), Tom will change his direction into Jerry's direction, and Jerry also will change his direction into Tom's original direction. This direction changing is before the judging of whether they should turn around.
The rabbits will turn left every certain hours. For example, if Tom turns left every 2 hours, then he will turn left at 2 o'clock , 4 o'clock, 6 o'clock..etc. But if a rabbit is just about to turn left when two rabbit meet, he will forget to turn this time. Given the initial speed and directions of the two rabbits, you should figure out where are they after some time.
 
Input
There are several test cases.
For each test case:
The first line is an integer N, meaning that the field is an N×N grid( 2≤N≤20).
The second line describes the situation of Tom. It is in format "c s t"。c is a letter indicating the initial running direction of Tom, and it can be 'W','E','N' or 'S' standing for west, east, north or south. s is Tom's speed( 1≤s<n). t means that Tom should turn left every t hours( 1≤ t ≤1000).
The third line is about Jerry and it's in the same format as the second line.
The last line is an integer K meaning that you should calculate the position of Tom and Jerry at K o'clock( 1 ≤ K ≤ 200).
The input ends with N = 0.
 
Output
For each test case, print Tom's position at K o'clock in a line, and then print Jerry's position in another line. The position is described by cell coordinate.
 
Sample Input
4 E 1 1 W 1 1 2 4 E 1 1 W 2 1 5 4 E 2 2 W 3 1 5 0
 
Sample Output
2 2 3 3 2 1 2 4 3 1 4 1
题意
就是有两只喵,然后他们会在n*n的格子里面走
速度分别为s1和s2,他们分别经过t1秒和t2秒之后,会向左转
当相遇的时候,交换方向
然后问你,t秒时候,他们俩在哪儿
题解
模拟大法好!
代码

#include
int hash(char c) { if(c=='N') return 0; else if(c=='W') return 1; else if(c=='S') return 2; else return 3; } //north 0;west 1;south 2;east 3; struct P { int v,x,y,s,t; }T,J; int n; void forward(P& p) { if(p.v==0) { p.x-=p.s; if(p.x<1) {p.x=2-p.x;p.v=2;} } else if(p.v==1) { p.y-=p.s; if(p.y<1) {p.y=2-p.y;p.v=3;} } else if(p.v==2) { p.x+=p.s; if(p.x>n) {p.x=2*n-p.x;p.v=0;} } else { p.y+=p.s; if(p.y>n) {p.y=2*n-p.y;p.v=1;} } } int main() { int i,k; while(scanf("%d",&n),n) { T.x=T.y=1;J.x=J.y=n; char c; getchar();scanf("%c %d%d",&c,&T.s,&T.t);T.v=hash(c); getchar();scanf("%c %d%d",&c,&J.s,&J.t);J.v=hash(c); scanf("%d",&k); for(i=1;i<=k;i++) { forward(T);forward(J); if(T.x==J.x&&T.y==J.y) { int tmp=T.v;T.v=J.v;J.v=tmp; continue; } if(i%T.t==0)T.v=(T.v+1)%4; if(i%J.t==0)J.v=(J.v+1)%4; } printf("%d %d\n%d %d\n",T.x,T.y,J.x,J.y); } return 0; }

 

转载地址:http://rfvyo.baihongyu.com/

你可能感兴趣的文章
arm-linux-gcc /lib/libc.so.6,Cannot find /lib/libc.so.6
查看>>
linux从类型void*到类型data*的转换无效,从类型“MyStruct”到类型“void*”的强制转换无效...
查看>>
linux 英语大全对照表,Linux下推荐的常用应用程序列表(国外英语).doc
查看>>
2018秋c语言程序设计考试答案,2018秋C语言程序设计上(赵三元)-中国大学mooc-题库零氪...
查看>>
c语言从文件读取矩阵做乘积,三阶矩阵的乘法(C语言从文件中读入)
查看>>
爱因斯坦阶梯问题用while循环c语言,【C语言编程练习】5.9 爱因斯坦的阶梯问题...
查看>>
c语言软件横8,经典C语言程序设计100例(8)
查看>>
c语言结构体初始化及赋值,结构体的定义、初始化和赋值
查看>>
C语言十六进制0-9转化位十进制,C语言编程 十六进制转化成十进制
查看>>
slip协议的实现 c语言,浅析PPP和SLIP协议
查看>>
大一c语言学霸笔记图片,点开这篇推文,你离学霸只差一个笔记的距离
查看>>
Android file 封装,Android Studio封装.so文件教程
查看>>
android 页面无法点击,为什么点击不跳转到下一界面,哪位大神帮瞅瞅
查看>>
android单位转换小程序,微信小程序中rpx与rem单位转换
查看>>
android phone win10下载,Win10 Your Phone新功能:PC端运行Android应用
查看>>
js android x5,腾讯x5开源库进行Js交互
查看>>
android开发中为视频添加暂停播放器,Android - 视频启动时暂停音乐播放器
查看>>
html绝对定位重叠,HTML_firefox下绝对定位元素重叠造成不可点击问题,重构地图网站过程中碰到的,f - phpStudy...
查看>>
ps切图教程 android,PS前端切图完整教程
查看>>
html显示服务器状态,显示服务器时间并一直显示(html代码)
查看>>