๋ฌธ์ ๋งํฌ
17780๋ฒ: ์๋ก์ด ๊ฒ์ (acmicpc.net)
17780๋ฒ: ์๋ก์ด ๊ฒ์
์ฌํ์ด๋ ์ฃผ๋ณ์ ์ดํด๋ณด๋ ์ค ์ฒด์คํ๊ณผ ๋ง์ ์ด์ฉํด์ ์๋ก์ด ๊ฒ์์ ๋ง๋ค๊ธฐ๋ก ํ๋ค. ์๋ก์ด ๊ฒ์์ ํฌ๊ธฐ๊ฐ N×N์ธ ์ฒด์คํ์์ ์งํ๋๊ณ , ์ฌ์ฉํ๋ ๋ง์ ๊ฐ์๋ K๊ฐ์ด๋ค. ๋ง์ ์ํ๋ชจ์์ด๊ณ , ํ
www.acmicpc.net
์ฌํ์ด๊ฐ ์ฒด์คํ์ผ๋ก ๋ง๋ ์๋ก์ด ๊ฒ์์ด ์ข ๋ฃ๋๊ธฐ๊น์ง ๋ช ํด์ด ๊ฑธ๋ฆฌ๋์ง ์ถ๋ ฅํ๋ ๋ฌธ์ .
์์ฆ ๋ฐฑ์ค ๋ฌธ์ ์ง ์ค์์ danimartinwife ๋์ด ์ฌ๋ ค์ฃผ์ โ ์ง์ ์ฝํ ๊ดํํ๋ฉด์ ๋ชจ์ ๋ฌธ์ ๋คโ ์ด๋ผ๋ ๋ฌธ์ ์ง์ ํ์ด๋ณด๊ณ ์๋๋ฐ ์ฌ๋ฐ๋ ๋ฌธ์ ๋ค์ด ๋ง์ ๊ฒ๊ฐ๋ค.
ํ์ด
๊ท์น์ ๊ฐ๋จํ๊ฒ ์ค๋ช ํ์๋ฉด
์ด K๊ฐ์ ๋ง์ด ์๋๋ฐ ํ ํด์ 1๋ฒ๋ง๋ถํฐ K๋ฒ๋ง๊น์ง ์์๋๋ก ์์ง์ธ๋ค. ๋จ, ๋ง์ ์ฌ๋ฌ๊ฐ ๊ฒน์น ์ ์๊ณ ๋ง์ฝ ๋ง์ด ๊ฒน์ณ์ง ๊ฒฝ์ฐ ๋งจ ์๋์ ์๋ ๋ง๋ง ์ด๋ํ ์ ์๋ค. ์ด๋ ์์ ์์ฌ์๋ ๋ง๋ ๊ฐ์ด ์ด๋ํ๋ค. ๋ง์ด 4๊ฐ ์ด์ ์์ธ ์นธ์ด ํ๋๋ผ๋ ์์ผ๋ฉด ์ฆ์ ๊ฒ์์ ์ข ๋ฃํ๋ค.
์ฌ๊ธฐ์ ์ด๋ํ ์นธ์ด
1. ํฐ์์ด๋ฉด ๊ทธ๋ฅ ์ด๋ํ๋ค. ์ด๋ํ๋ ค๋ ์นธ์ ์ด๋ฏธ ๋ง์ด ์์ผ๋ฉด ์ด ์์ ๊ฒน์ณ์ ์๋๋ค.
2. ๋นจ๊ฐ์์ด๋ฉด ์ด๋ํ ํ์ ์์ฌ์๋ ๋ง๋ค์ ๋ค์ง๋๋ค. ๋ง์ฝ 1์ด ์ ์ผ ๋ฐ์ ์๊ณ 1,2,3 ์์ผ๋ก ์์ฌ์์๋ค๋ฉด 3์ด ์ ์ผ ์๋๋ก ๊ฐ๊ฒ๋๊ณ 3,2,1 ์์๋ก ๋ณ๊ฒฝ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ฏธ ์์ฌ์ ธ์๋ ์นธ์ ์์ฌ์ง ๋ง์ ๊ฒน์ณ ์์๋์๋ ์๋ก ์์ ๋ง๋ค๋ง ์์๋ฅผ ๋ฐ๊พผ๋ค.
3. ํ๋์ ์นธ์ด๊ฑฐ๋ ํ์ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ ๋ง์ ๋ฐฉํฅ์ ๋ฐ๋๋ก ๋ฐ๊พธ๊ณ ํ ์นธ ์ด๋ํ๋ค. ๊ทธ ์ด๋ํ ์นธ์ด ๋ง์ฝ ๋ ํ๋์ ์นธ์ด๊ฑฐ๋ ํ์ ๋ฒ์ด๋๋ค๋ฉด ์ ์๋ฆฌ์ ๊ฐ๋งํ์๋๋ค.
์ค์ํ ๊ฒ์ ๋ง์ด ์ฌ๋ฌ๊ฐ ์์ฌ์์ ๋ ๋งจ ์๋์ ์๋ ๋ง๋ง ์์ง์ธ๋ค๋ ๊ฒ์ด๋ค.
1~K๋ฒ๊น์ง ๋ง์ ํ์ํ๋ฉด์ ๋งจ ์๋์ ์๋ ๋ง๋ง ์ด๋์ํค๋ฉด ๋๋ ๊ฒ์ด๋ค.
for (int i = 1; i <= K; i++) {
int now_y = Position[i].first;
int now_x = Position[i].second;
int horse_num = horse[now_y][now_x][0];
if (horse_num != i)
continue;
์ฌ๊ธฐ์ horse ๋ ๊ฐ ์นธ์ ์๋ ๋ง์ ๋ฒํธ๋ฅผ ์ ์ฅํด๋ ๋ฒกํฐ์ด๋ค. ์นธ์ ๋ค์ด์จ ์์๋๋ก ๋ฒกํฐ์ ์์ด๊ธฐ ๋๋ฌธ์ 0๋ฒ ์ธ๋ฑ์ค์ ์๋ ๋ง์ด ๊ฐ์ฅ์๋์ ์๋ ๋ง์ ๋ปํ๋ค. ๋ฐ๋ผ์ horse_num์๋ ๊ฐ์ฅ ์๋์ ์๋ ๋ง์ ๋ฒํธ๊ฐ ์ ๋ ฅ๋๊ณ , ์ด ๋ฒํธ๊ฐ ํ์ฌ ์์ง์ผ ๋ง์ ๋ฒํธ์ ๊ฐ์ง ์๋ค๋ฉด continue๋ก ์คํตํ๋ค.
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
|
#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 <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 20 + 5;
int N, K;
vector<int> horse[MAX_N][MAX_N];
int Direction[MAX_N];
int Map[MAX_N][MAX_N];
pair<int, int> Position[MAX_N];
int dx[] = { 0,1,-1,0,0 };
int dy[] = { 0,0,0,-1,1 };
bool is_ok(pair<int, int> Position) {
if (Position.first < 1 || Position.first > N || Position.second < 1 || Position.second > N)
return false;
return true;
}
void Change_Direction(int horse_num) {
if (Direction[horse_num] == 1) Direction[horse_num] = 2;
else if (Direction[horse_num] == 2) Direction[horse_num] = 1;
else if (Direction[horse_num] == 3) Direction[horse_num] = 4;
else if (Direction[horse_num] == 4) Direction[horse_num] = 3;
}
bool Move(pair<int,int> from, pair<int,int> to, int color) {
if (color == 0) { //ํฐ์
for (int i = 0; i < horse[from.first][from.second].size(); i++) {
horse[to.first][to.second].push_back(horse[from.first][from.second][i]);
Position[horse[from.first][from.second][i]] = { to.first, to.second };
}
}
else if (color == 1) {
for (int i = (int)horse[from.first][from.second].size() - 1; i >= 0; i--) {
horse[to.first][to.second].push_back(horse[from.first][from.second][i]);
Position[horse[from.first][from.second][i]] = { to.first, to.second };
}
}
horse[from.first][from.second].clear();
if (horse[to.first][to.second].size() >= 4)
return false;
return true;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> K;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++)
cin >> Map[i][j];
}
for (int i = 1; i <= K; i++) {
int X, Y, D;
cin >> Y >> X >> D;
horse[Y][X].push_back(i);
Position[i] = { Y,X };
Direction[i] = D;
}
int turn = 0;
while (true) {
turn++;
if (turn > 1000)
break;
bool chk = true;
for (int i = 1; i <= K; i++) {
int now_y = Position[i].first;
int now_x = Position[i].second;
int horse_num = horse[now_y][now_x][0];
if (horse_num != i)
continue;
pair<int, int> next = { now_y + dy[Direction[horse_num]], now_x + dx[Direction[horse_num]] };
if (!is_ok(next) || Map[next.first][next.second] == 2) {
Change_Direction(horse_num);
next = { now_y + dy[Direction[horse_num]],now_x + dx[Direction[horse_num]] };
if (!is_ok(next) || Map[next.first][next.second] == 2)
continue;
else if (!Move({ now_y,now_x }, next, Map[next.first][next.second])) {
chk = false;
break;
}
}
else {
if (!Move({ now_y,now_x }, next, Map[next.first][next.second])) {
chk = false;
break;
}
}
}
if (!chk)
break;
}
if (turn > 1000)
cout << "-1\n";
else cout << turn << "\n";
return 0;
}
|
cs |
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 1800 : ์ธํฐ๋ท ์ค์น (0) | 2022.04.20 |
---|---|
[BOJ] 16639 : ๊ดํธ ์ถ๊ฐํ๊ธฐ 3 (0) | 2022.04.19 |
[BOJ] 10021 : Watering the Fields (0) | 2022.04.14 |
[BOJ] 15684 : ์ฌ๋ค๋ฆฌ ์กฐ์ (0) | 2022.04.14 |
[BOJ] 14499 : ์ฃผ์ฌ์ ๊ตด๋ฆฌ๊ธฐ (0) | 2022.04.08 |