50121. Push Stones
難度:4/5 Used Time: 25:571
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#include <stdio.h>
#include <stdlib.h>
#define max(a,b) ((a)>(b)?(a):(b))
int n, m;
int x, y, e;
int o;
int arr[512][512] = {0};
int dx[5] = {0, 0, 1, 0, -1};
int dy[5] = {0, 1, 0, -1, 0};
// 4
// 3 1
// 2
static inline int is_valid(int i, int j){
return i >= 0 && j >= 0 && i < n && j < m;
}
static inline void ipt(){
scanf("%d %d", &n, &m);
scanf("%d %d %d", &x, &y, &e);
scanf("%d", &o);
for(int i = 0; i < o; i++){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
arr[a][b] = c;
}
}
static inline void print_map(){
int temp_info = arr[x][y];
arr[x][y] = e;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
printf("%d%c", arr[i][j], (j == m - 1) ? '\n' : ' ');
}
}
arr[x][y] = temp_info;
}
static inline void solve(int op){
if(op == 0) print_map();
else{
int dl_x = dx[op], dl_y = dy[op];
int start_x = x, start_y = y;
int end_x = x + dl_x, end_y = y + dl_y;
int sum = 0;
while(is_valid(end_x, end_y)){
if(arr[end_x][end_y] == 0) break;
sum += arr[end_x][end_y];
end_x += dl_x, end_y += dl_y;
}
if((!is_valid(end_x, end_y)) || sum > e) return;
int move_x = end_x, move_y = end_y;
while(!(move_x == start_x && move_y == start_y)){
arr[move_x][move_y] = arr[move_x - dl_x][move_y - dl_y];
move_x -= dl_x, move_y -= dl_y;
}
arr[move_x][move_y] = 0;
x += dl_x, y += dl_y;
e -= sum;
}
}
int main(){
ipt();
int op;
while(scanf("%d", &op) != EOF){
solve(op);
}
}