1949. [모의 SW 역량테스트] 등산로 조성
제일 긴 등산로의 길이를 반환하라!
가장 높은 지형들에서 DFS()를 호출하여 가장 긴 등산로를 찾는다.
if (map[nx][ny] < start.h) { // 현재보다 낮은 곳
DFS(new Pair(nx, ny, map[nx][ny]), isCutted, length+1);
}
else { // 산을 깎을 수 있는가?
if (isCutted == false && (map[nx][ny]-start.h)<k)
DFS(new Pair(nx, ny, start.h-1), true, length+1);
}
map[][]
의 값을 바꾸지 않기 위해서 현재 높이를 기록한다.isCutted
)를 넘겨주었다.isCutted==false
) 깎을 높이가 k보다 작을 때 산을 깎고 이동할 수 있다. (-> isCutted
를 true로 넘겨줌)import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
static class Pair {
int x;
int y;
int h; // height
Pair() {}
Pair(int x, int y, int h) {this.x=x; this.y=y; this.h=h;}
}
static int k=0;
static int n=0;
static int [][] map = new int[8][8];
static boolean [][] isVisited = new boolean [8][8];
static int maxLength = 0;
static int [] dx = {-1, 0, 1, 0};
static int [] dy = {0, -1, 0, 1};
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++)
{
n = sc.nextInt();
k = sc.nextInt();
int max=0;
map = new int[n][n];
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
// Load map
map[i][j] = sc.nextInt();
if (map[i][j] > max) max = map[i][j];
// init visited
isVisited[i][j] = false;
}
}
// 가장 높은곳에서부터 등산로 탐색
maxLength = 0;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (map[i][j] == max) {
DFS(new Pair(i, j, map[i][j]), false, 1);
}
}
}
System.out.printf("#%d %d\n", test_case, maxLength);
}
}
static void DFS(Pair start, boolean isCutted, int length) {
int nx, ny;
isVisited[start.x][start.y] = true;
if (length > maxLength) maxLength = length;
for (int d=0; d<4; d++) {
nx = start.x+dx[d];
ny = start.y+dy[d];
// 영역 밖 또는 이미방문
if(nx<0||nx>=n||ny<0||ny>=n||isVisited[nx][ny]) continue;
isVisited[nx][ny] = true;
if (map[nx][ny] < start.h) { // 현재보다 낮은 곳
DFS(new Pair(nx, ny, map[nx][ny]), isCutted, length+1);
}
else { // 산을 깎을 수 있는가?
if (isCutted == false && (map[nx][ny]-start.h)<k)
DFS(new Pair(nx, ny, start.h-1), true, length+1);
}
isVisited[nx][ny] = false;
}
isVisited[start.x][start.y] = false;
}
}
nx<=0
으로 했었다..ㅎ ! !