1. 문제에 대한 이해
- 우리가 풀어야 할 문제는 무엇인가?
- 주어진 자료는 무엇인가?
- 조건은 무엇인가?
- 우리가 문제를 풀기 위해 주어진 자료가 충분한가?
- 숨겨진 조건이나 자료가 있는가? 그렇다면 그 것을 다른 방법으로 해석해보라.
2. 계획
- 전에 비슷한 문제를 알고 있는가?
- 이 문제를 푸는데 있어서 유용하게 쓸 수 있는 지식은 무엇인가?
- 비슷한 문제를 풀어본 적이 있다면 그 것을 활용할 수 있는가?
- 만약 문제를 풀 수 없다면 문제를 더 단순하게 하기 위해서 주어진 조건을 버려보아라
- 주어진 자료로부터 유용한 것을 이끌어 낼 수 있는가?
- 자료는 모두 사용했는가?
- 조건을 모두 사용했는가?
- 문제에 포함된 핵심적인 개념은 모두 고려했는가?
3. 실행
- 풀이 계획을 실행하고, 각 단계가 올바른지 점검하라.
import java.util.*;
class Pair{
int x;
int y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
}
public class Main {
public static int maxResult = Integer.MIN_VALUE;
public static int N;
public static int M;
public static int[][] map;
public static int[][] tempMap;
public static boolean[][] check;
public static boolean[][] visited;
public static int[] dx = {0, 0, -1, 1};
public static int[] dy = {-1, 1, 0, 0};
public boolean isInside(int x, int y){
if(0<=x && x<N && 0<=y && y <M){
return true;
}else{
return false;
}
}
public void BFS(int sx, int sy){
Queue<Pair> q = new LinkedList<>();
q.add(new Pair(sx, sy));
visited[sx][sy] = true;
while(!q.isEmpty()){
Pair curr = q.poll();
int x = curr.x;
int y = curr.y;
for(int i=0; i<4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(isInside(nx,ny) && visited[nx][ny] == false && tempMap[nx][ny] == 0){
visited[nx][ny] = true;
tempMap[nx][ny] = 2;
q.add(new Pair(nx,ny));
}
}
}
}
public int getSafeArea(ArrayList<Pair> arr){
int safeArea = 0;
tempMap = new int[N][M];
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
int num = map[i][j];
tempMap[i][j] = num;
}
}
visited = new boolean[N][M];
for(int i=0; i<3; i++){
Pair curr = arr.get(i);
tempMap[curr.x][curr.y] = 1;
}
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
if(tempMap[i][j] == 2 && visited[i][j] == false){
BFS(i, j);
}
}
}
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
if(tempMap[i][j] == 0){
safeArea++;
}
}
}
return safeArea;
}
public void DFS(int cnt, ArrayList<Pair> arr){
if(cnt == 3){
int result = getSafeArea(arr);
maxResult = Math.max(maxResult, result);
return;
}
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
if(check[i][j] == false && map[i][j] == 0){
check[i][j] = true;
arr.add(new Pair(i,j));
DFS(cnt+1, arr);
arr.remove(arr.size()-1);
check[i][j] = false;
}
}
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Main T = new Main();
N = sc.nextInt();
M = sc.nextInt();
map = new int[N][M];
check = new boolean[N][M];
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
int num = sc.nextInt();
map[i][j] = num;
}
}
ArrayList<Pair> arr = new ArrayList<>();
T.DFS(0, arr);
System.out.println(maxResult);
}
}
4. 반성
- 문제를 다른 방식으로 해결할 수 있는가?
- 결과나 방법을 어떤 다른 문제에 활용할 수 있는가?
- 어떻게 하면 더 효율적으로 문제를 해결할 수 있는가?
- 어떻게 하면 더 효과적으로 문제를 해결할 수 있는가?
- 어떻게 하면 더 효율적으로 문제를 해결할 수 있는가?
-> 배열의 값은 객체이기 때문에, 복사를 하면, 해당 값을 참조하게 된다.
-> 따라서 복사하기를 원한다면, 값을 하나씩 빼서 대입을 해줘야 한다.
-> 깊은 복사와 얕은 복사를 공부하자
'PS' 카테고리의 다른 글
| 치즈 [BOJ 2636] (0) | 2022.09.05 |
|---|---|
| 사과 담기 게임 [BOJ 2828] (0) | 2022.09.04 |
| 쿼드 트리 [BOJ 1992] (0) | 2022.09.04 |
| 균형잡힌 세상 [BOJ 4949] (0) | 2022.09.04 |
| 괄호 [BOJ 9012] (0) | 2022.09.04 |