程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 4452 Running Rabbits (模擬—12年金華賽區現場賽K題)

hdu 4452 Running Rabbits (模擬—12年金華賽區現場賽K題)

編輯:C++入門知識

Running Rabbits
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 607    Accepted Submission(s): 432

 

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
2012 Asia JinHua Regional Contest


Recommend
zhuyuanchen520
 
題意:兩只兔子。給定一個N*N的格子。初始時兔子1在(1,1),兔子2在(n,n)。兔子每隔一段時間T左轉。
           1、如果兩只兔子剛好跳到同一格而兔子到了該轉彎的時間,則不按規定左轉,而是兩只兔子互換跳動方向。
           2、如果兔子碰到牆不能繼續前進,則轉換為相反的方向。例如:南就變北,東就變西。
           輸入包括:第一行,n,表示格子的規模。
                             第二行,兔子1的初始運動方向,S=速度(每小時走幾格),T=每隔多長時間轉換方向.
                             第三行,兔子2.。。。
                             第四行,TIME(問TIME後兩只兔子所在的位置)
分析:1、用while循環沒到TIME就一直按規定running和change direction。寫兩個函數go()和change().每次以小時為單位,分開操作。
           走完後就判斷是不是碰到另一只兔子,判斷是否到了轉彎的時間。
           2、到了TIME,就輸出坐標。
           3、go()函數:用for循環,每次走一步,直到把一小時可以走的步數走完。如果碰到牆不能走,則變為反方向運動。用tim1、tim2分別計時。
                 這樣就可以在主函數中用取余操作判斷是否到了該轉彎的時間。參數都傳引用。
           4、change()是單純的按規定左轉操作函數。
感想:模擬啊模擬。我哭。。。
           其實都分析到了,只是我有個地方搞錯了,我想的是每次走一步,分開操作,就判斷是否兩只兔子相碰,碰則交換運動方向;
           判斷是否撞到牆不能走了。然後又要顧及到有沒有到規定的轉動時間,還有顧及到有沒有到TIME。然後就凌亂了。
           基礎不好,太弱了。。。囧。。。!!!!我這個邏輯拙計的孩子。。。
           唯一讓我得意的就是想到宏定義E、W、S、N作為下標方便坐標變換,這個很方便。
 
代碼:

#include<cstdio>
#include<iostream>
#include<cstring>
#define N 0
#define W 1
#define S 2
#define E 3
using namespace std;

int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int n;

void change(char &dir)
{
    if(dir=='E')
        dir='N';
    else if(dir=='N')
        dir='W';
    else if(dir=='W')
        dir='S';
    else
        dir='E';
}

void go(int &x,int &y,int &time,char &dir,int s)
{
    for(int i=0;i<s;i++)
    {
        if(dir=='E')
        {
            if(y==n)
            {
                dir='W';
                y=y+dy[W];
            }
            else
                y=y+dy[E];
        }
        else if(dir=='W')
        {
            if(y==1)
            {
                dir='E';
                y=y+dy[E];
            }
            else
                y=y+dy[W];
        }
        else if(dir=='N')
        {
            if(x==1)
            {
                dir='S';
                x+=dx[S];
            }
            else
                x+=dx[N];
        }
        else if(dir=='S')
        {
            if(x==n)
            {
                dir='N';
                x+=dx[N];
            }
            else
                x+=dx[S];
        }
        //printf("test: %d %d\n",x,y);
    }
    time++;
}

int main()
{
    char c1,c2,tmp;
    int tim1,tim2,k,t1,t2,x1,y1,x2,y2,s1,s2;
    while(scanf("%d",&n)&&n)
    {
        getchar();
        scanf("%c%d%d",&c1,&s1,&t1);
        getchar();
        scanf("%c%d%d",&c2,&s2,&t2);
        scanf("%d",&k);
        x1=1,y1=1,x2=n,y2=n;
        tim1=0,tim2=0;
        while(k--)
        {
            go(x1,y1,tim1,c1,s1);
            go(x2,y2,tim2,c2,s2);
            if(x1==x2&&y1==y2)
            {
                tmp=c1;
                c1=c2;
                c2=tmp;
            }
            else
            {
                if(tim1%t1==0)
                    change(c1);
                if(tim2%t2==0)
                    change(c2);
            }
        }
        printf("%d %d\n%d %d\n",x1,y1,x2,y2);
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved