Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions g0rnn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@
| 26์ฐจ์‹œ | 2025.05.26 | ๊ตฌํ˜„ | [์„œ๋ฒ„ ์ฆ์„ค ํšŸ์ˆ˜](https://school.programmers.co.kr/learn/courses/30/lessons/389479?language=java) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/106 |
| 27์ฐจ์‹œ | 2025.06.15 | Floyd-Warshall | [์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ 6๋‹จ๊ณ„ ๋ฒ•์น™](https://www.acmicpc.net/problem/1389) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/113 |
| 28์ฐจ์‹œ | 2025.06.27 | BFS | [์ƒ์–ด ์ค‘ํ•™๊ต](https://www.acmicpc.net/problem/21609) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/115 |
| 29์ฐจ์‹œ | 2025.07.22 | combination | [์†Œ๋ฌธ๋‚œ ์น ๊ณต์ฃผ](https://www.acmicpc.net/problem/1941) | https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/119 |
---
72 changes: 72 additions & 0 deletions g0rnn/combination/29-g0rnn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package beakjoon;

import java.util.*;
import java.io.*;

public class Sol1941 {

static int ans = 0;
static char[][] board = new char[5][5];
static int[][] offset = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
static List<Integer> selected = new ArrayList<>();

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 5; i++) {
board[i] = br.readLine().toCharArray();
}
br.close();

comb(0, 0);
System.out.println(ans);
}

// ์กฐํ•ฉ์„ ๋งŒ๋“ค๊ณ  ๋ฝ‘์€ ์นธ์ด ์—ฐ์†์ ์ด๋ฉด์„œ & S๊ฐ€ 4๊ฐœ ์ด์ƒ์ธ์ง€ ํ™•์ธ
private static void comb(int start, int depth) {
if (depth == 7) {
if (isValid()) {
ans++;
}
return;
}

for (int i = start; i < 25; i++) {
selected.add(i);
comb(i + 1, depth + 1);
selected.remove(selected.size() - 1);
}
}

private static boolean isValid() {
int cntS = 0;
boolean[] check = new boolean[25];
Queue<Integer> q = new ArrayDeque<>();
q.offer(selected.get(0));
check[selected.get(0)] = true;

int countVisited = 1; // ๋ฐฉ๋ฌธํ•œ ์นธ์˜ ์ˆ˜
if (board[selected.get(0) / 5][selected.get(0) % 5] == 'S') cntS++;

while (!q.isEmpty()) {
int now = q.poll();
int x = now % 5;
int y = now / 5;

for (int[] o : offset) {
int nx = x + o[0];
int ny = y + o[1];
int next = ny * 5 + nx;

if (nx < 0 || nx >= 5 || ny < 0 || ny >= 5) continue;
if (!selected.contains(next)) continue; // ๋‚ด๊ฐ€ ์„ ํƒํ•œ ์นธ๋งŒ ์ด๋™ํ•˜๋„๋ก
if (check[next]) continue;

check[next] = true;
q.offer(next);
countVisited++;
if (board[ny][nx] == 'S') cntS++;
}
}
return countVisited == 7 && cntS >= 4;
}
}