The Last Ant(模擬)

題目鏈接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1336

Problem B: The Last Ant

A straight tunnel without branches is crowded with busy ants coming and going. Some ants walk left to right and others right to left. All ants walk at a constant speed of 1 cm/s. When two ants meet, they try to pass each other. However, some sections of the tunnel are narrow and two ants cannot pass each other. When two ants meet at a narrow section, they turn around and start walking in the opposite directions. When an ant reaches either end of the tunnel, it leaves the tunnel.

The tunnel has an integer length in centimeters. Every narrow section of the tunnel is integer centimeters distant from the both ends. Except for these sections, the tunnel is wide enough for ants to pass each other. All ants start walking at distinct narrow sections. No ants will newly enter the tunnel. Consequently, all the ants in the tunnel will eventually leave it. Your task is to write a program that tells which is the last ant to leave the tunnel and when it will.

Figure B.1 shows the movements of the ants during the first two seconds in a tunnel 6 centimeters long. Initially, three ants, numbered 1, 2, and 3, start walking at narrow sections, 1, 2, and 5 centimeters distant from the left end, respectively. After 0.5 seconds, the ants 1 and 2 meet at a wide section, and they pass each other. Two seconds after the start, the ants 1 and 3 meet at a narrow section, and they turn around.

Figure B.1 corresponds to the first dataset of the sample input.


Figure B.1. Movements of ants

Input

The input consists of one or more datasets. Each dataset is formatted as follows.

n l
d1 p1
d2 p2
...
dn pn

The first line of a dataset contains two integers separated by a space. n (1 ≤ n ≤ 20) represents the number of ants, and l (n + 1 ≤ l ≤ 100) represents the length of the tunnel in centimeters. The following n lines describe the initial states of ants. Each of the lines has two items, di and pi, separated by a space. Ants are given numbers 1 through n. The ant numbered i has the initial direction di and the initial position pi. The initial direction di (1 ≤ i ≤ n) is L (to the left) or R (to the right). The initial position pi (1 ≤ i ≤ n) is an integer specifying the distance from the left end of the tunnel in centimeters. Ants are listed in the left to right order, that is, 1 ≤ p1 < p2 < ... < pn ≤ l - 1.

The last dataset is followed by a line containing two zeros separated by a space.

Output

For each dataset, output how many seconds it will take before all the ants leave the tunnel, and which of the ants will be the last. The last ant is identified by its number. If two ants will leave at the same time, output the number indicating the ant that will leave through the left end of the tunnel.

Sample Input

3 6
R 1
L 2
L 5
1 10
R 1
2 10
R 5
L 7
2 10
R 3
L 8
2 99
R 1
L 98
4 10
L 1
R 2
L 8
R 9
6 10
R 2
R 3
L 4
R 6
L 7
L 8
0 0

Output for the Sample Input

5 1
9 1
7 1
8 2
98 2
8 2
8 3
題目大意:
有n只螞蟻在一根木棍上(螞蟻的編號從1~n),木棍是不是規則的,有的地方粗細不一致(詳細請看圖),每隻螞蟻方向朝左或者朝右,速度恆定1cm/s。在活動過程中,有可能會有兩隻螞蟻相撞,如果他們相撞的地點位於木棍比較粗的地方,那麼他們則不會改變方向,仍然向前行動。如果他們相撞的地點位於比較細的地方,則他們會立刻轉向(改變方向的時間忽略不計)。現在要求,最後一隻螞蟻從木棍上掉下來的時刻是多少,還要輸出是第幾只螞蟻。如果最後有幾隻螞蟻同時掉落,則輸出在木棍左邊掉落的那隻編號最小的螞蟻。
解題思路:
看到這個題目,想到的就是劉汝佳的訓練指南(白書)上的有一個關於螞蟻的題目(忘了是哪頁,反正應該就在前幾頁)。裏面介紹了求螞蟻運動的時間,即最後一隻螞蟻出來的時間是多少。詳細請看白書。當求出時間之後,就可以根據時間來對螞蟻的運動狀態進行更新了,純粹的模擬(但是其中有幾個地方需要注意)看下面的代碼吧。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;

struct point
{
    int loc;//螞蟻的座標
    int dir;//螞蟻的方向
}p[50];
int n,len;
int num[50];
int t[50];//原本是寫成time[50],我在AIZU上面交了幾次,返回CE。後來才知道oj上面顯示這個跟time.h頭文件衝突(我又沒寫這個頭文件.....有點不懂),之後改了過來
int main()
{
    while(scanf("%d%d",&n,&len)&&n&&len)//n代表螞蟻的數目,len代表木棍的長度
    {
        for(int i=1;i<=n;i++)
        {
            getchar();//因爲%c輸入會喫掉回車符
            char a;
            int b;
            scanf("%c %d",&a,&b);
            p[i].loc=b;
            if(a=='R')
                p[i].dir=1;
            else
                p[i].dir=0;
            if(p[i].dir)//根據白書上的思路,先把時間求出來
                t[i]=len-p[i].loc;
            else
                t[i]=p[i].loc;
        }
        sort(t+1,t+1+n);//排序得到的t[n],就是最後一個螞蟻出來的時間了
        for(int i=1;i<=t[n];i++)
        {
            for(int j=1;j<=n;j++)//每一秒鐘,更新一次螞蟻的狀態(座標和方向)
            {
                if(p[j].dir)
                {
                    p[j].loc++;
                }
                else
                {
                    p[j].loc--;
                }
            }
            for(int j=1;j<n;j++)//這個處理要注意
            {
                for(int k=j+1;k<=n;k++)
                {
                    if(p[j].loc==p[k].loc)//判斷如果有兩隻螞蟻的位置一樣,說明他們相撞了,那麼就改變他們的方向
                    {
                        p[j].dir=!p[j].dir;
                        p[k].dir=!p[k].dir;
                    }
                }
            }
        }
        int cnt=0;//判斷是否最後有多個螞蟻同時出來
        for(int i=1;i<=n;i++)
        {
            if(p[i].loc==0||p[i].loc==len)//出來那麼就代表他此時的位於木棍的左端或者右端(之前出來的螞蟻此時座標都是大於木棍的長度或者是小於零了)
            {
                num[cnt++]=i;//記錄螞蟻的編號
            }
        }
        if(cnt==1)
            printf("%d %d\n",t[n],num[0]);
        else
        {
            for(int i=0;i<cnt;i++)
            {
                if(!p[num[i]].dir)
                {
                    printf("%d %d\n",t[n],num[i]);
                    break;
                }
            }
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章