olrlobt
[์๋ฐ/๋ฐฑ์ค ๊ณจ๋4] #14500 ํ ํธ๋ก๋ฏธ๋ ธ ๋ณธ๋ฌธ
๐ ๊ณจ๋4 - #14500 ํ ํธ๋ก๋ฏธ๋ ธ
โ๏ธ ํ์ด๋ฒ
ํด๋น๋ฌธ์ ๋ ๊ฐ๋จํ๊ฒ ํด์ํ์๋ฉด, 4๊ฐ์ ๋ถ์ด์๋ ์ซ์๋ค์ ํฉ์ด ๊ฐ์ฅ ํฐ ๊ฐ์ ์ฐพ๋ ๋ฌธ์ ์๋ค.
๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ค ๋์์ผ ๋ต์ ๊ตฌํ ์ ์๊ธฐ ๋๋ฌธ์ DFS๋ฅผ ์ฌ์ฉํ์์ผ๋ฉฐ,
์์ ์ขํ์์๋ถํฐ DFS๋ฅผ ์์ํ๊ธฐ ๋๋ฌธ์, ๋ฐฐ์ด์ ๋ณต์ฌํด์ ๋๊ฒจ์ฃผ๋ฉด์ ํ์๋ค.
ํ์ด๋ ๊ฐ๋จํ ์ฒ์ ์์ํ ์๋ฆฌ์์ ์ค๋ฅธ์ชฝ > ์๋์ชฝ > ์ผ์ชฝ ์์ผ๋ก ๊ฒ์ฆ์ ์งํํ๋ฉด ํ๊ฒ ๋ค๊ณ ํ๋จํ์ฌ,
๋ฐ๋ก ํ์ด๋ฅผ ์งํํ๋ค.
ํ์ง๋ง, ํ
์คํธ์ผ์ด์ค 3๋ฒ์ ์์ ์ฒ๋ผ ใ
ใ
ใ
ใ
์ ๊ฒฝ์ฐ์ ๋ํ ์๊ฐ์ ๋ชปํ ํ์ด๋ฒ์ด์๊ณ ,
3๋ฒ์ ๊ฒฝ์ฐ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ์ฌ solve() ํ๋จ๋ถ๋ถ์ if ๋ฌธ์ผ๋ก ๊น๋ํ์ง๋ ๋ชปํ๊ฒ ํด๊ฒฐํ ๋ฌธ์ ์๋ค.
๐๏ธ ํ์ด
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Solution14500 {
static List<Integer> answer = new ArrayList<>();
static List<Integer> ar = new ArrayList<>();
private static int N;
private static int M;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
for (int i = 0; i < N; i++) { //์
๋ ฅ๋ฐ๋ ๊ตฌ๋ฌธ
for (int j = 0; j < M; j++) {
ar.add(sc.nextInt());
}
}
for (int k = 0; k < ar.size(); k++) { // solve
List<Integer> tetro = new ArrayList<>();
solve(k + 1, tetro);
}
int maxAnswer = Collections.max(answer);
System.out.println(maxAnswer);
sc.close();
}
private static void solve(int start, List<Integer> tetro) { // ์์์ง์ ์์ ์ค๋ฅธ์ชฝ๊ณผ ์๋๋ง ์ฐพ๋๋ค.
tetro.add(start);
if (tetro.size() == 4) { // ๋ฐฐ์ด ๋ณต์ฌํด์ ๋๊ฒจ์ค์ผํ ๋ฏ.
answer.add(sumTetro(tetro));
return;
}
if (validate(start + 1, 0, tetro)) {// ์ฐ์ธกํ์นธ ๊ฐ๋ฅ ?
List<Integer> copyTetro = new ArrayList<>(tetro);
solve(start + 1, copyTetro);
}
if (validate(start + M, 1, tetro)) {// ์๋ํ์นธ ๊ฐ๋ฅ ?
List<Integer> copyTetro = new ArrayList<>(tetro);
solve(start + M, copyTetro);
}
if (validate(start - 1, 2, tetro)) {// ์ข์ธกํ์นธ ๊ฐ๋ฅ ?
List<Integer> copyTetro = new ArrayList<>(tetro);
solve(start - 1, copyTetro);
}
if (tetro.size() == 3 && validateStick(tetro)) { // 3์ฐ์ 1์๋ก ๋ฐฐ์น๋ ๋, ์์ธ ๊ฒ์ฆ
if (tetro.get(1) - tetro.get(0) == 1) {
if (validate(tetro.get(1) + M, 1, tetro)) {// ใ
List<Integer> copyTetro = new ArrayList<>(tetro);
solve(tetro.get(1) + M, copyTetro);
}
if (validate(tetro.get(1) - M, 3, tetro)) {// ใ
List<Integer> copyTetro = new ArrayList<>(tetro);
solve(tetro.get(1) - M, copyTetro);
}
} else { // ์ธ๋ก
if (validate(tetro.get(1) + 1, 0, tetro)) {// ใ
List<Integer> copyTetro = new ArrayList<>(tetro);
solve(tetro.get(1) + 1, copyTetro);
}
if (validate(tetro.get(1) - 1, 2, tetro)) {// ใ
List<Integer> copyTetro = new ArrayList<>(tetro);
solve(tetro.get(1) - 1, copyTetro);
}
}
}
}
private static boolean validateStick(List<Integer> tetro) { // tetro์ ๊ฐ๋ค 3๊ฐ๊ฐ 1์ ๋ฐฐ์น์ผ๋,
if (tetro.get(1) - tetro.get(0) == tetro.get(2) - tetro.get(1)) {
return true;
}
return false;
}
private static Integer sumTetro(List<Integer> tetro) {
int sum = 0;
for (int i : tetro) {
sum += ar.get(i - 1);
}
return sum;
}
private static boolean validate(int index, int direction, List<Integer> tetro) { // direction 0 ์ค๋ฅธ์ชฝ /1 ์๋/ 2์ผ์ชฝ
if (direction == 0 && index % M == 1) { // ์ค๋ฅธ์ชฝ์ผ๋ก ํ ์นธ ๊ฐ๋๋ฐ, ์ฐ์ธก ๋์ผ๋.
return false;
}
if (direction == 1 && index > N * M) { // ์๋
return false;
}
if (direction == 2 && index % M == 0) { // ์ผ์ชฝ
return false;
}
if (direction == 3 && index < 1) { // ์์ชฝ
return false;
}
if (tetro.contains(index)) {
return false;
}
return true;
}
}
'Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฐ/๋ฐฑ์ค ๊ณจ๋3] #13904 ๊ณผ์ (0) | 2022.12.31 |
---|---|
[์๋ฐ/๋ฐฑ์ค ๊ณจ๋4] #1715 ์นด๋ ์ ๋ ฌํ๊ธฐ (0) | 2022.12.31 |
[์๋ฐ/๋ฐฑ์ค ๊ณจ๋2] #2437 ์ ์ธ (1) | 2022.12.31 |
[์๋ฐ/๋ฐฑ์ค ๊ณจ๋5] #12904 A์ B (0) | 2022.12.31 |
[์๋ฐ/๋ฐฑ์ค ๊ณจ๋5] #2212 ์ผ์ (0) | 2022.12.31 |
Comments