Skip to content

Commit 603108f

Browse files
committed
BOJ 1941 소문난 칠공주 ✅ 🔥
1 parent 3e1ffde commit 603108f

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
class Position {
5+
int row;
6+
int col;
7+
8+
public Position(int row, int col) {
9+
this.row = row;
10+
this.col = col;
11+
}
12+
13+
public String toString() {
14+
return this.row + "/" + this.col;
15+
}
16+
}
17+
18+
public class Main {
19+
static int[][] movingDir = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
20+
static char[][] twoDStudents = new char[5][5];
21+
static char[] oneDStudents = new char[25];
22+
static boolean[][] checked = new boolean[5][5];
23+
static int answer = 0;
24+
25+
public static boolean bfs() {
26+
Queue<Position> queue = new LinkedList<>();
27+
boolean[][] visited = new boolean[5][5];
28+
int area = 0;
29+
30+
boolean flag = false;
31+
for (int row = 0; row < 5; row++) {
32+
for (int col = 0; col < 5; col++) {
33+
if (checked[row][col]) {
34+
queue.add(new Position(row, col));
35+
visited[row][col] = true;
36+
flag = true;
37+
break;
38+
}
39+
}
40+
if (flag) {
41+
break;
42+
}
43+
}
44+
45+
while (!queue.isEmpty()) {
46+
Position curPos = queue.poll();
47+
48+
for (int idx = 0; idx < movingDir.length; idx++) {
49+
int nextRow = curPos.row + movingDir[idx][0];
50+
int nextCol = curPos.col + movingDir[idx][1];
51+
52+
if (0 <= nextRow && nextRow < 5 && 0 <= nextCol && nextCol < 5) {
53+
if (!visited[nextRow][nextCol] && checked[nextRow][nextCol]) {
54+
queue.add(new Position(nextRow, nextCol));
55+
visited[nextRow][nextCol] = true;
56+
}
57+
}
58+
}
59+
area += 1;
60+
}
61+
62+
if (area == 7) {
63+
return true;
64+
}
65+
return false;
66+
}
67+
68+
public static void dfs(int curIdx, int sCount, int yCount, int curCount) {
69+
int curRow = curIdx / 5;
70+
int curCol = curIdx % 5;
71+
checked[curRow][curCol] = true;
72+
73+
if (oneDStudents[curIdx] == 'S') {
74+
sCount += 1;
75+
} else {
76+
yCount += 1;
77+
}
78+
79+
if (curCount < 7) {
80+
for (int nextIdx = curIdx + 1; nextIdx < 25; nextIdx++) {
81+
dfs(nextIdx, sCount, yCount, curCount + 1);
82+
}
83+
}
84+
85+
if (curCount == 7) {
86+
if (sCount >= 4 && bfs()) {
87+
answer += 1;
88+
}
89+
}
90+
91+
checked[curRow][curCol] = false;
92+
}
93+
94+
public static void main(String[] args) throws IOException {
95+
System.setIn(new FileInputStream("Problem\\BOJ_1941_소문난 칠공주\\input.txt"));
96+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
97+
98+
// input
99+
for (int row = 0; row < 5; row++) {
100+
twoDStudents[row] = br.readLine().toCharArray();
101+
for (int col = 0; col < 5; col++) {
102+
oneDStudents[row * 5 + col] = twoDStudents[row][col];
103+
}
104+
}
105+
106+
for (int idx = 0; idx < 25; idx++) {
107+
dfs(idx, 0, 0, 1);
108+
}
109+
110+
System.out.println(answer);
111+
}
112+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
YYYYY
2+
SYSYS
3+
YYYYY
4+
YSYYS
5+
YYYYY

0 commit comments

Comments
 (0)