-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP114.cpp
131 lines (122 loc) · 2.59 KB
/
P114.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <iostream>
/*
m x n grid, lower left is 0,0
- bumpers on grid points
- walls surrounding
- Ball: Initial position (x,y), direction (up,down,left,right), lifetime.
- speed: 1 grid cell / time.
- rebound clockwise.
- lifetime: move + bumper cost + wall cost
- dirs: 0 ->
1 ^
2 <-
3 v
input:
3 <= m n <= 50
wall cost
|bumpers| = p
...
x y value cost : bumper
...
x y dir lifetime>0 : ball
...
.....
..O..
.O...
.....
*/
void updatePos(int &x, int &y, const int dir) {
switch(dir) {
case 0:
++x;
return;
case 1:
++y;
return;
case 2:
--x;
return;
case 3:
--y;
return;
}
}
int main() {
// Read input:
int m, n, wall_cost, p;
bool bumpers[50*50] = {0};
int values[50*50] = {0};
int costs[50*50] = {0};
std::cin >> m;
--m;
std::cin >> n;
--n;
std::cin >> wall_cost;
std::cin >> p;
for(int i = 0; i < p; ++i) {
int x, y, value, cost;
std::cin >> x;
--x;
std::cin >> y;
--y;
std::cin >> value;
std::cin >> cost;
values[y*m+x] = value;
costs[y*m+x] = cost;
bumpers[y*m+x] = 1;
}
//std::cerr << "GAME MxN " << m << " x " <<n<<" with "<<p<<" bumpers"<<std::endl;
int score_total = 0;
// Handle balls:
while(!std::cin.eof()) {
int score = 0;
int x, y, dir, life;
std::cin >> x;
--x;
if(std::cin.eof())
break;
std::cin >> y;
--y;
std::cin >> dir;
std::cin >> life;
//std::cerr << "Ball start at " << x << "," <<y<<" "<<dir<<" "<<life<<std::endl;
while(life > 1) {
int oldx = x;
int oldy = y;
updatePos(x, y, dir);
// std::cerr << "Moves to " << x << "," <<y<<std::endl;
// move:
--life;
// handle bumper:
bool hitSomething = false;
if(dir == 0 && x == m ||
dir == 1 && y == n ||
dir == 2 && x == 0 ||
dir == 3 && y == 0) {
dir = (dir+3)%4;
life -= wall_cost;
// std::cerr << "Bumps wall => life " << life <<std::endl;
hitSomething = true;
}
else if(bumpers[y*m+x] == 1) {
dir = (dir+3)%4;
score += values[y*m+x];
life -= costs[y*m+x];
// std::cerr << "Bumps bumper " << values[y*m+x] << ", " << costs[y*m+x] << " => life " << life <<std::endl;
hitSomething = true;
}
if(hitSomething) {
x = oldx;
y = oldy;
// just update pos.
}
// std::cerr << " => " << x << "," << y << ", dir " << dir << ", life " <<life<<", score " << score <<std::endl;
}
std::cout << score << std::endl;
score_total += score;
// Handle ball:
}
std::cout << score_total << std::endl;
return 0;
}