티스토리 뷰

PS/PS 일반

2638번 치즈

푸르른강물을먹이를찾아어슬렁거리는연어처럼 2021. 1. 5. 20:13
#pragma warning(disable:4996)
#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
#include <random>
#include<time.h>
#include<string>
#include<queue>
#include <set>
#include <map>
#include <list>
#include<stack>
#include<deque>
#include<cstring>
#define ll  long long
#define INF 987654321
#define pa pair<int, int>
int dirX[] = { 1,0 ,-1,0 };
int dirY[] = { 0,1,0,-1 };
using namespace std;
int n, m;
int mapp[101][101];
int visited[101][101];


void checkOutdoor() {
    queue<pa> q; q.push({ 0,0 });
    visited[0][0] = 2;

    while (!q.empty()) {
        pa cur = q.front(); q.pop();

        
        for (int dir = 0; dir < 4; dir++) {
            int nextY = cur.first + dirY[dir];
            int nextX = cur.second + dirX[dir];

            if (nextY < 0 || nextX < 0 || nextY >= n || nextX >= m || visited[nextY][nextX])
                continue;
            if (mapp[nextY][nextX] > 0)
                continue;
            visited[nextY][nextX] = 2;
            q.push({ nextY, nextX });
        }
    }
}
bool checkItIsEnd() {
    bool flag = false;
    for (int y = 0; y < n; y++) {
        for (int x = 0; x < m; x++) {
            if (mapp[y][x] > 0)
                return false;
        }
    }
    return true;
}


int dfs() {
    memset(visited, 0, sizeof(visited));
    checkOutdoor();
    if (checkItIsEnd())
        return 0;
    for (int y = 0; y < n; y++) {
        for (int x = 0; x < m; x++) {
            if (mapp[y][x] > 0 && !visited[y][x]) {

                queue<pa> q; q.push({ y,x });
                visited[y][x] = 1;
                while (!q.empty()) {
                    
                    pa cur = q.front(); q.pop();

                    int countNear = 0;

                    for (int dir = 0; dir < 4; dir++) {
                        int nextY = cur.first + dirY[dir];
                        int nextX = cur.second + dirX[dir];

                        if (nextY < 0 || nextX < 0 || nextY >= n || nextX >= m || visited[nextY][nextX]==1)
                            continue;
                        if (visited[nextY][nextX] == 2)
                            countNear += 1;
                        if (mapp[nextY][nextX] > 0 && !visited[nextY][nextX]) {
                            q.push({ nextY ,nextX });
                            visited[nextY][nextX] = 1;
                        }
                    }
                    if (countNear >= 2)
                        mapp[cur.first][cur.second] = 0;
                }
            }
        }
    }
    return dfs() + 1;
}



int main() {

    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    
    memset(mapp, 0, sizeof(mapp));
    memset(visited, 0, sizeof(visited));
    
    cin >> n >> m;
    for (int y = 0; y < n; y++) {
        for (int x = 0; x < m; x++) {
            cin >> mapp[y][x];
        }
    }
    int res = dfs();
    cout << res << endl;


  


    return 0;
}

- 주변에 공기와 접촉하는 변이 두 변 이상일 경우 해당하는 칸을 업데이트 해줘야 한다(96번째 줄). 헌데, 업데이트를 조사하는 놈에게 해줘야 하는데(cur.first, cur.second), 엉뚱한(y,x) 놈을 넣어 업데이트를 해주었음.

- 코드를 작성함에 있어서, scope에 신경을 더 쓰도록 하자.

 

'PS > PS 일반' 카테고리의 다른 글

[백준] 2252번: 줄 세우기  (0) 2021.01.17
[백준] 1103 게임  (0) 2021.01.06
2573번 빙산  (0) 2021.01.04
11066 파일 합치기  (0) 2020.12.27
Third Avenue Editorial(AtCoder Beginner Contest 184, E)  (0) 2020.12.14
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함