-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_14500.java
More file actions
67 lines (57 loc) · 1.86 KB
/
boj_14500.java
File metadata and controls
67 lines (57 loc) · 1.86 KB
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
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
//참고: https://hanyeop.tistory.com/416
public class boj_14500 {
static int N,M;
static int[][] arr;
static boolean[][] visit;
static int[] dx={-1,1,0,0};
static int[] dy={0,0,-1,1};
static int result=Integer.MIN_VALUE;
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());
arr=new int[N][M];
visit=new boolean[N][M];
for(int i=0;i<N;i++){
st=new StringTokenizer(br.readLine());
for(int j=0;j<M;j++){
arr[i][j]=Integer.parseInt(st.nextToken());
}
}
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
visit[i][j]=true;
solve(i,j,arr[i][j],1);
visit[i][j]=false;
}
}
System.out.println(result);
}
private static void solve(int x,int y,int sum,int count){
if(count==4){
result=Math.max(result,sum);
return;
}
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<0 || nx>=N || ny<0 || ny>=M) continue;
if(!visit[nx][ny]){
//ㅗ모양 만들기 위해서 탐색 한번더 진행
if(count==2){
visit[nx][ny]=true;
solve(x,y,sum+arr[nx][ny],count+1);
}
visit[nx][ny]=true;
solve(nx,ny,sum+arr[nx][ny],count+1);
visit[nx][ny]=false;
}
}
}
}