UVa 13046 - Bus Collisions

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

給予兩個公車行駛路線,每台公車的速度都相同,分別從起始位置出發,請問第一個碰撞位置為何?

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
4
0 4
2 4
2 5
0 5
4
1 7
1 2
3 2
3 7
4
5 0
6 0
6 1
5 1
4
7 0
8 0
8 1
7 1

Sample Output

1
2
Case 1: 1 5
Case 2: No Collision

Solution

抓路線長度的最小公倍數,一定能在這段時間內找到碰撞位置,否則不存在碰撞地點。

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
#include <bits/stdc++.h>
using namespace std;
struct Pt {
int x, y;
Pt(int x = 0, int y = 0): x(x), y(y) {
}
int dist(Pt u) {
return abs(x - u.x) + abs(y - u.y);
}
bool operator==(const Pt &u) const {
return x == u.x && y == u.y;
}
Pt operator-(const Pt &u) const {
return Pt(x - u.x, y - u.y);
}
Pt operator+(const Pt &u) const {
return Pt(x + u.x, y + u.y);
}
};
int main() {
int testcase, cases = 0;
int N[2], x, y;
Pt R[2][64];
scanf("%d", &testcase);
while (testcase--) {
int Len[2] = {};
for (int i = 0; i < 2; i++) {
scanf("%d", &N[i]);
for (int j = 0; j < N[i]; j++) {
scanf("%d %d", &x, &y);
R[i][j] = Pt(x, y);
}
for (int j = 0; j < N[i]; j++)
Len[i] += R[i][j].dist(R[i][(j+1)%N[i]]);
}
printf("Case %d: ", ++cases);
int simT = Len[0]/__gcd(Len[0], Len[1])*Len[1];
int posIdx[2], has = 0;
Pt posXY[2];
for (int i = 0; i < 2; i++)
posIdx[i] = 0, posXY[i] = R[i][0];
for (int it = 0; it < simT; it++) {
for (int i = 0; i < 2; i++) {
// move
if (posXY[i] == R[i][posIdx[i]])
posIdx[i] = (posIdx[i]+1)%N[i];
Pt dv = R[i][posIdx[i]] - posXY[i];
int lenDv = abs(dv.x) + abs(dv.y);
dv.x /= lenDv, dv.y /= lenDv;
posXY[i] = posXY[i] + dv;
}
if (posXY[0] == posXY[1]) {
printf("%d %d\n", posXY[0].x, posXY[0].y);
has = 1;
break;
}
}
if (!has)
puts("No Collision");
}
return 0;
}