Problem:Running Rabbits
Description:题目意思是说有两个兔子,一个起始位于(1,1)位置,另一个兔子起始位于(N,N)位置。给出兔子开始跳的时候的方向,速度(每小时s格),还有每隔t小时向左转一次方向。两只兔子相遇时会交换方向,问第k个时刻两只兔子分别的位置。
Solution:模拟题。根据题目意思判断好方向的转化就好了。
Code(C++):
#include <iostream>
using namespace std;
struct point
{
int x;
int y;
};
int main()
{
int n,s1,t1,s2,t2,t,k1,k2;
point p[4];
point T,J;
p[0].x=-1,p[0].y=0;
p[1].x=0,p[1].y=1;
p[2].x=1,p[2].y=0;
p[3].x=0,p[3].y=-1;
char c1,c2;
while(cin>>n,n)
{
point now1,now2;
T.x=1,T.y=1;
J.x=n,J.y=n;
cin>>c1>>s1>>t1;
cin>>c2>>s2>>t2;
cin>>t;
if(c1=='N')
k1=0;
if(c1=='E')
k1=1;
if(c1=='S')
k1=2;
if(c1=='W')
k1=3;
if(c2=='N')
k2=0;
if(c2=='E')
k2=1;
if(c2=='S')
k2=2;
if(c2=='W')
k2=3;
now1=T;
now2=J;
for(int tt=1; tt<=t; tt++)
{
for(int j=0; j<s1; j++)
{
now1.x=now1.x+p[k1].x;
now1.y=now1.y+p[k1].y;
if(now1.x<1||now1.x>n||now1.y<1||now1.y>n)
{
now1.x=now1.x-p[k1].x;
now1.y=now1.y-p[k1].y;
k1=(k1+2)%4;
now1.x=now1.x+p[k1].x;
now1.y=now1.y+p[k1].y;
}
}
for(int j=0; j<s2; j++)
{
now2.x=now2.x+p[k2].x;
now2.y=now2.y+p[k2].y;
if(now2.x<1||now2.x>n||now2.y<1||now2.y>n)
{
now2.x=now2.x-p[k2].x;
now2.y=now2.y-p[k2].y;
k2=(k2+2)%4;
now2.x=now2.x+p[k2].x;
now2.y=now2.y+p[k2].y;
}
}
if(now1.x==now2.x&&now1.y==now2.y)
{
int tmp=k1;
k1=k2;
k2=tmp;
continue;
}
if(tt%t1==0)
k1=(k1+3)%4;
if(tt%t2==0)
k2=(k2+3)%4;
}
cout<<now1.x<<" "<<now1.y<<endl;
cout<<now2.x<<" "<<now2.y<<endl;
}
return 0;
}