Baekjoon #14502

Baekjoon #14502

2개의 풀이 · C++, Python

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
#include<bits/stdc++.h>
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int a[10][10];
bool vis[10][10];
int N, M;
vector<pair<int,int>> empty_cell;
int ans;

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

void dfs(int idx, int cnt) {
    if(cnt == 3) {
        queue<pair<int,int>> q;
        memset(vis, false, sizeof(vis));
        for(int i=0;i<N;++i) {
            for(int j=0;j<M;++j) {
                if(!vis[i][j] && a[i][j] == 2) {
                    queue<pair<int,int>> q;
                    q.push({i, j});
                    vis[i][j] = true;
                    while(!q.empty()) {
                        int x = q.front().first;
                        int y = q.front().second;
                        q.pop();
                        for(int k=0;k<4;++k) {
                            int nx = x + dx[k];
                            int ny = y + dy[k];
                            if(!valid(nx, ny) || vis[nx][ny]) continue;
                            if(a[nx][ny] == 1) continue;
                            vis[nx][ny] = true;
                            q.push({nx, ny});
                        }
                    }
                }
            }
        }
        int safe = 0;
        for(int i=0;i<N;++i) {
            for(int j=0;j<M;++j) {
                if(!vis[i][j] && a[i][j] == 0) ++safe;
            }
        }
        ans = max(ans, safe);
        return ;
    }
    if(idx == empty_cell.size()) return ;
    int x = empty_cell[idx].first;
    int y = empty_cell[idx].second;
    a[x][y] = 1;
    dfs(idx+1, cnt+1);
    a[x][y] = 0;
    dfs(idx+1, cnt);
}

int main() {
    cin.tie(nullptr); ios::sync_with_stdio(false);
    cin >> N >> M;
    for(int i=0;i<N;++i) {
        for(int j=0;j<M;++j) {
            cin >> a[i][j];
            if(a[i][j] == 0) empty_cell.emplace_back(i, j);
        }
    }
    dfs(0, 0);
    cout << ans << '\n';
    return 0;
}

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.

GITHUB COMMUNITY

커뮤니티 평가

GitHub 계정으로 남긴 최신 평가 1건만 반영하며, 데이터는 매일 저장소에 동기화됩니다.

최근 동기화

체감 난이도

아직 평가 없음

첫 난이도 평가를 남겨주세요.

문제 추천

아직 평가 없음

이 문제가 도움이 되었는지 알려주세요.

이 문제 평가하기

GitHub 로그인 후 난이도를 제출하고, 원하는 경우 추천 여부도 함께 남길 수 있습니다.

추천 여부 (선택)

평가하려면 GitHub로 로그인해 주세요.

평가는 자동으로 저장소 Discussion에 기록되므로 Discussion 화면을 직접 열 필요가 없습니다. 같은 문제를 다시 평가하면 기존 평가가 갱신됩니다.

DISCUSSION

댓글

GitHub 로그인 후 작성할 수 있으며 모든 댓글은 이 저장소의 Discussions에 보관됩니다.