๋ฐฑ์ค 18430 ๋ฌด๊ธฐ๊ณตํ[JAVA]
- -
๐๋งํฌ
18430 ๋ฌด๊ธฐ ๊ณตํ gold4
โ ๏ธ๋ฌธ์
๊ณตํ์ ๊ธธ๋์ด๋ ์ธ๋ถ์ ์นจ๋ต์ผ๋ก๋ถํฐ ๋ง์์ ์งํฌ ์ ์๋ ๋ถ๋ฉ๋ ๋ฌด๊ธฐ๋ฅผ ๊ฐ๋ฐํ๋ ๊ณตํ์๋ค. ๊ธธ๋์ด๋ ๋ถ๋ฉ๋ ์ ์์ ์ํ ๊ณ ๊ธ ๋๋ฌด ์ฌ๋ฃ๋ฅผ ๊ตฌํ๋ค. ์ด ๋๋ฌด ์ฌ๋ฃ๋ NxMํฌ๊ธฐ์ ์ง์ฌ๊ฐํ ํํ์ด๋ฉฐ ๋๋ฌด ์ฌ๋ฃ์ ๋ถ์๋ง๋ค ๊ทธ ๊ฐ๋๊ฐ ์กฐ๊ธ์ฉ ๋ค๋ฅด๋ค.
์๋ฅผ ๋ค์ด ๋๋ฌด ์ฌ๋ฃ์ ํฌ๊ธฐ๊ฐ 2x3์ผ ๋๋ ๋ค์๊ณผ ๊ฐ์ด ์ด 6์นธ์ผ๋ก ๊ตฌ์ฑ๋๋ค.
๊ธธ๋์ด๋ ์ด์ฒ๋ผ ๋์ ์ฌ๊ฐํ ํํ์ ๋๋ฌด ์ฌ๋ฃ๋ฅผ ์๋ผ์ ์ฌ๋ฌ ๊ฐ์ ๋ถ๋ฉ๋์ ๋ง๋ค๊ณ ์ ํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ถ๋ฉ๋์ ํญ์ 3์นธ์ ์ฐจ์งํ๋ ‘ใฑ’๋ชจ์์ผ๋ก ๋ง๋ค์ด์ผ ํ๋ค. ๋ฐ๋ผ์ ๋ถ๋ฉ๋์ ๊ฐ๋ฅํ ๋ชจ์์ ๋ค์๊ณผ ๊ฐ์ด ์ด 4๊ฐ์ง๋ค.
์ด๋ ๋ถ๋ฉ๋์ ์ค์ฌ์ด ๋๋ ์นธ์ ๊ฐ๋์ ์ํฅ์ 2๋ฐฐ๋ก ๋ฐ๋๋ค. ์ ๊ทธ๋ฆผ์์ ๋ ธ๋์์ผ๋ก ์น ํ ๋ถ๋ถ์ด ‘์ค์ฌ์ด ๋๋ ์นธ’์ด๋ค. ์๋ฅผ ๋ค์ด ์์ ์์์์๋ ๋ค์๊ณผ ๊ฐ์ด 2๊ฐ์ ๋ถ๋ฉ๋์ ๋ง๋ค ์ ์์ผ๋ฉฐ, ์ด๋ ๋ง๋ค์ด์ง๋ ๋ถ๋ฉ๋๋ค์ ๊ฐ๋์ ํฉ์ 46์ผ๋ก ์ด๋ณด๋ค ๊ฐ๋์ ํฉ์ด ๋์์ง๋ ๊ฒฝ์ฐ๋ ์๋ค.
๋ํ ๋๋ฌด ์ฌ๋ฃ์ ํน์ ์์น๋ ์์ ์ฌ์ฉํ์ง ์์๋ ๊ด์ฐฎ๋ค. ์๋ฅผ ๋ค์ด ์์ ์์์์ ๋ค์๊ณผ ๊ฐ์ด 1๊ฐ์ ๋ถ๋ฉ๋๋ง์ ๋ง๋ค์ด๋ ๊ด์ฐฎ๋ค. ๋ค๋ง, ์ด๋ ๊ฒ ๋ง๋ค๊ฒ ๋๋ฉด ๋ถ๋ฉ๋๋ค์ ๊ฐ๋์ ํฉ์ด 18์ด ๋๊ธฐ ๋๋ฌธ์ ๋นํจ์จ์ ์ด๋ค.
๋๋ฌด ์ฌ๋ฃ์ ํํ์ ๊ฐ ์นธ์ ๊ฐ๋๊ฐ ์ฃผ์ด์ก์ ๋, ๊ธธ๋์ด๊ฐ ๋ง๋ค ์ ์๋ ๋ถ๋ฉ๋๋ค์ ๊ฐ๋ ํฉ์ ์ต๋๊ฐ์ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์๋ ๊ธธ๋์ด๊ฐ ๊ฐ์ง๊ณ ์๋ ๋๋ฌด ์ฌ๋ฃ์ ์ธ๋ก, ๊ฐ๋ก ํฌ๊ธฐ๋ฅผ ์๋ฏธํ๋ ๋ ์์ฐ์ N, M์ด ์ฃผ์ด์ง๋ค. (1 ≤ N, M ≤ 5) ๋ค์ N๊ฐ์ ์ค์ ๊ฑธ์ณ์, ๋งค ์ค๋ง๋ค ๋๋ฌด ์ฌ๋ฃ์ ๊ฐ ์์น์ ๊ฐ๋๋ฅผ ๋ํ๋ด๋ M๊ฐ์ ์์ฐ์ K๊ฐ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถ๋์ด ์ฃผ์ด์ง๋ค. (1 ≤ K ≤ 100)
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ๊ธธ๋์ด๊ฐ ๋ง๋ค ์ ์๋ ๋ถ๋ฉ๋๋ค์ ๊ฐ๋ ํฉ์ ์ต๋๊ฐ์ ์ถ๋ ฅํ๋ค.
๋จ, ๋๋ฌด ์ฌ๋ฃ์ ํฌ๊ธฐ๊ฐ ์์์ ๋ถ๋ฉ๋์ ํ๋๋ ๋ง๋ค ์ ์๋ ๊ฒฝ์ฐ๋ 0์ ์ถ๋ ฅํ๋ค.
๐๋ถ์
DFS ๋ฐฑํธ๋ํน์ ์ฌ์ฉํ์ฌ ํด๊ฒฐํ์๋ค. ์์๋๋ก ์ํํ๋ฉด์ ๋ถ๋ฉ๋์ ๋ง๋ค์ด๋ณด๊ณ ๋ค์ ๋ค๋ก ๊ฐ์ ๋ค๋ฅธ ๋ชจ์์ ๋ถ๋ฉ๋์ ๋ง๋ค์ด๋ณด๋ฉด์ ๊ฐ๋ฅํ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฒดํฌํ์ฌ ์ต๋๊ฐ์ ๋ฝ์๋ด๋ฉด ๋๋ ๋ฌธ์ ๋ผ๊ณ ์๊ฐํ๋ค. ์ฃผ์ ํ ์ ์ ์ํํ๋ฉด์ visited์ฒดํฌ์ ์ผ์ด์ค๋ฅผ ์ฌ๋ฌ ๊ฐ๋ก ๋๋์ด์ ์งํํ๋ฉด ์ค์๊ฐ ๋ง์ด ๋์ฌ ๊ฒ ๊ฐ์ ์ผ์ด์ค๋ฅผ ๋๋๋ ๋ถ๋ถ์ ์ข ์ฃผ์ํด์ ๊ตฌํํ๋ ค๊ณ ๋ ธ๋ ฅํ๋ค.
1. ๋ณ์ ์ ์ธ์ ์๋ ์ฝ๋์ ๊ฐ์ด ํ์๋ค. ์ผ์ด์ค์ ๊ฐ๋ ์ฑ์ ๋์ด๊ณ ์ถ์ด์ int๋ฐฐ์ด๋ณด๋ค๋ ํด๋์ค ๊ฐ์ฒด๋ฅผ ํ์ฉํ์๊ณ , ๊ฐ๋๋ฅผ ์ ์ฅํ๋ board์ ํด๋น ์์น๊ฐ ์ด๋ฏธ ๋ถ๋ฉ๋์ ์ฌ์ฉ๋ ๋ถ๋ถ์ธ์ง ํ๋จํ๋ visited๋ฐฐ์ด์ ์ ์ธํ๋ค.
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N, M;
static int[][] board;
static boolean[][] visited;
static Coordinate[][] boomerangCase = {
{new Coordinate(1, 0), new Coordinate(0, 0), new Coordinate(0, 1)},
{new Coordinate(1, 0), new Coordinate(0, 0), new Coordinate(0, -1)},
{new Coordinate(-1, 0), new Coordinate(0, 0), new Coordinate(0, 1)},
{new Coordinate(-1, 0), new Coordinate(0, 0), new Coordinate(0, -1)},
};
static int result = 0;
2. ๋ค์์ ์ขํ ํด๋์ค๋ฅผ ๋ง๋ค์๋ค. x, y ์ขํ๋ฅผ ๋ฉค๋ฒ ๋ณ์๋ก ์ ์ธํ๊ณ , dfs์ ๋ค์ ์ขํ๋ฅผ ๊ณ์ํ์ฌ ์ ๋ ฅ์ํฌ ์์ ์ด๋ผ ๋ค์ ์ขํ๋ฅผ ๊ณ์ฐํ์ฌ ๋ฐํํ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด ๋์๋ค.
class Coordinate {
int x;
int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public Coordinate nextCoordinate(int m){
int x2 = x;
int y2 = y;
y2++;
if(y2 >= m){
x2++;
y2 = 0;
}
return new Coordinate(x2, y2);
}
}
3. dfs๊ธฐ๋ฅ์ ๊ตฌํํ๋ค. 0,0 ์ขํ๋ถํฐ ์์ํ์ฌ ํ์ฌ ์ขํ๊ฐ ์ค์ฌ์ด ๋๋ ๋ถ๋ฉ๋์ ๋ง๋ค ์ ์๋ ์ง ์ฐ์ ํ์ธํ๋ค.
for (Coordinate coordinate : coordinates) {
if (!checkInside(cur.x + coordinate.x, cur.y + coordinate.y) || visited[cur.x + coordinate.x][cur.y + coordinate.y]) {
able = false;
break;
}
}
4. ๊ฐ๋ฅํ๋ค๋ฉด ํด๋น ๋ถ๋ฉ๋ ์์น์ ๊ฐ๋๋ฅผ ๋ชจ๋ ๋ํ๊ณ visited(์ด๋ฏธ ๋ถ๋ฉ๋์ ๋ง๋ ์์น)๋ true๋ก ๊ณ์ฐํ์ฌ ๋ฐํํ๋ค. ์ดํ, ๋ค์ ์์น๋ก ๊ฐ์ ๋ถ๋ฉ๋์ ๋ง๋ค์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ํ์ฌ ์คํ๋๋ ํจ์๋ฅผ ์ฌ๊ท์ ์ผ๋ก ์คํ์ํจ๋ค.
5.๊ณ์ฐ์ ์ฌ๊ท์ ์ผ๋ก ๊ณ์ฐํ ๋ค์ ํจ์์ ๋งก๊ฒผ๊ธฐ ๋๋ฌธ์ ๊ณ์ฐ์ด ๋๋ฌ์ ๋, ํ์ฌ ์ฒดํฌํ๋ ๊ฐ๋์ ๊ฐ๊ณผ ๋ง๋ค์๋ ์์น๋ฅผ ๋ณต๊ท ์์ผ์ผ ํ๋ค. (๋ค๋ฅธ ๋ชจ์์ ๋ถ๋ฉ๋๋ ๊ณ์ฐํด์ผ ํ๊ธฐ ๋๋ฌธ)
6. ํ์ฌ ์์น๊ฐ ์ค์ฌ์ด ๋๋ ๋ถ๋ฉ๋์ด ํฌํจ๋์ง ์๋๋ผ๋ ์ต๋๊ฐ์ด ๋๋ ๊ฒฝ์ฐ๊ฐ ์์ผ๋ฏ๋ก ํ์ฌ ์์น๊ฐ ์ค์ฌ์ด ๋๋ ๋ถ๋ฉ๋์ ๊ฒฝ์ฐ๊ฐ ๋๋ฌ์ผ๋ฉด ํ์ฌ ์์น๋ฅผ ๊ทธ๋ฅ ์คํตํ๊ณ ๋ค์ ์์น๋ฅผ ๊ณ์ฐํ๋ ๋ก์ง๋ ์์ด์ผ ํ๋ค. ๊ทธ๋ฌ๋ฏ๋ก ํ์ฌ ์์น๋ฅผ ๊ณ์ฐํ์ง ์๋ ์ฌ๊ท ํจ์๋ ํฌํจ์ํจ๋ค.
public static void makeBoomerang(Coordinate cur, int totalStrength) {
if(cur.x >= N){
result = Math.max(result, totalStrength);
return;
}
for (Coordinate[] coordinates : boomerangCase) {
boolean able = true;
for (Coordinate coordinate : coordinates) {
if (!checkInside(cur.x + coordinate.x, cur.y + coordinate.y) || visited[cur.x + coordinate.x][cur.y + coordinate.y]) {
able = false;
break;
}
}
if(able){
for (Coordinate coordinate : coordinates) {
if(coordinate.x == 0 && coordinate.y == 0){
totalStrength += board[cur.x + coordinate.x][cur.y + coordinate.y] * 2;
}else{
totalStrength += board[cur.x + coordinate.x][cur.y + coordinate.y];
}
visited[cur.x + coordinate.x][cur.y + coordinate.y] = true;
}
makeBoomerang(cur.nextCoordinate(M), totalStrength);
for (Coordinate coordinate : coordinates) {
if(coordinate.x == 0 && coordinate.y == 0){
totalStrength -= board[cur.x + coordinate.x][cur.y + coordinate.y] * 2;
}else{
totalStrength -= board[cur.x + coordinate.x][cur.y + coordinate.y];
}
visited[cur.x + coordinate.x][cur.y + coordinate.y] = false;
}
}
}
makeBoomerang(cur.nextCoordinate(M), totalStrength);
}
public static boolean checkInside(int x, int y){
return x >= 0 && x < N && y >= 0 && y < M;
}
โ์ ์ฒด ์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class Main {
//dfs
//์ ๊ฒฝ์ธ ์ : ์ผ์ด์ค ๋๋๊ธฐ
// ํ์ถ ์กฐ๊ฑด
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N, M;
static int[][] board;
static boolean[][] visited;
static Coordinate[][] boomerangCase = {
{new Coordinate(1, 0), new Coordinate(0, 0), new Coordinate(0, 1)},
{new Coordinate(1, 0), new Coordinate(0, 0), new Coordinate(0, -1)},
{new Coordinate(-1, 0), new Coordinate(0, 0), new Coordinate(0, 1)},
{new Coordinate(-1, 0), new Coordinate(0, 0), new Coordinate(0, -1)},
};
static int result = 0;
public static void main(String[] args) throws IOException {
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
board = new int[N][M];
visited = new boolean[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
makeBoomerang(new Coordinate(0, 0), 0);
System.out.println(result);
}
public static void makeBoomerang(Coordinate cur, int totalStrength) {
if(cur.x >= N){
result = Math.max(result, totalStrength);
return;
}
for (Coordinate[] coordinates : boomerangCase) {
boolean able = true;
for (Coordinate coordinate : coordinates) {
if (!checkInside(cur.x + coordinate.x, cur.y + coordinate.y) || visited[cur.x + coordinate.x][cur.y + coordinate.y]) {
able = false;
break;
}
}
if(able){
for (Coordinate coordinate : coordinates) {
if(coordinate.x == 0 && coordinate.y == 0){
totalStrength += board[cur.x + coordinate.x][cur.y + coordinate.y] * 2;
}else{
totalStrength += board[cur.x + coordinate.x][cur.y + coordinate.y];
}
visited[cur.x + coordinate.x][cur.y + coordinate.y] = true;
}
makeBoomerang(cur.nextCoordinate(M), totalStrength);
for (Coordinate coordinate : coordinates) {
if(coordinate.x == 0 && coordinate.y == 0){
totalStrength -= board[cur.x + coordinate.x][cur.y + coordinate.y] * 2;
}else{
totalStrength -= board[cur.x + coordinate.x][cur.y + coordinate.y];
}
visited[cur.x + coordinate.x][cur.y + coordinate.y] = false;
}
}
}
makeBoomerang(cur.nextCoordinate(M), totalStrength);
}
public static boolean checkInside(int x, int y){
return x >= 0 && x < N && y >= 0 && y < M;
}
}
class Coordinate {
int x;
int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public Coordinate nextCoordinate(int m){
int x2 = x;
int y2 = y;
y2++;
if(y2 >= m){
x2++;
y2 = 0;
}
return new Coordinate(x2, y2);
}
}
๐๊ฒฐ๊ณผ
๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๋ค๋ฅธ ๊ฒฐ๊ณผ์ ๋นํด ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ ๋ฐฐ์ ๋ ๋ ์ปธ๋ค. ์๋ง ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํจ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ฆ๊ฐํ๊ณ ๋ค์ ์ขํ๋ฅผ ๊ณ์ฐํ ๋ ๊ณ์ํด์ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ๋ฐํํ๋ ์ ์ด ์ข ํฌ๊ฒ ์์ฉํ๋ ๊ฒ ๊ฐ๋ค. ์ด๋ฅผ ์์ ํ๋ฉด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ค์ผ ์ ์์ ๊ฒ์ด๋ค.
→ ํด๋น ๋ถ๋ฌธ๋ง ์์ ํ์ ๋ ๊ฒฐ๊ณผ
** dfs๋ ์ฌ๊ท ๊ด๋ จ ๋ค๋ฃฐ ๋ ๊ฐ์ฒด ๋ณต์ฌ ์ฃผ์ํ๊ธฐ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 2668 ์ซ์๊ณ ๋ฅด๊ธฐ[JAVA] (0) | 2023.09.26 |
---|---|
๋ฐฑ์ค 1106 ํธํ [JAVA] (0) | 2023.08.02 |
๋ฐฑ์ค 1765 ๋ญ์ธ์ ํ ์ ํ๊ธฐ[JAVA] (0) | 2023.08.02 |
๋ฐฑ์ค 1068 ํธ๋ฆฌ[JAVA] (0) | 2023.07.30 |
์์คํ ๊ณต๊ฐ ๊ฐ์ฌํฉ๋๋ค