티스토리 뷰

PS

18428. 감시 피하기

tose33 2021. 8. 3. 18:26

https://www.acmicpc.net/problem/18428

 

18428번: 감시 피하기

NxN 크기의 복도가 있다. 복도는 1x1 크기의 칸으로 나누어지며, 특정한 위치에는 선생님, 학생, 혹은 장애물이 위치할 수 있다. 현재 몇 명의 학생들은 수업시간에 몰래 복도로 빠져나왔는데, 복

www.acmicpc.net

 

1. 재귀를 이용한 dfs로 선생님과, 학생이 없는 곳에 장애물을 놓을수 있는 모든 경우를 구한다

2. 모든 경우에 대하여 선생님의 4방향 시선 경로에 학생이 있는지 판단한다

3. 한 번이라도 숨을수 있는 경우가 있으면 YES

 

#include <iostream>
#include <algorithm>
#include <vector>
#define endl '\n'
using namespace std;

int n;
char map[7][7];
vector<pair<int,int>> tl; // teacher's location
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
bool ans = false;

// 숨을수 있으면 true
bool CanHide()
{
    for(int t = 0; t < tl.size(); t++)
    {
        int tr = tl[t].first;
        int tc = tl[t].second;

        // 4방향 탐색
        for(int i = 0; i < 4; i++)
        {
            int _tr = tr + dr[i];
            int _tc = tc + dc[i];

            while(_tr >= 0 && _tr < n && _tc >= 0 && _tc < n)
            {
                // 시선경로에 벽이 있으면 더이상 탐색 불가능
                if(map[_tr][_tc] == 'O') break;
                // 학생을 찾음
                if(map[_tr][_tc] == 'S')
                {
                    return false;
                }

                _tr += dr[i];
                _tc += dc[i];
            }
        }
    }
    return true;
}

void MakeWalls(int depth)
{
    if(ans) return;

    if(depth == 3)
    {
        if(CanHide())
        {
            ans = true;
        }

        return;
    }

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(map[i][j] == 'T' || map[i][j] == 'S' || map[i][j] == 'O') continue;

            map[i][j] = 'O';
            MakeWalls(depth+1);
            map[i][j] = 'X';
        }
    }

}

int main()
{
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cin >> map[i][j];

            if(map[i][j] == 'T')
                tl.push_back({i,j});
        }
    }

    MakeWalls(0);

    if(ans)
        cout << "YES";
    else
        cout << "NO";
}

'PS' 카테고리의 다른 글

백준 1914. 하노이 탑  (0) 2021.08.04
백준 9663. N-Queen  (0) 2021.08.04
백준 1342. 행운의 문자열  (0) 2021.08.03
백준 11068. 회문인 수  (0) 2021.08.03
백준 2116. 주사위 쌓기  (0) 2021.08.03
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
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
글 보관함