50002. Game of Life

難度:3.5/5 Used Time: 16:57

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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int n, k;
int map[105][105];
int live_count[105][105];
int temp_board[105][105];

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

static inline void ipt(){
scanf("%d %d", &n, &k);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
scanf("%d", &map[i][j]);
live_count[i][j] = 0;
}
}
}
static inline int max(int a, int b){
if(a > b) return a;
else return b;
}
static inline void print_line(int arr[], int n){
printf("%d", arr[1]);
for(int i = 2; i <= n; i++){
printf(" %d", arr[i]);
}
printf("\n");
}

static inline bool is_legal(int x, int y){
if((x >= 1 && x <= n) && (y >= 1) && (y <= n)) return true;
else return false;
}
int count_around(int x, int y){
int cnt = 0;
for(int k = 0; k < 8; k++){
if(is_legal(x + dx[k], y + dy[k])){
if(map[x + dx[k]][y + dy[k]] == 1){
cnt++;
}
}
}
return cnt;
}
static inline void update_board(){
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){
int cnt = count_around(i, j);

if(map[i][j] == 1){
if(cnt == 2 || cnt == 3) temp_board[i][j] = 1;
else temp_board[i][j] = 0;
}
else{
if(cnt == 3) temp_board[i][j] = 1;
else temp_board[i][j] = 0;
}
}

for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){
map[i][j] = temp_board[i][j];
}

}
static inline void update_count(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(map[i][j] == 1) live_count[i][j]++;
}
}
}

static inline int get_max_count(){
int max_num = live_count[1][1];
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){
max_num = max(max_num, live_count[i][j]);
}
return max_num;
}


int main(){
ipt();

update_count();
for(int i = 0; i < k; i++){
update_board();
update_count();
}

int max_num = get_max_count();
int max_x, max_y;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){
if(live_count[i][j] == max_num){
max_x = i; max_y = j;
}
}

for(int i = 1; i <= n; i++) print_line(map[i], n);
printf("%d %d\n", max_x, max_y);

return 0;
}


50002. Game of Life
https://aaronlin1229.github.io/judgegirl_50002/
Author
Akizumi
Posted on
July 17, 2023
Licensed under