시간 초과
216
jhjikhsdsdw
작성한 질문수 27
0
코드는 강의에 올려주신 것과 비슷한데 시간 초과가 발생합니다...
import java.util.*;
public class Ch8_14 {
public static int n,m, dis, answer = Integer.MAX_VALUE;
public static int[][] board;
public static ArrayList<Point> pizza = new ArrayList<>();
public static ArrayList<Point> house = new ArrayList<>();
public static int[] combi;
public static class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void DFS(int index, int start) {
if(index == m) {
int sum = 0;
for(Point h : house) {
dis=Integer.MAX_VALUE;
for (int idx : combi) {
dis = Math.min(dis, Math.abs(h.x - pizza.get(idx).x) + Math.abs(h.y - pizza.get(idx).y));
}
sum+=dis;
}
answer = Math.min(answer, sum);
} else {
for(int i = start; i < pizza.size(); i++) {
combi[index] = i;
DFS(index+1, start+1);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
board = new int[n][n];
// 입력 받기
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
int tmp = sc.nextInt();
if(tmp == 1) house.add(new Point(i,j));
if(tmp == 2) pizza.add(new Point(i,j));
}
}
combi = new int[m];
DFS(0,0);
System.out.println(answer);
}
}
답변 1
안녕하세요. 바뀐 채점사이트 관련해서 문의드립니다.
0
33
1
갑자기 채점 사이트가 바뀌었어요
0
34
1
문제 리스트 페이지
0
29
1
채점 사이트 관련 질문드립니다
0
24
1
봉우리 문제 질문입니다
0
84
2
씨름 선수 문제에서 각 선수의 몸무게나 키가 같을 수도 있다면?
0
65
0
이 코드랑 영상 코드중에 뭐가 더 좋은 코드인가요?
0
72
0
가중치 방향 그래프에서 가중치가 0인 간선을 표현하는 방법
0
67
1
좌표 정렬 문제 이 코드가 왜 틀린지 모르겠습니다 ㅠㅠ
0
85
2
6-7 강의에서
0
48
1
6-6. 장난꾸러기 질문 있습니다.
0
46
1
강의 수강후 코딩테스트
0
111
1
answer 변수 사용 여부
0
46
1
2중 for문
1
85
2
2-11. 임시반장정하기 (Runtime Error)
0
63
1
혹시 LinkedList 같은 자료 구조들은 따로 배우지 않나요?
0
70
1
이런 풀이는 어떨까요
0
44
1
자바 스트림 방식의 효율성 질문 드립니다.
0
57
1
알고리즘 자료 구조들..
0
63
1
StringBuilder vs BufferdWriter
0
48
1
원더랜드(프림)
0
50
1
이런 코드는 어떤가요?
0
61
1
bfs 풀이
0
57
1
병합정렬
0
57
1





