상세 컨텐츠

본문 제목

16441번: 아기돼지와 늑대 - 그래프(DFS)

알고리즘/baekjoon

by oVeron 2023. 5. 27. 10:13

본문

728x90
반응형

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

 

16441번: 아기돼지와 늑대

첫 번째 줄에는 격자의 행의 수를 나타내는 N (3 ≤ N ≤ 100) 과 격자의 열의 수를 나타내는 M (3 ≤ M ≤ 100) 이 주어집니다. 두 번째 줄부터 N개의 줄에 지도의 정보를 나타내는 길이가 M인 문자열

www.acmicpc.net

전형적인 그래프 구현 문제이되, 빙판 구현을 신경써야 한다. 

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;

int N, M;
char board[101][101];
vector<pii> wolf;
bool vis[101][101];
int dir[2][4] = {{-1, 1, 0, 0}, {0, 0, -1, 1}};

void DFS(int x, int y)
{
    vis[x][y] = true;
    for (int i{0}; i < 4; i++)
    {
        int nx = x, ny = y;
        while (true)
        {
            nx += dir[0][i];
            ny += dir[1][i];

            if (nx >= 1 && nx <= N && ny >= 1 && ny <= M)
            {
                if (board[nx][ny] == '+')
                    continue;
                else if (board[nx][ny] == '.' && !vis[nx][ny])
                    DFS(nx, ny);
                else if (board[nx][ny] == '#' && !vis[nx - dir[0][i]][ny - dir[1][i]])
                    DFS(nx - dir[0][i], ny - dir[1][i]);
                break;
            }
        }
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    cin >> N >> M;
    for (int i{1}; i <= N; i++)
    {
        for (int j{1}; j <= M; j++)
        {
            cin >> board[i][j];
            if (board[i][j] == 'W')
            {
                wolf.push_back({i, j});
                board[i][j] = '.';
            }
        }
    }

    for (auto [x, y] : wolf)
        DFS(x, y);

    for (auto [x, y] : wolf)
        board[x][y] = 'W';
    for (int i{1}; i <= N; i++)
    {
        for (int j{1}; j <= M; j++)
        {
            if (board[i][j] == '.' && !vis[i][j])
                cout << 'P';
            else
                cout << board[i][j];
        }
        cout << "\n";
    }
}
728x90
반응형

관련글 더보기

댓글 영역