백준 11967번: 불켜기

Seungil Kim·2021년 10월 23일
0

PS

목록 보기
62/206

불켜기

백준 11967번: 불켜기

아이디어

원래라면 방문하지 못했을 곳이 나중에 불을 켜서 방문할 수 있게 되는 경우가 있다. 이 경우를 위해 지금은 못가지만 나중에 불이 켜지기만 하면 갈 수 있는 곳을 추가로 기록한다. 그리고 불을 켰을 때 귀여운 소 베시를 거기로 순간이동(?) 시켜준다.

코드

#include <bits/stdc++.h>

using namespace std;

int N, M, ANS = 1;
bool visited[101][101];
bool light[101][101];
bool available[101][101];
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};
vector<pair<int, int>> graph[101][101];
queue<pair<int, int>> q;

bool check_safe(int y, int x) {
    return (y > 0 && y <= N && x > 0 && x <= N);
}

void bfs() {
    visited[1][1] = true;
    available[1][1] = true;
    light[1][1] = true;
    q.push({1, 1});
    while(!q.empty()) {
        int y = q.front().first;
        int x = q.front().second;
        q.pop();
        
        for (auto l : graph[y][x]) {
            if (!light[l.first][l.second]) {
                light[l.first][l.second] = true;
                ANS++;
                if (available[l.first][l.second]) {
                    visited[l.first][l.second] = true;
                    q.push({l.first, l.second});
                    //cout << "Teleport " << l.first << ' ' << l.second << '\n';
                }
            }
        }
        
        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (!check_safe(ny, nx)) continue;
            else if (visited[ny][nx]) continue;
            else if (!light[ny][nx]) {
                available[ny][nx] = true;
            }
            else {
                available[ny][nx] = true;
                visited[ny][nx] = true;
                q.push({ny, nx});
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> N >> M;
    int x, y, a, b;
    while (M--) {
        cin >> x >> y >> a >> b;
        graph[x][y].push_back({a, b});
    }
    bfs();
    cout << ANS << '\n';
    return 0;
}

여담

시작 위치 불 안켜줘서 계속 틀림. 어떤 TC에서 삑났는지 보여주면 참 좋을텐데..
USACO 문제에 나오는 소 이름 너무 귀엽다. 베시~

profile
블로그 옮겼어용 https://ks1ksi.io/

0개의 댓글