여니의 취준 준비/코딩테스트 (Java)

[14500] 테트로미노 in Java

여니's 2022. 9. 17. 19:13

 

 

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

 

14500번: 테트로미노

폴리오미노란 크기가 1×1인 정사각형을 여러 개 이어서 붙인 도형이며, 다음과 같은 조건을 만족해야 한다. 정사각형은 서로 겹치면 안 된다. 도형은 모두 연결되어 있어야 한다. 정사각형의 변

www.acmicpc.net


백트래킹을 사용하여

폴리오미노 모양을 만들어주었던 문제

 

이때 방문했던 지점에서 깊이가 4가 되기전까지는

무조건 상하좌우 방향을 계속해서 탐색해줘야한다.

 

그래서 dfs 내에서는 3중 for문을 돌려야함.

 

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package week5;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.StringTokenizer;
 
public class n14500 {
    static int n,m;
    static int[][] map;
    static int[] dxs= {-1,1,0,0};
    static int[] dys= {0,0,-1,1};
    static int answer;
    static boolean[][] visited;
    static ArrayList<Point> queue;
    
    static class Point{
        int x,y;
 
        public Point(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
        
    }
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        n=Integer.parseInt(st.nextToken());
        m=Integer.parseInt(st.nextToken());
        map=new int[n][m];
        answer=0;
        visited=new boolean[n][m];
        queue=new ArrayList<>();
        
        for (int i = 0; i < n; i++) {
            st=new StringTokenizer(br.readLine());
            for (int j = 0; j < m; j++) {
                map[i][j]=Integer.parseInt(st.nextToken());
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                visited[i][j]=true;
                queue.add(new Point(i,j));
                dfs(1,map[i][j]);
                visited[i][j]=false;
                queue.remove(0);
            }
        }
        System.out.println(answer);
    }
 
    public static void dfs(int cnt, int sum) {
        if(cnt==4) {
            answer=Math.max(answer, sum);
            return;
        }
        for (int c = 0; c < queue.size(); c++) {
            Point p=queue.get(c);
            for (int i = 0; i < 4; i++) {
                int nx=p.x+dxs[i];
                int ny=p.y+dys[i];
                if(canGo(nx,ny) && !visited[nx][ny]) {
                    visited[nx][ny]=true;
                    queue.add(new Point(nx,ny));
                    dfs(cnt+1,sum+map[nx][ny]);
                    visited[nx][ny]=false;
                    queue.remove(queue.size()-1);
                }
                
            }
        }    
    }
 
    public static boolean canGo(int x, int y) {
        return x>=0 && x<&& y>=0 && y<m;
    }
}
 
cs

 

package week5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class n14500 {
	static int n,m;
	static int[][] map;
	static int[] dxs= {-1,1,0,0};
	static int[] dys= {0,0,-1,1};
	static int answer;
	static boolean[][] visited;
	static ArrayList<Point> queue;
	
	static class Point{
		int x,y;

		public Point(int x, int y) {
			super();
			this.x = x;
			this.y = y;
		}
		
	}
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		n=Integer.parseInt(st.nextToken());
		m=Integer.parseInt(st.nextToken());
		map=new int[n][m];
		answer=0;
		visited=new boolean[n][m];
		queue=new ArrayList<>();
		
		for (int i = 0; i < n; i++) {
			st=new StringTokenizer(br.readLine());
			for (int j = 0; j < m; j++) {
				map[i][j]=Integer.parseInt(st.nextToken());
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				visited[i][j]=true;
				queue.add(new Point(i,j));
				dfs(1,map[i][j]);
				visited[i][j]=false;
				queue.remove(0);
			}
		}
		System.out.println(answer);
	}

	public static void dfs(int cnt, int sum) {
		if(cnt==4) {
			answer=Math.max(answer, sum);
			return;
		}
		for (int c = 0; c < queue.size(); c++) {
			Point p=queue.get(c);
			for (int i = 0; i < 4; i++) {
				int nx=p.x+dxs[i];
				int ny=p.y+dys[i];
				if(canGo(nx,ny) && !visited[nx][ny]) {
					visited[nx][ny]=true;
					queue.add(new Point(nx,ny));
					dfs(cnt+1,sum+map[nx][ny]);
					visited[nx][ny]=false;
					queue.remove(queue.size()-1);
				}
				
			}
		}	
	}

	public static boolean canGo(int x, int y) {
		return x>=0 && x<n && y>=0 && y<m;
	}
}