λ¬Έμ λ§ν¬
21608λ²: μμ΄ μ΄λ±νκ΅ (acmicpc.net)
21608λ²: μμ΄ μ΄λ±νκ΅
μμ΄ μ΄λ±νκ΅μλ κ΅μ€μ΄ νλ μκ³ , κ΅μ€μ N×N ν¬κΈ°μ 격μλ‘ λνλΌ μ μλ€. νκ΅μ λ€λλ νμμ μλ N2λͺ μ΄λ€. μ€λμ λͺ¨λ νμμ μ리λ₯Ό μ νλ λ μ΄λ€. νμμ 1λ²λΆν° N2λ²κΉμ§ λ²νΈ
www.acmicpc.net
쑰건μ λ§κ² νμλ€μ μ리μ λ°°μΉνλ λ¨μ ꡬν λ¬Έμ .
νμ΄
μ리μ λ°°μΉνκΈ° μν΄ νμν μ 보
1. κ° νμλ€μ΄ μ’μνλ νμμ λ²νΈ
2. νμλ€μ λ°°μΉ ν μμ
1λ²μ LikeλΌλ intν 벑ν°λ₯Ό λ°°μ΄λ‘ μ μΈν΄μ μ 보λ₯Ό μ μ₯νλ€. Like[ i ]μλ iνμμ΄ μ’μνλ νμλ€μ λ²νΈκ° λ€μ΄κ°μλ€.
2λ² νμλ€μ λ°°μΉ ν μμ λ StudentλΌλ intν 벑ν°λ₯Ό λ§λ€μ΄μ μ λ ₯λλ μμλλ‘ νμλ€μ λ²νΈλ₯Ό μ μ₯νλ€.
1. λΉμ΄μλ μΉΈ μ€μμ μ’μνλ νμμ΄ μΈμ ν μΉΈμ κ°μ₯ λ§μ μΉΈμΌλ‘ μ리λ₯Ό μ νλ€.
2. 1μ λ§μ‘±νλ μΉΈμ΄ μ¬λ¬ κ°μ΄λ©΄, μΈμ ν μΉΈ μ€μμ λΉμ΄μλ μΉΈμ΄ κ°μ₯ λ§μ μΉΈμΌλ‘ μ리λ₯Ό μ νλ€.
3. 2λ₯Ό λ§μ‘±νλ μΉΈλ μ¬λ¬ κ°μΈ κ²½μ°μλ νμ λ²νΈκ° κ°μ₯ μμ μΉΈμΌλ‘, κ·Έλ¬ν μΉΈλ μ¬λ¬ κ°μ΄λ©΄ μ΄μ λ²νΈκ° κ°μ₯ μμ μΉΈμΌλ‘ μ리λ₯Ό μ νλ€.β
μμ 쑰건μ μμλλ‘ μ μ©μν€λ©΄ λλ€.
1λ² μ‘°κ±΄μ λ°λΌ λ¨Όμ λͺ¨λ μΉΈμ λλ©΄μ ν΄λΉ μΉΈμ΄ λΉμΉΈμ΄λ©΄, ν΄λΉμΉΈμ λν΄μ μΈμ ν μΉΈ(4μΉΈ) μ€ νμ¬ μ리μ μμ νμ( i νμ)μ΄ μ’μνλ νμμ΄ κ°μ₯ λ§μ μΉΈλ€μ μΈλ±μ€λ₯Ό Whereμ΄λΌλ 벑ν°μ μ μ₯ν΄λλ€.
μ΄λ, λ§μ½ Where 벑ν°μ ν¬κΈ°κ° 1μ΄λ©΄ λ€μ΄κ° μλ¦¬κ° νλλΏμ΄λΌλ λ»μ΄λ―λ‘ ν΄λΉ μΈλ±μ€μ i νμμ μνλ€.
Where 벑ν°μ ν¬κΈ°κ° 2μ΄μμ΄λΌλ©΄ 2λ² μ‘°κ±΄μ μ μ©μμΌμΌνλ€.
2λ² μ‘°κ±΄μ λ°λΌ Whereμ λ€μ΄μλ μΈλ±μ€λ₯Ό λλ©΄μ μΈμ ν μΉΈ μ€ λΉμΉΈμ΄ κ°μ₯ λ§μ μ리μ μΈλ±μ€λ₯Ό λ€μ ν λ² Where 벑ν°μ λ£λλ€.
λ¬Όλ‘ μ΄λ κΈ°μ‘΄μ Whereμ λ€μ΄μλ κ°μ λΉΌμ£Όκ³ μλ‘μ΄ κ°λ§ λ£μ΄μ£Όμ΄μΌνλ€. κ·Έλμ λλ TempλΌλ 벑ν°μ μΈμ μΉΈ μ€ λΉμΉΈμ΄ κ°μ₯ λ§μ μΈλ±μ€λ₯Ό λ£μ΄λκ³ λ§μ§λ§μ Where = Temp λΌκ³ ν΄μ€¬λ€.
2λ² μ‘°κ±΄μ μ μ©μν¨ ν Where 벑ν°μ ν¬κΈ°κ° 1μ΄λ©΄ ν΄λΉ μ리μ i νμμ μνλ€.
Where 벑ν°μ ν¬κΈ°κ° 2μ΄μμ΄λΌλ©΄ 3λ² μ‘°κ±΄μ λ°λΌ Where 벑ν°λ₯Ό μ λ ¬ν΄μ€ ν κ°μ₯ μμμλ μΈλ±μ€μ i νμμ μνλ©΄ λ.
μ΄λ κ² λͺ¨λ νμλ€μ μκ²νκ³ λμ λͺ¨λ μΉΈμ λλ©΄μ ν΄λΉ νμλ€μ λ§μ‘±λλ₯Ό ansλΌλ λ³μμ λν΄μ€¬λ€.
λ§μ§λ§μ ansλ₯Ό μΆλ ₯ν΄μ£Όλ©΄ λμ΄λ€.
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <deque>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 400 + 5;
int N, Q;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
vector<int> Like[MAX_N];
vector<int> Student;
int A[MAX_N][MAX_N];
vector<pair<int, int>> Where;
bool is_ok(pair<int, int>);
void num_one(int std);
void num_two();
bool is_like(int std, int like);
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for (int i = 1; i <= N * N; i++) {
int Std;
cin >> Std;
Student.push_back(Std);
for (int j = 0; j < 4; j++) {
int like; cin >> like;
Like[Std].push_back(like);
}
}
for (int i = 0; i < Student.size(); i++) {
Where.clear();
int now = Student[i];
num_one(now);
if (Where.size() == 1) {
A[Where[0].first][Where[0].second] = now;
continue;
}
num_two();
if (Where.size() == 1) {
A[Where[0].first][Where[0].second] = now;
continue;
}
sort(Where.begin(), Where.end());
A[Where[0].first][Where[0].second] = now;
}
int ans = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
int now = A[i][j];
int cnt = 0;
for (int k = 0; k < 4; k++) {
pair<int, int> next = { i + dy[k], j + dx[k] };
if (is_ok(next) && is_like(now, A[next.first][next.second]))
cnt++;
}
if (cnt == 0)
continue;
ans += (int)pow(10, cnt - 1);
}
}
cout << ans << "\n";
return 0;
}
void num_one(int std) {
int Max = -1;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (A[i][j])
continue;
int cnt = 0;
for (int k = 0; k < 4; k++) {
pair<int, int> next = { i + dy[k], j + dx[k] };
if (is_ok(next) && is_like(std, A[next.first][next.second]))
cnt++;
}
if (cnt > Max) {
Max = cnt;
Where.clear();
Where.push_back({ i,j });
}
else if (cnt == Max)
Where.push_back({ i,j });
}
}
}
void num_two() {
int Max = -1;
vector<pair<int,int>> temp;
for (int i = 0; i < Where.size(); i++) {
pair<int, int> now = Where[i];
int cnt = 0;
for (int j = 0; j < 4; j++) {
pair<int, int> next = { now.first + dy[j], now.second + dx[j] };
if (is_ok(next) && !A[next.first][next.second])
cnt++;
}
if (cnt > Max) {
Max = cnt;
temp.clear();
temp.push_back(now);
}
else if (cnt == Max)
temp.push_back(now);
}
Where = temp;
}
bool is_like(int std, int like) {
for (int i = 0; i < Like[std].size(); i++) {
if (Like[std][i] == like)
return true;
}
return false;
}
bool is_ok(pair<int, int> now) {
if (now.first < 1 || now.first > N || now.second < 1 || now.second > N)
return false;
return true;
}
|
cs |
μ¬κΈ°μ μ’μνλ νμμΈμ§ μλμ§λ₯Ό μ΄μ°¨μ bool λ°°μ΄μ μ΄μ©ν΄μ νννλ€λ©΄,
μλ₯Όλ€μ΄ bool is_like[ i ][ j ] κ° trueλ©΄ iκ° jλ₯Ό μ’μνλλ€λ λ»μ΄λ€.
μ΄λ κ² νλ©΄ νμ iκ° νμ jλ₯Ό μ’μνλμ§λ₯Ό O(1)λ§μ νλ³ν μ μλ€.
κΈ°μ‘΄μ λ΄ μ½λλ iκ° μ’μνλ νμλ€μ λͺ©λ‘μ jκ° λ€μ΄μλμ§ νλ¨νλ―λ‘ μ΅λ O(4) λ§νΌμ μκ°μ΄ κ±Έλ¦°λ€.
λμ bool μ΄μ°¨μ λ°°μ΄μ μ μΈν΄μΌνλ―λ‘ λ©λͺ¨λ¦¬κ° μ‘°κΈ λ μ°μΌ κ²κ°λ€.
'λ°±μ€ > Gold' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 21610 : λ§λ²μ¬ μμ΄μ λΉλ°λΌκΈ° (0) | 2022.06.02 |
---|---|
[BOJ] 21609 : μμ΄ μ€νκ΅ (0) | 2022.06.02 |
[BOJ] 20058 : λ§λ²μ¬ μμ΄μ νμ΄μ΄μ€ν° (0) | 2022.05.31 |
[BOJ] 20057 : λ§λ²μ¬ μμ΄μ ν λ€μ΄λ (0) | 2022.05.30 |
[BOJ] 20056 : λ§λ²μ¬ μμ΄μ νμ΄μ΄λ³Ό (0) | 2022.05.24 |