-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2117.cpp
130 lines (101 loc) · 2.3 KB
/
2117.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
//
// Created by 융미 on 2019-10-19.
//
//swea 2117 홈 방범 서비스
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 21
int tc, n, m;
int answer;
int arr[MAX][MAX] = {0,};
int dis[MAX][MAX] = {0,};
int costArr[2*MAX] = {0,};
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
queue<pair<int,int>> q;
void make_cost(){
for(int i = 1; i<2*MAX; ++i){
costArr[i] = i*i + (i - 1)*(i - 1);
}
}
void init(){
for(int i = 0; i<n; ++i)
for(int j = 0; j<n; ++j)
dis[i][j]=0;
}
void print(){
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++)
{
cout << arr[i][j] << ' ';
}
cout << endl;
}
cout << endl;
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++)
{
cout << dis[i][j] << ' ';
}
cout << endl;
}
cout << endl;
}
void bfs(int a, int b){
int cnt = 0;
int benefit = 0;
q.push(make_pair(a,b));
dis[a][b]=1;
if(arr[a][b] == 1) cnt++;
for(int k=1;;k++){
if( q.empty() )
break;
int size = (int)q.size();
benefit = (cnt * m) - costArr[k];
if(benefit >= 0)
{
if(cnt > answer) answer = cnt;
}
for(int t=0; t<size; t++){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int i=0; i<4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
if(dis[nx][ny] != 0) continue;
dis[nx][ny] = dis[x][y] + 1;
q.push(make_pair(nx,ny));
if(arr[nx][ny] == 1) ++cnt;
}
}
}
init();
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
make_cost();
cin >> tc;
for(int t = 1; t<= tc; ++t){
cin >> n >> m;
for(int i = 0; i<n; ++i){
for(int j = 0; j<n; ++j) {
cin >> arr[i][j];
}
}
for(int i = 0; i<n;++i){
for(int j = 0; j<n; ++j){
bfs(i,j);
}
}
cout << "#" << t << ' ' << answer << '\n';
answer = -1;
}
return 0;
}