뜌릅

Cow Art(적록색약) 10026번 [백준] 본문

알고리즘/PS 문제

Cow Art(적록색약) 10026번 [백준]

TwoCastle9 2022. 8. 31. 15:30
반응형

dfs을 활용하여 간단하게 풀 수 있는 문제이다. 이미 접근한 좌표는 다시 접근하지 못하게 visited배열을 활용한다.

#include<bits/stdc++.h>
#include<sys/types.h>


using namespace std;
#define endl '\n'
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i, j) for(ll i=0;i<j;i++)

int n;
string s[100];
int board[100][100];
bool visited[100][100];

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int dfs(int x, int y, int type) {

    rep(i, 4) {
        int r = x + dx[i];
        int c = y + dy[i];
        if (r < 0 || r >= n || c < 0 || c >= n)continue;

        if (type == 0) {
            if (!visited[r][c] && board[x][y] == board[r][c]) {
                visited[r][c] = true;
                dfs(r, c, type);
            }

        } else if (type == 1) {
            if (!visited[r][c]) {
                if ((board[x][y] == 1 || board[x][y] == 2) && (board[r][c] == 1 || board[r][c] == 2)) {
                    visited[r][c] = true;
                    dfs(r, c, type);
                } else if (board[x][y] == 3 && board[r][c] == 3) {
                    visited[r][c] = true;
                    dfs(r, c, type);
                }

            }
        }
    }
    return 1;
}

void solve() {
//#ifndef ONLINE_JUDGE
//    freopen("input.txt", "r", stdin);
//    freopen("all.txt", "w", stdout);
//#endif
    cin >> n;
    rep(i, n)cin >> s[i];
    rep(i, n)
        rep(j, n) {
            if (s[i][j] == 'R')board[i][j] = 1;
            else if (s[i][j] == 'G')board[i][j] = 2;
            else if (s[i][j] == 'B')board[i][j] = 3;
            else {
                cout << "ERROR" << endl;
                return;
            }
        }
    int ans1 = 0;
    rep(i, n)rep(j, n) {
            if (!visited[i][j])
                ans1 += dfs(i, j, 0);
        }
    int ans2 = 0;
    rep(i, 100) {
        memset(visited[i], false, sizeof(visited[i]));
    }
    rep(i, n)rep(j, n) {
            if (!visited[i][j])
                ans2 += dfs(i, j, 1);
        }
    cout << ans1 << " " << ans2 << endl;
}

int main() {
    fast_io;
    solve();
    return 0;
}

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

 

10026번: 적록색약

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)

www.acmicpc.net

 

 

반응형

'알고리즘 > PS 문제' 카테고리의 다른 글

테트로미노 14500번 [백준]  (0) 2022.09.02
외판원 순회3 16991번 [백준]  (0) 2022.09.01
DSLR 9019번 [백준]  (0) 2022.08.30
이중 우선순위 큐 7662번 [백준]  (1) 2022.08.29
토마토 7569번 [백준]  (4) 2022.08.28