You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
풀 수 있는 방법이 여러가지 있는데 시간복잡도를 계산했을 때 효율적이려면 이전에 선택한 칸을 제외하기 위해 중복 제거하면서 풀어야 함
어려운 점, 실수
시간 복잡도 때문에 실패
풀이
packagemain.java.com.poogle.BOJ.Q18290;
importjava.util.Scanner;
publicclassMain {
staticint[][] a = newint[10][10];
staticboolean[][] c = newboolean[10][10];
staticintn, m, k;
staticintans = -2147483647;
//인접한 곳 좌표 차이 오 왼 아 위staticint[] dx = {0, 0, 1, -1};
staticint[] dy = {1, -1, 0, 0};
//cnt: 선택한 칸의 개수//sum: 선택한 칸의 합// //1번 O((NM) ^ k): 선택한 칸이 같은데 선택한 순서가 다른 방법을 여러 번 계산 => 중복 선택// static void go(int cnt, int sum) {// //2번 px: previous의 x값: 이전에 선택한 행을 빼고 하기 위해서 -> 같은 행의 경우 중복 삭제 안됨// static void go(int px, int cnt, int sum) {//3번 (px, py): 이전에 선택한 칸 -> 중복 제거// static void go(int px, int py, int cnt, int sum) {// //4번: N과 M처럼 일차원으로 생각해서 풀이staticvoidgo(intprev, intcnt, intsum) {
if (cnt == k) {
if (ans < sum) ans = sum;
return;
}
// for (int x = 0; x < n; x++) {// for (int y = 0; y < m; y++) {// for (int x = px; x < n; x++) {// for (int y = 0; y < m; y++) {// for (int x = px; x < n; x++) {// for (int y = (x == px ? py : 0); y < m; y++) {for (intj = prev + 1; j < n * m; j++) {
//4번만intx = j / m;
inty = j % m;
if (c[x][y]) continue; //중복선택 판단booleanok = true;
//인접 네 방향 알아보기for (inti = 0; i < 4; i++) {
intnx = x + dx[i];
intny = y + dy[i];
//범위 안에 있고 그 칸을 선택한 적 있으면 falseif (0 <= nx && nx < n && 0 <= ny && ny < m) {
if (c[nx][ny]) ok = false;
//ok는 선택 가능하면 true, 아니면 false
}
}
if (ok) {
c[x][y] = true;
// go(cnt + 1, sum + a[x][y]);// go(x, cnt + 1, sum + a[x][y]);// go(x, y, cnt + 1, sum + a[x][y]);go(x * m + y, cnt + 1, sum + a[x][y]);
c[x][y] = false;
}
}
}
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
k = sc.nextInt();
for (inti = 0; i < n; i++) {
for (intj = 0; j < m; j++) {
a[i][j] = sc.nextInt();
}
}
// go(0, 0);// go(0, 0, 0);// go(0, 0, 0, 0);go(-1, 0, 0);
System.out.println(ans);
}
}
The text was updated successfully, but these errors were encountered:
문제
핵심 아이디어
어려운 점, 실수
풀이
The text was updated successfully, but these errors were encountered: