본문 바로가기
  • 소소한 개발자 이야기
Algorithm Study/BOJ with C++

(BAEKJOON) 17837번: 새로운 게임2

by Siwan_Min 2020. 7. 8.
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= {000-11};
int dx[5= {01-100};
 
int _trunDir(int dir){
    if(dir == 1return 2;
    if(dir == 2return 1;
    if(dir == 3return 4;
    if(dir == 4return 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 >=4return 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

댓글