본문 바로가기
백준

백준 8972 미친 아두이노

by 콩순이냉장고 2021. 10. 24.

문제 URL : https://www.acmicpc.net/problem/8972

 

8972번: 미친 아두이노

요즘 종수는 아두이노를 이용해 "Robots"이라는 게임을 만들었다. 종수는 아두이노 한대를 조정하며, 미친 아두이노를 피해다녀야 한다. 미친 아두이노는 종수의 아두이노를 향해 점점 다가온다.

www.acmicpc.net

 

문제 접근법 :  설명대로 구현만 해주면됩니다.

그렇지만 한가지 생각해야할게 있는데 미친아두이노 들끼리의 충돌을 처리할려고 map을 사용했습니다.

그리고 한가지더 아두이노가 움직일때 종수가 있는자리에 도착하면 게임종료 시켜야합니다.

미친 아두이노가 종수랑 여럿이서 충돌때문에 게임안끝나고 종수가살아 있따는 생각을 못하고

한번 틀리게 됐네요 ㅠ.ㅠ

 

이것만 생각하면 구현하는데 문제가 없을 겂니다.

 

소스코드 : 

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
#include <bits/stdc++.h>
using namespace std;
int n, m;
char board[101][101];
int dy[10= { 0,1,1,1,0,0,0,-1,-1,-1 };
int dx[10= { 0,-1,0,1,-1,0,1,-1,0,1 };
string ms;
vector<pair<intint>> mad;
int cy, cx;
void input() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> board[i][j];
            if (board[i][j] == 'I') {
                cy = i, cx = j;
                board[i][j] = '.';
            }
            else if (board[i][j] == 'R') {
                mad.push_back({ i,j });
                board[i][j] = '.';
            }
        }
    }
    cin >> ms;
}
 
bool isrange(int y, int x) {
    return 0 <= y && y < n && 0 <= x && x < m;
}
int getDis(int y1, int x1, int y2, int x2) {
    return abs(y1 - y2) + abs(x1 - x2);
}
bool isfinish() {
    for (int i = 0; i < mad.size(); i++
        if (cy == mad[i].first && cx == mad[i].second)return true;
    return false;
}
void madmove(int Time) {
    map<pair<intint>int> s;
    vector<pair<intint>> v;
    for (int i = 0; i < mad.size(); i++) {
        int mindis = 1e7;
        int fy = 0, fx = 0;
        for (int j = 1; j <= 9; j++) {
            int ny = mad[i].first + dy[j];
            int nx = mad[i].second + dx[j];
            if (isrange(ny, nx) && getDis(cy, cx, ny, nx)<mindis) {
                mindis = getDis(cy, cx, ny, nx);
                fy = ny;
                fx = nx;
            }
        }
        s[{fy, fx}]++;
        if (fy == cy && fx == cx) {
            cout << "kraj " << Time + 1 << "\n";
            exit(0);
        }
    }
    for (auto& it : s) {
        if (it.second == 1)
            v.push_back(it.first);
    }
    mad = v;
}
void solve() {
    for (int Time = 0; Time<ms.size(); Time++) {
        cy += dy[ms[Time] - '0'];
        cx += dx[ms[Time] - '0'];
        if (isfinish()) {
            cout << "kraj " << Time + 1 << "\n";
            return;
        }
        madmove(Time);
    }
    board[cy][cx] = 'I';
    for (int i = 0; i < mad.size(); i++)
        board[mad[i].first][mad[i].second] = 'R';
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << board[i][j];
        }
        cout << "\n";
    }
 
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt", "r", stdin);
    input();
    solve();
}
cs

 

궁금한점 혹은 모르는점 어떤질문이든 댓글은 언제나 환영입니다.

 

'백준' 카테고리의 다른 글

백준 11000 강의실 배정  (0) 2021.10.30
백준 1715 카드 정렬하기  (0) 2021.10.30
백준 1113 수영장 만들기  (0) 2021.10.17
백준 2666 벽장문의 이동  (0) 2021.10.05
백준 10835 카드게임  (0) 2021.10.05