문제 링크
https://www.acmicpc.net/problem/14502
문제 해결
1. DFS를 이용해서 문제 해결. 0과 2의 row, column 인덱스를 저장하고 3중 for문을 활용해서 문제 해결.
주의할 점 || 생각해볼 점
-
참고
-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
explanation in vjerksen.tistory.com | |
*/ | |
#include<cstdio> | |
#include<iostream> | |
#include<string> | |
#include<algorithm> | |
#include<cstring> | |
#include<set> | |
#include<stack> | |
#include<map> | |
#include<cmath> | |
#include<queue> | |
#include<cstdlib> | |
#include<vector> | |
#include<list> | |
using namespace std; | |
int N,M; | |
int ans; | |
int row_move[]={0,0,-1,1}; | |
int col_move[]={-1,1,0,0}; | |
vector<pair<int,int> > zero,two; | |
int a[10][10],b[10][10]; | |
void dfs(int row,int col){ | |
for(int i=0;i<4;i++){ | |
int nrow=row+row_move[i]; | |
int ncol=col+col_move[i]; | |
if(nrow>=0&&nrow<N&&ncol>=0&&ncol<M){ | |
if(b[nrow][ncol]==0){ | |
b[nrow][ncol]=2; | |
dfs(nrow,ncol); | |
} | |
} | |
} | |
} | |
int main(){ | |
cin >> N >> M; | |
for(int i=0;i<N;i++){ | |
for(int j=0;j<M;j++){ | |
scanf("%d",&a[i][j]); | |
if(a[i][j]==2) | |
two.push_back({i,j}); | |
else if(a[i][j]==0) | |
zero.push_back({i,j}); | |
} | |
} | |
for(int i=0;i<zero.size();i++){ | |
for(int j=i+1;j<zero.size();j++){ | |
for(int k=j+1;k<zero.size();k++){ | |
memcpy(b,a,sizeof(a)); | |
b[zero[i].first][zero[i].second]=1; | |
b[zero[j].first][zero[j].second]=1; | |
b[zero[k].first][zero[k].second]=1; | |
for(int l=0;l<two.size();l++){ | |
dfs(two[l].first,two[l].second); | |
} | |
int cnt=0; | |
for(int x=0;x<N;x++){ | |
for(int y=0;y<M;y++){ | |
if(b[x][y]==0) | |
cnt++; | |
} | |
} | |
ans=max(ans,cnt); | |
} | |
} | |
} | |
printf("%d",ans); | |
return 0; | |
} |
※ 정확하고 부드러운 태클은 언제나 환영입니다.
'Problem Solving > 그래프' 카테고리의 다른 글
SW Expert Academy 1795 - 인수의 생일 파티 (0) | 2017.09.08 |
---|---|
codeground practice - 최소 신장 트리 (0) | 2017.07.14 |
BOJ 14621 - 나만 안되는 연애 (0) | 2017.06.29 |
BOJ 1884 - 고속도로 (0) | 2017.06.24 |
BOJ 5558 - 치 ~ 즈 (0) | 2017.06.15 |