Skip to content

Commit

Permalink
Merge pull request #35 from AlgoLeadMe/8-dhlee777
Browse files Browse the repository at this point in the history
8-dhlee777
  • Loading branch information
dhlee777 authored May 8, 2024
2 parents ef99fe7 + 9aeab06 commit ab8d008
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions dhlee777/dfs/적둝색약.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include<iostream>
using namespace std;
char color[100][100]; //정상인이 λ³΄λŠ” κ·Έλ¦Ό
char color2[100][100]; //적둝색약이 λ³΄λŠ” κ·Έλ¦Ό
bool visited[100][100]; //정상인 그림의 λ°©λ¬Έμ—¬λΆ€
bool visited2[100][100]; //적둝색약 그림의 λ°©λ¬Έμ—¬λΆ€
string line; // ν•œμ€„ μž…λ ₯을 λ°›κΈ°μœ„ν•œ 슀트링
int siz; //그림의 ν•œ λ³€μ˜ 길이
int coun = 0; //정상인이 κ·Έλ¦Όμ„λ΄€μ„λ•Œ κ΅¬μ—­μ˜κ°œμˆ˜
int coun2 = 0; //적둝색약이 λ΄€μ„λ•Œ κ΅¬μ—­μ˜κ°œμˆ˜
int x[4]={0,0,-1,1}; //μƒν•˜μ’Œμš° 탐색을 μœ„ν•œ xμ’Œν‘œ 이동값
int y[4] = { -1,1,0,0 }; // yμ’Œν‘œ 이동값

void dfs(int a,int b,char color[][100], bool visited[][100]) { //x:νƒμƒ‰μ‹œμž‘ μ’Œν‘œ,y:νƒμƒ‰μ‹œμž‘ yμ’Œν‘œ color:μ •μƒμΈμ˜ 탐색인지 μ λ‘μƒ‰μ•½μ˜ 탐색인지
for (int i = 0; i < 4; i++) {
int a2 = a + x[i];
int b2 = b + y[i];
if (a2 >= 0 && a2 < siz && b2 >= 0 && b2 < siz && !visited[a2][b2]&&color[a][b]==color[a2][b2]) {
visited[a2][b2] = true;
dfs(a2, b2,color,visited);
}
}
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> siz;
for (int i = 0; i < siz; i++) { //color,clolor2배열에 그림을 μž…λ ₯λ°›μ•„ μ €μž₯ν•΄μ€€λ‹€.
cin >> line;
for (int j = 0; j < siz; j++) {
color[i][j] = line[j];
if (line[j] == 'R') color2[i][j] = 'G'; //적둝색약을 μœ„ν•΄ r을 g둜 λ‹€ λ°”κΏ”μ„œ μ €μž₯ν•΄μ€€λ‹€.
else color2[i][j] = line[j];
}
}

for (int i = 0; i < siz; i++) { //μ •μƒμΈμ˜ 탐색
for (int j = 0; j < siz; j++) {
if (!visited[i][j]) {
dfs(i,j,color,visited);
coun++;
}
}
}
for (int i = 0; i < siz; i++) { //μ λ‘μƒ‰μ•½μ˜ 탐색
for (int j = 0; j < siz; j++) {
if (!visited2[i][j]) {
dfs(i, j, color2,visited2);
coun2++;
}
}
}
cout << coun <<" "<< coun2;
return 0;
}

0 comments on commit ab8d008

Please sign in to comment.