반응형
https://www.acmicpc.net/problem/1987
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int R, C, answer, cnt;
static char[][] map;
static boolean[][] visited;
static int[][] dir = {{0,1},{1,0},{0,-1},{-1,0}};
static boolean[] check;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
map = new char[R][C];
for(int i=0; i<R; i++) {
String s = br.readLine();
for(int j=0; j<C; j++) {
map[i][j] = s.charAt(j);
}
}
visited = new boolean[R][C];
check = new boolean[91];
DFS(0, 0);
System.out.println(answer+1);
}
public static void DFS(int x, int y) {
answer = Math.max(cnt, answer);
visited[x][y] = true;
check[map[x][y]] = true;
for(int i=0; i<4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(isInside(nx, ny) && !visited[nx][ny] && !check[map[nx][ny]]) {
cnt++;
visited[nx][ny] = true;
check[map[nx][ny]] = true;
DFS(nx, ny);
visited[nx][ny] = false;
check[map[nx][ny]] = false;
cnt--;
}
}
}
public static boolean isInside(int x, int y) {
return x>=0 && x<R && y>=0 && y<C;
}
}
반응형
'알고리즘 > 문제풀이' 카테고리의 다른 글
[백준] 12865. 평범한 배낭 (0) | 2023.06.13 |
---|---|
[백준] 11404. 플로이드 (0) | 2021.10.30 |
[백준] 3055. 탈출 (0) | 2020.06.30 |
[백준] 16401. 과자 나눠주기 (0) | 2020.06.08 |
[프로그래머스] 프린터 (0) | 2020.05.04 |