Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 구현
- 알고리즘
- 그래프
- 백트래킹
- MST
- kapt
- spring boot
- 누적 합
- 위상 정렬
- 분리 집합
- Java
- disjoint set
- BFS
- miller-rabin
- SCC
- DFS
- kruskal
- tarjan
- Linux
- BindingAdapter
- union-find
- 이분 탐색
- 위상정렬
- DP
- Meet in the middle
- springdoc
- 페르마 소정리
- MySQL
- concurreny
- 투 포인터
Archives
- Today
- Total
기맹기 개발 블로그
[BOJ 1987] 알파벳 (Java) 본문
BOJ 1987 알파벳
난이도 : 골드 4
https://www.acmicpc.net/problem/1987
1987번: 알파벳
세로 R칸, 가로 C칸으로 된 표 모양의 보드가 있다. 보드의 각 칸에는 대문자 알파벳이 하나씩 적혀 있고, 좌측 상단 칸 (1행 1열) 에는 말이 놓여 있다. 말은 상하좌우로 인접한 네 칸 중의 한 칸으
www.acmicpc.net
전략
주어진 조건을 만족하도록 DFS로 탐색하면서 가장 많이 이동한 칸을 저장하면 된다.
시행착오
간단한 문제로 시행착오는 없었다.
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int R, C;
static char[][] map;
static boolean[] visited;
final static int[] dr = { -1, 1, 0, 0 };
final static int[] dc = { 0, 0, -1, 1 };
static int ans = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] line = br.readLine().split(" ");
R = Integer.parseInt(line[0]);
C = Integer.parseInt(line[1]);
map = new char[R][C];
visited = new boolean[26];
for (int i = 0; i < R; i++) {
String str = br.readLine();
for (int j = 0; j < str.length(); j++)
map[i][j] = str.charAt(j);
}
visited[map[0][0] - 'A'] = true;
dfs(0, 0, 1);
System.out.println(ans);
}
static void dfs(int r, int c, int cnt) {
ans = Math.max(ans, cnt);
for (int i = 0; i < 4; i++) {
int nR = dr[i] + r;
int nC = dc[i] + c;
if (nR >= 0 && nR < R && nC >= 0 && nC < C) {
int alphabet = map[nR][nC] - 'A';
if (!visited[alphabet]) {
visited[alphabet] = true;
dfs(nR, nC, cnt + 1);
visited[alphabet] = false;
}
}
}
}
}
'PS' 카테고리의 다른 글
[BOJ 16724] 피리 부는 사나이 (Java) (0) | 2022.08.26 |
---|---|
[BOJ 1208] 부분수열의 합 2 (Java) (0) | 2022.08.25 |
[BOJ 10775] 공항 (Java) (0) | 2022.08.21 |
[BOJ 9527] 1의 개수 (Java) (0) | 2022.08.20 |
[BOJ 10986] 나머지 합 (Java) (0) | 2022.08.20 |