AOJ1336 the Last Ant

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.

それぞれ左右に動き,他の蟻にぶつかった時に向きを反転する蟻が 匹いる.最後に巣を出た蟻の番号を答える.
左右の移動は確定で,移動した後に同じ場所に蟻が 匹いれば向きを反転する.どうタイミングで巣を出た時は左側から出たの蟻の番号を答えるのを忘れない.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <queue>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main() {
  int n, l;
  while(cin >> n >> l) {
      if(n == 0 && l == 0) break;

      vector<char> dir(n);
      vector<int> p(n);
      rep(i, n) {
          cin >> dir[i] >> p[i];
      }

      int cnt = 0, id = -1, next[105];
      bool used[25];
      memset(used, 0, sizeof(used));

      while(true) {
          memset(next, 0, sizeof(next));

          rep(i, n) {
              if(used[i]) continue;
              if(dir[i] == 'R') p[i]++;
              else p[i]--;

              next[p[i]]++;
          }

          rep(i, n) {
              if(used[i]) continue;
              if(next[p[i]] == 2) {
                  dir[i] = (dir[i] == 'R' ? 'L' : 'R');
              }
          }

          rep(i, n) {
              if(used[i]) continue;
              if(p[i] == l) {
                  used[i] = true;
                  id = i;
              }
          }

          rep(i, n) {
              if(used[i]) continue;
              if(p[i] == 0) {
                  used[i] = true;
                  id = i;
              }
          }

          bool flag = true;
          rep(i, n) {
              if(used[i]) continue;
              flag = false;
          }

          if(flag) break;
          cnt++;
      }

      cout << cnt + 1 << " " << id + 1 << endl;
  }

  return 0;
}
Mar 22nd, 2016