Skip to content

Commit 8c01012

Browse files
authored
Create 백준_14500_테트로미노.java
1 parent 1a466a1 commit 8c01012

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.util.*;
2+
import java.io.*;
3+
public class Main {
4+
static int[][] tetris;
5+
static boolean[][] visited;
6+
static int[] dx = {-1,1,0,0};
7+
static int[] dy = {0,0,-1,1};
8+
static int result;
9+
static int N,M;
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
StringTokenizer st = new StringTokenizer(br.readLine()," ");
13+
N = Integer.parseInt(st.nextToken());//행
14+
M = Integer.parseInt(st.nextToken());//열
15+
16+
tetris = new int[N][M];
17+
visited = new boolean[N][M];
18+
for(int i=0; i<N; i++){
19+
st = new StringTokenizer(br.readLine()," ");
20+
for(int j=0; j<M; j++){
21+
tetris[i][j]=Integer.parseInt(st.nextToken());
22+
}
23+
}
24+
25+
for (int i = 0; i < N; i++) {
26+
for (int j = 0; j < M; j++) {
27+
visited[i][j] = true;
28+
dfs(i, j, 1, tetris[i][j]); // DFS로 4칸 합
29+
visited[i][j] = false;
30+
31+
checkExtraShape(i, j); //예외 처리
32+
}
33+
}
34+
System.out.println(result);
35+
36+
}
37+
static void dfs(int a, int b, int depth, int sum){//행열
38+
if(depth==4){
39+
result = Math.max(result, sum);
40+
return;
41+
}
42+
for(int i=0; i<4; i++){
43+
int nx = b+ dx[i];
44+
int ny = a+dy[i];
45+
46+
if(nx>=M ||ny>=N|| nx<0||ny<0) continue;
47+
if(visited[ny][nx]) continue;
48+
visited[ny][nx]=true;
49+
dfs(ny, nx, depth+1, sum+tetris[ny][nx]);
50+
visited[ny][nx]=false;
51+
}
52+
}
53+
static void checkExtraShape(int y,int x){
54+
for (int i = 0; i < 4; i++) {
55+
int temp = tetris[y][x];
56+
int cnt =0;
57+
for (int j = 0; j < 4; j++) {
58+
if(i==j) continue;
59+
int ny = y + dy[j];
60+
int nx = x + dx[j];
61+
if (ny < 0 || nx < 0 || ny >= N || nx >= M){
62+
temp=0;
63+
break;
64+
}
65+
temp += tetris[ny][nx];
66+
cnt++;
67+
}
68+
if(cnt ==3) result = Math.max(result, temp);
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)