50085. Tank Simulation

難度:4/5 Used Time: 35:18

去年考古題,耗時50分,途中看錯兩次題目所以花了很多時間。

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
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

static inline int max(int a, int b){
return (a > b) ? a : b;
}
static inline int min(int a, int b){
return (a < b) ? a : b;
}

typedef struct{
int x, y;
} s_pt;

int n, m, l, w;
s_pt tank;
int obj_cnt;
int map[512][512] = {0};

int dx[] = {0, 0, 1, 0, -1, 1};
int dy[] = {0, 1, 0, -1, 0, 1};

static inline void ipt(){
scanf("%d %d %d %d", &n, &m, &l, &w);
scanf("%d", &obj_cnt);
for(int i = 0; i < obj_cnt; i++){
int a, b; scanf("%d %d", &a, &b);
map[b][a] = 2;
}
}

static inline int is_tank(int x, int y){
return ( x >= tank.x && x < tank.x + l &&
y >= tank.y && y < tank.y + w);
}

void print_arr(){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(is_tank(i, j)) printf("1");
else printf("%d", map[i][j]);
}
puts("");
}
}

void move(int cmd){
int cnt = 0;
if(cmd == 0) print_arr();
else if(cmd == 1){
if(tank.y + w == m) return;
else{
for(int i = 0; i < l; i++){
if(map[tank.x + i][tank.y + w] == 2) cnt++;
}
if(cnt <= 1){
for(int i = 0; i < l; i++){
map[tank.x + i][tank.y + w] = 0;
}
tank.y++;
}
}
}
else if(cmd == 2){
if(tank.x + l == n) return;
else{
for(int i = 0; i < w; i++){
if(map[tank.x + l][tank.y + i] == 2) cnt++;
}
if(cnt <= 1){
for(int i = 0; i < w; i++){
map[tank.x + l][tank.y + i] = 0;
}
tank.x++;
}
}
}
else if(cmd == 3){
if(tank.y == 0) return;
else{
for(int i = 0; i < l; i++){
if(map[tank.x + i][tank.y - 1] == 2) cnt++;
}
if(cnt <= 1){
for(int i = 0; i < l; i++){
map[tank.x + i][tank.y - 1] = 0;
}
tank.y--;
}
}
}
else if(cmd == 4){
if(tank.x == 0) return;
else{
for(int i = 0; i < w; i++){
if(map[tank.x - 1][tank.y + i] == 2) cnt++;
}
if(cnt <= 1){
for(int i = 0; i < w; i++){
map[tank.x - 1][tank.y + i] = 0;
}
tank.x--;
}
}
}
else if(cmd == 5){
if(tank.y + w == m || tank.x + l == n) return;
else{
for(int i = 1; i < l; i++){
if(map[tank.x + i][tank.y + w] == 2) cnt++;
}
for(int i = 1; i < w; i++){
if(map[tank.x + l][tank.y + i] == 2) cnt++;
}
if(map[tank.x + l][tank.y + w] == 2) cnt++;

if(cnt <= 1){
for(int i = 1; i < l; i++){
map[tank.x + i][tank.y + w] = 0;
}
for(int i = 1; i < w; i++){
map[tank.x + l][tank.y + i] = 0;
}
map[tank.x + l][tank.y + w] = 0;
tank.x++, tank.y++;
}
}
}
}

int main(){
ipt();
int cmd;
// print_arr();
// printf("\n");
while(scanf("%d", &cmd) != EOF){
move(cmd);
}
}

50085. Tank Simulation
https://aaronlin1229.github.io/judgegirl_50085/
Author
Akizumi
Posted on
July 17, 2023
Licensed under