• Home
  • About
    • Ryureka Moment photo

      Ryureka

      Sin Prisa, Sin Pausa

    • About Me
    • Facebook
    • Github
    • Youtube
  • Projects
  • Posts
    • Posts
    • ProblemSolvings
    • Tags
    • Blog
    • Examples
  • ProblemSolving
    • ProblemSolving
    • BOJ
    • Programmers
    • SWEA
    • LeetCode
  • FrontEnd
    • FrontEnd
    • HTML
  • BackEnd
    • BackEnd
    • Server
      • Server
      • Spring
      • NodeJS
    • DataBase
      • DataBase
      • MySQL
      • MongoDB
  • Programming
    • Programming
    • Java
    • JS
    • Python
    • CleanCode
  • ComputerScience
    • DataStructure
    • Algorithm

[BOJ] 1987. 알파벳

15 Apr 2026

Reading time ~1 minute

  • 풀이

풀이

import java.util.Scanner;

public class Main {
	static Scanner sc=new Scanner(System.in);
	static int H=sc.nextInt();
	static int W=sc.nextInt();
	static int dx[]= {1,0,-1,0};
	static int dy[]= {0,1,0,-1};
	static char map[][]=new char[H][W];
	static int ans=Integer.MIN_VALUE;
	static boolean check[]=new boolean[26];
	public static void main(String[] args) {
		sc.nextLine();
		for (int i = 0; i < H; i++) {
			String s=sc.nextLine();
			for (int j = 0; j < W; j++) {
				map[i][j]=s.charAt(j);
			}
		}
				
		check[map[0][0]-'A']=true;
		go(0,0,1);
		
		System.out.println(ans);
	}
	
	public static void go(int x,int y,int d) {
		if(d>ans) {
			ans=d;
		}
		for (int i = 0; i < 4; i++) {
			int nx=x+dx[i];
			int ny=y+dy[i];
			if(nx<0 || ny<0 || nx>H-1 || ny> W-1) continue;
			if(!check[map[nx][ny]-'A']) {
				check[map[nx][ny]-'A']=true;
				go(nx,ny,d+1);
				check[map[nx][ny]-'A']=false;
			}
		}
	}
}


백트래킹 Share