hdu 4452 Running Rabbits
来源:互联网 发布:中班美工交通标志教案 编辑:程序博客网 时间:2024/05/22 04:28
Running Rabbits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 976 Accepted Submission(s): 695
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.
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.
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
4E 1 1W 1 124E 1 1W 2 154E 2 2W 3 150
Sample Output
2 23 32 12 43 14 1
简单搜索。。
#include <stdio.h>#include <string.h>#include <iostream>#include <string>#include <vector>#include <map>#include <queue>#include <stack>#include <math.h>#include <algorithm>using namespace std;typedef long long ll;#define rep(i,s,t) for(int i=s;i<t;i++)#define red(i,s,t) for(int i=s-1;i>=t;i--)#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)#define clr(a,v) memset(a,v,sizeof a)#define max(a,b) (a<b?b:a)#define min(a,b) (a<b?a:b)#define abs(a) ((a)<0?-(a):(a))#define L t<<1#define R t<<1|1#define MID int mid=(l+r)>>1inline int input(){ int ret=0;char c=getchar(); while(c<'0' || c>'9'){ c=getchar(); } while(c>='0' && c<='9'){ ret=ret*10+c-'0'; c=getchar(); } return ret;}inline void output(int x){ if(x<0){ putchar('-');x=-x; } int len=0,data[10]; while(x){ data[len++]=x%10;x/=10; } if(!len) data[len++]=0; while(len--) putchar(data[len]+48); putchar('\n');}const int MAXN=25;const int dx[]={1,-1,0,0};const int dy[]={0,0,1,-1};int n;char d[2][2];int s1,t1,s2,t2,d1,d2;int k;inline int get(char c){ if(c=='E') return 2; if(c=='W') return 3; if(c=='N') return 1; return 0;}inline int turn(int a){ if(a==0) return 2; if(a==1) return 3; if(a==2) return 1; return 0;}inline int back(int a){ if(a==0) return 1; if(a==1) return 0; if(a==2) return 3; return 2;}inline bool ok(int x,int y){ return (x>=1 && x<=n && y>=1 && y<=n);}struct node{ int x1,x2,y1,y2; int t;};inline void getNext(int &a,int &b,int x,int y,int &d,int v){ rep(i,0,v){ int aa=x+dx[d],bb=y+dy[d]; if(ok(aa,bb)) {x=aa,y=bb;} else{ d=back(d); x+=dx[d]; y+=dy[d]; } } a=x,b=y;}inline void bfs(){ queue<node>q; node vn,vw; vn.x1=vn.y1=1; vn.x2=vn.y2=n; vn.t=0; q.push(vn); while(!q.empty()){ vn=q.front();q.pop(); if(vn.t==k) break; if(vn.x1==vn.x2 && vn.y1==vn.y2){ swap(d1,d2); } else{ if(vn.t && vn.t%t1==0){ d1=turn(d1); } if(vn.t && vn.t%t2==0){ d2=turn(d2); } } int a,b; getNext(a,b,vn.x1,vn.y1,d1,s1); vw.x1=a,vw.y1=b; getNext(a,b,vn.x2,vn.y2,d2,s2); vw.x2=a,vw.y2=b; vw.t=vn.t+1; q.push(vw); } printf("%d %d\n%d %d\n",vn.x1,vn.y1,vn.x2,vn.y2);}int main(){ while(n=input(), n){ scanf("%s%d%d%s%d%d%d",d[0],&s1,&t1,d[1],&s2,&t2,&k); d1=get(d[0][0]),d2=get(d[1][0]); bfs(); } return 0;}
0 0
- hdu 4452 Running Rabbits
- hdu 4452 Running Rabbits
- hdu 4452 Running Rabbits
- hdu - 4452 - Running Rabbits
- hdu 4452 Running Rabbits
- hdu 4452 Running Rabbits
- HDU 4452 Running Rabbits
- hdu 4452 Running Rabbits
- HDU 4452 Running Rabbits
- HDU 4452 Running Rabbits
- HDU 4452 Running Rabbits 【模拟】
- [模拟] hdu 4452 Running Rabbits
- HDU 4452 Running Rabbits [模拟]
- [hdu 4452] Running Rabbits 模拟
- hdu 4452 Running Rabbits (模拟)
- Hdu 4452 Running Rabbits 大模拟
- hdu 4452 Running Rabbits (简单模拟)
- hdu 4452 Running Rabbits(模拟水题)
- Android app widget中实现跑马灯效果
- 深拷贝和浅拷贝的理解?
- 关于mysql删除唯一约束的问题(求解答)
- 458 - The Decoder
- java中String、StringBuffer和StringBuilder的区别
- hdu 4452 Running Rabbits
- APP测试流程~开启博客之旅
- 第一章 LINUX 常用命令
- poj 4044 Score Sequence(暴力)
- 微软面试100题2010年版全部答案集锦(含下载地址)
- opencv Error:Bad argument <Unknown arrray type> in cvarrTomat
- H.264与MPEG4区别
- C# 窗体间传递数据
- easyui easyui-datebox日期选择