728x90
https://www.acmicpc.net/problem/17837
17837번: 새로운 게임 2
재현이는 주변을 살펴보던 중 체스판과 말을 이용해서 새로운 게임을 만들기로 했다. 새로운 게임은 크기가 N×N인 체스판에서 진행되고, 사용하는 말의 개수는 K개이다. 말은 원판모양이고, 하�
www.acmicpc.net
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
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct info{
int y, x, d;
};
int n, k;
int dy[5] = {0, 0, 0, -1, 1};
int dx[5] = {0, 1, -1, 0, 0};
int _trunDir(int dir){
if(dir == 1) return 2;
if(dir == 2) return 1;
if(dir == 3) return 4;
if(dir == 4) return 3;
}
info horse[10];
int map[13][13];
vector <int> h[13][13]; // 말 위치
int moveHorse(int i){
int yy = horse[i].y + dy[horse[i].d];
int xx = horse[i].x + dx[horse[i].d];
// 벽 or 파랑
if( yy<=0 || yy > n || xx <=0 || xx > n || map[yy][xx] == 2){
horse[i].d = _trunDir(horse[i].d);
yy = horse[i].y + dy[horse[i].d];
xx = horse[i].x + dx[horse[i].d];
//반대 방향도 벽 or 파랑
if(yy<=0 || yy > n || xx <=0 || xx > n || map[yy][xx] == 2){
return 0;
}
}
vector<int> &cur = h[horse[i].y][horse[i].x];
vector<int> &next = h[yy][xx];
auto s = find(cur.begin(), cur.end(), i);
//빨간 칸 뒤집기
if(map[yy][xx] == 1){
reverse(s, cur.end());
}
for(auto iter = s; iter != cur.end(); iter++){
horse[*iter].y = yy;
horse[*iter].x = xx;
next.push_back(*iter);
}
cur.erase(s, cur.end());
return next.size();
}
int search(){
int ans, i, y, x, stack_cnt;
for(int ans = 1; ans <= 1000; ans++){
for(i = 0; i < k; ++i){
stack_cnt = moveHorse(i);
if(stack_cnt >=4) return ans;
}
}
return -1;
}
int main(){
cin>>n>>k;
for(int i = 1; i <=n; i++){
for(int j = 1; j<=n; j++){
cin>>map[i][j];
}
}
for(int i = 0; i < k; i++){
info& ho = horse[i];
cin >> ho.y >> ho.x >> ho.d;
h[ho.y][ho.x].push_back(i);
}
cout<<search();
return 0;
}
|
728x90
'Algorithm Study > BOJ with C++' 카테고리의 다른 글
(BAEKJOON) 17779번: 게리맨더링 2 (0) | 2020.07.08 |
---|---|
(BAEKJOON) 17142번: 연구소 3 (0) | 2020.07.08 |
(BAEKJOON) 17140번: 이차원 배열과 연산 (0) | 2020.07.08 |
(BAEKJOON) 17143번: 낚시왕 (0) | 2020.07.08 |
(BAEKJOON) 17144번: 미세먼지 안녕! (0) | 2020.07.08 |
댓글