现在的位置: 首页 > 综合 > 正文

hdu4452

2013年09月10日 ⁄ 综合 ⁄ 共 4110字 ⁄ 字号 评论关闭

Running Rabbits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 595    Accepted Submission(s): 422


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
 


Source
很坑的一题虽然是模拟,但是有很多小细节要注意,比如,相遇的时候不要用左转,边界是1和n,开始那一秒不用左转!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
int main()
{
   int n,tc,ts,tt,jc,js,jt,tpx,tpy,jpx,jpy,i,k;
   int left[4],back[4],right[4];
   char c;
   left[0]=3;left[1]=0;left[2]=1;left[3]=2;
   back[0]=2;back[1]=3;back[2]=0;back[3]=1;
   right[0]=1;right[1]=2;right[2]=3;right[3]=0;
   while(scanf("%d",&n)!=EOF)
   {
       if(n==0)
       break;
       getchar();
       scanf("%c%d%d",&c,&ts,&tt);
       //printf("%c",c);
       if(c=='E')
       {
           tc=1;
       }
       else if(c=='N')
       {
           tc=2;
       }
       else if(c=='S')
       {
           tc=2;
       }
       else if(c=='W')
       {
           tc=1;
       }
       getchar();
       scanf("%c%d%d",&c,&js,&jt);
       //printf("%c",c);
       if(c=='E')
       {
           jc=3;
       }
       else if(c=='N')
       {
           jc=0;
       }
       else if(c=='S')
       {
           jc=0;
       }
       else if(c=='W')
       {
           jc=3;
       }
       tpx=1;tpy=1;
       jpx=n;jpy=n;
       scanf("%d",&k);
       for(i=1;i<=k;i++)
       {
        //printf("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          %d %d %d %d %d %d\n",tpx,tpy,tc,jpx,jpy,jc);
          tpx+=ts*dir[tc][1];
          tpy+=ts*dir[tc][0];
          if(tc==0)
          {
             if(tpx<=0)
             {
                 tc=back[tc];
                 tpx=2-tpx;
             }
          }
          else if(tc==1)
          {
              if(tpy>n)
              {
                  tpy%=n;
                  tc=back[tc];
                  tpy=n-tpy;
              }
          }
          else if(tc==2)
          {
              if(tpx>n)
              {
                  tpx%=n;
                  tc=back[tc];
                  tpx=n-tpx;
              }
          }
          else if(tc==3)
          {
              if(tpy<=0)
              {
                  tc=back[tc];
                  tpy=2-tpy;
              }
          }

          jpx+=js*dir[jc][1];
          jpy+=js*dir[jc][0];
          if(jc==0)
          {
             if(jpx<=0)
             {
                 jc=back[jc];
                 jpx=2-jpx;
             }
          }
          else if(jc==1)
          {
              if(jpy>n)
              {
                  jpy%=n;
                  jc=back[jc];
                  jpy=n-jpy;
              }
          }
          else if(jc==2)
          {
              if(jpx>n)
              {
                  jpx%=n;
                  jc=back[jc];
                  jpx=n-jpx;
              }
          }
          else if(jc==3)
          {
              if(jpy<=0)
              {
                  jc=back[jc];
                  jpy=2-jpy;
              }
          }

          if(tpx==jpx&&tpy==jpy)
          {
              int temp;
              temp=tc;
              tc=jc;
              jc=temp;
              continue;
          }

            if(i%tt==0)
          {
             // tc=left[tc];
            if(tc==0)
          {
             if(tpy==1)
             {
                 tc=right[tc];

             }
             else
             {
                tc=left[tc];
             }
          }
          else if(tc==1)
          {
              if(tpx==1)
              {
                 tc=right[tc];
              }
              else
              {
                tc=left[tc];
              }
          }
          else if(tc==2)
          {
              if(tpy==n)
              {
                  tc=right[tc];
              }
              else
              {
                tc=left[tc];
              }
          }
          else if(tc==3)
          {
              if(tpx==n)
              {
                  tc=right[tc];

              }
              else
              {
                tc=left[tc];
              }
          }


          }
            if(i%jt==0)
          {

            if(jc==0)
          {
             if(jpy==1)
             {
                 jc=right[jc];

             }
             else
             {
                jc=left[jc];
             }
          }
          else if(jc==1)
          {
              if(jpx==1)
              {
                 jc=right[jc];
              }
              else
              {
                jc=left[jc];
              }
          }
          else if(jc==2)
          {
              if(jpy==n)
              {
                  jc=right[jc];
              }
              else
              {
                jc=left[jc];
              }
          }
          else if(jc==3)
          {
              if(jpx==n)
              {
                  jc=right[jc];

              }
              else
              {
                jc=left[jc];
              }
          }
         // jc=left[jc];
          }
       }
       printf("%d %d\n%d %d\n",tpx,tpy,jpx,jpy);
   }
    return 0;
}

抱歉!评论已关闭.