From a99c8247f2cdf89422abf402391e5ca1c1c8dbe7 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Wed, 12 Mar 2025 09:17:58 +0900 Subject: [PATCH] =?UTF-8?q?Create=2012=20SWEA=20=EB=94=94=EC=A0=80?= =?UTF-8?q?=ED=8A=B8=EC=B9=B4=ED=8E=98.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\355\212\270\354\271\264\355\216\230.md" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "lkhyun/202503/12 SWEA \353\224\224\354\240\200\355\212\270\354\271\264\355\216\230.md" diff --git "a/lkhyun/202503/12 SWEA \353\224\224\354\240\200\355\212\270\354\271\264\355\216\230.md" "b/lkhyun/202503/12 SWEA \353\224\224\354\240\200\355\212\270\354\271\264\355\216\230.md" new file mode 100644 index 00000000..e65394fb --- /dev/null +++ "b/lkhyun/202503/12 SWEA \353\224\224\354\240\200\355\212\270\354\271\264\355\216\230.md" @@ -0,0 +1,63 @@ +```java +import java.util.*; +import java.io.*; + +public class Solution { + static int N, maxCount; + static int[][] map; + static boolean[] dessertVisited; + + static int[] di = {1, 1, -1, -1}; + static int[] dj = {1, -1, -1, 1}; + + public static void main(String[] args) throws Exception { + //System.setIn(new FileInputStream("sample_input.txt")); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine()); + StringTokenizer st; + for (int t = 1; t <= T; t++) { + N = Integer.parseInt(br.readLine()); + map = new int[N][N]; + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < N; j++) { + map[i][j] = Integer.parseInt(st.nextToken()); + } + } + maxCount = -1; + + for (int i = 0; i < N - 1; i++) { + for (int j = 1; j < N - 1; j++) { + dessertVisited = new boolean[101]; + dessertVisited[map[i][j]] = true; + dfs(i, j, i, j, 1, 0, 0); + } + } + System.out.println("#" + t + " " + maxCount); + } + } + + static void dfs(int startI, int startJ, int curI, int curJ, int count, int d, int turn) { + + for (int newdirection = d; newdirection <= d + 1 && newdirection < 4; newdirection++) { + int newi = curI + di[newdirection]; + int newj = curJ + dj[newdirection]; + + if (newi < 0 || newi >= N || newj < 0 || newj >= N) + continue; + + if (newi == startI && newj == startJ && count >= 4) { + maxCount = Math.max(maxCount, count); + continue; + } + if (dessertVisited[map[newi][newj]]) + continue; + + dessertVisited[map[newi][newj]] = true; + dfs(startI, startJ, newi, newj, count + 1, newdirection, (newdirection == d ? turn : turn + 1)); + dessertVisited[map[newi][newj]] = false; + } + } +} + +```