알고리즘63 :: BOJ_1987_알파벳
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 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class 알파벳 { static int n,m; static char[][] s; static int[][] visit; static int[][] cost; static int[] dx = {-1,1,0,0}; static int[] dy = {0,0,-1,1}; static String str = ""; static int cnt = 1; static int ans = Integer.MIN_VALUE; static void DFS(int nn, int cnt) { if(ans<cnt) ans = cnt; for(int i =0; i<4; i++) { int ddx = (nn/m) + dx[i]; int ddy = (nn%m) + dy[i]; if(ddx<0 || ddx>=n || ddy<0 || ddy>=m) continue; String temp = ""; temp += s[ddx][ddy]; if(visit[ddx][ddy]==0 && !str.contains(temp)) { visit[ddx][ddy] = 1; String k = str; str+=s[ddx][ddy]; DFS(ddx*m+ddy,cnt+1); str = k; visit[ddx][ddy] = 0; } } } public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); s = new char[n][m]; for(int i=0; i<n; i++) { st = new StringTokenizer(br.readLine()); String tte = st.nextToken(); s[i] = tte.toCharArray(); }//end of for loop visit = new int[n][m]; str += s[0][0]; visit[0][0] = 1; DFS(0,1); System.out.println(ans); } } | cs |
'알고리즘' 카테고리의 다른 글
알고리즘68 :: BOJ_17471_게리맨더링 (0) | 2020.02.26 |
---|---|
알고리즘66 :: BOJ_1914_하노이탑 (0) | 2020.02.24 |
알고리즘62 :: BOJ_15740_A+B - 9 (0) | 2020.02.24 |
알고리즘61 :: BOJ_14500_테트로미노 (0) | 2020.02.24 |
알고리즘60 :: BOJ_1748_수이어쓰기1 (0) | 2020.02.24 |
댓글
이 글 공유하기
다른 글
-
알고리즘68 :: BOJ_17471_게리맨더링
알고리즘68 :: BOJ_17471_게리맨더링
2020.02.26 -
알고리즘66 :: BOJ_1914_하노이탑
알고리즘66 :: BOJ_1914_하노이탑
2020.02.24 -
알고리즘62 :: BOJ_15740_A+B - 9
알고리즘62 :: BOJ_15740_A+B - 9
2020.02.24 -
알고리즘61 :: BOJ_14500_테트로미노
알고리즘61 :: BOJ_14500_테트로미노
2020.02.24