-
Notifications
You must be signed in to change notification settings - Fork 1
29-g0rnn #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
wnsmir
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5 by 5๋ ์ด๋ฏธ ์ ํด์ง ์ฌ์ค์ด๋ผ ์์ ํ์์ ๋ฐฉํฅ์ผ๋ก ์ ๊ทผํ์ต๋๋ค.
ํ์นธ์ ๋ํด ๊ฐ๋ฅํ ์ํ์ข์ฐ์ ๋ชจ๋ ๊ฒฝ์ฐ์์๋ฅผ ํด๋ณด๋ ค๊ณ ํ์ผ๋ ์๋ชป๋๋ค๋๊ฑธ ๊นจ๋ฌ์์ต๋๋ค.
bfs๋ฅผ ๊ฒฐ๊ตญ ๊ฒฝ์ฐ์์๋งํผ ๋ค ๋๋ ค๋ด์ผํ๊ธฐ ๋๋ฌธ์ ๋๋ค. ์ฆ ๋ฐ๋๋ก์๊ฐํด๋ณด๋ฉด
์กฐํฉ์ ๋ฏธ๋ฆฌ ๋ง๋ค์ด๋๊ณ , bfs๋ก ์ํ์ข์ฐ๋ก ์ด์ด์ ธ์๋์ง ํ์ธ์ ํด์ฃผ๋ ๊ฒ์ ๋๋ค.
๊ตฌํ์ ๊ฐ๋จํ์ต๋๋ค! ์ ์ ํ ๋ฌธ์ ๋ค์!!
from itertools import combinations
from collections import deque
board = [list(input().strip()) for _ in range(5)]
ans = 0
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
def is_connected(cands):
q = deque([cands[0]])
visited = set([cands[0]])
cands_set = set(cands)
while q:
cur = q.popleft()
x, y = cur // 5, cur % 5
for d in range(4):
nx, ny = x+dx[d], y+dy[d]
if 0<=nx<5 and 0<=ny<5:
nxt = nx*5+ny
if nxt in cands_set and nxt not in visited:
visited.add(nxt)
q.append(nxt)
return len(visited) == 7
for cands in combinations(range(25), 7):
S_cnt = sum(1 for idx in cands if board[idx//5][idx%5]=='S')
if S_cnt >= 4 and is_connected(cands):
ans += 1
print(ans)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฒ์์ ์ ๋ ๊ท ํธ๋์ฒ๋ผ dfs ๋ก ์ ๊ทผํ์ต๋๋ค.
๊ณจ๋ 3 ๋ฌธ์ ๋ฅผ 20๋ถ๋ง์?? ํ๋ฉด์ ์ ์ถํ๋๋ฐ...
ํ๋ ธ์ต๋๋ค. ๊ฐ ๋จ๋๋ผ๊ตฌ์.
๊ท ํธ๋ ๋ง๋๋ก ํ
์ผ์์ ์ฃผ์ด์ง ๋ต 2๊ฐ๋ dfs๋ก๋ ๋ง๋ค ์ ์๋ ํํ๋๊ตฐ์.
๊ทธ๋์ ์ ๋ bfs๋ก 25๊ฐ์ค 7๊ฐ๋ฅผ ๋ฝ๊ณ , ๊ทธ 7๊ฐ ์๋ฆฌ๊ฐ ์ฐ๊ฒฐ๋์๋์ง, ๊ทธ๋ฆฌ๊ณ ์ด๋ค์์จ์ ํ๊ฐ 4์ธ ์ด์ ์ธ์ง๋ฅผ ๊ฒ์ฌํ์ต๋๋ค.
์กฐํฉ์ ์ ํฌ๊ฐ ์๋ ๋ง์ดํด์ ๋น ๋ฅด๊ฒ ํ์ง๋ง, ์กฐ๊ฑด๊ฒ์ฌ๊ฐ ์๊ฐ๋ณด๋ค ๊น๋ค๋กญ๋๊ตฐ์.
๊ฐ๋จํ ํ๊ณ ์ถ์ ๋ง์์ visited๋ selected ๊ฒ์ฌ๋ ํ์ง ์๊ณ ๊ทธ๋ฅ dir์ ํตํด ์ด๋ํ y_, x_๊ฐ candi์ ์์ผ๋ฉด ์ฐ๊ฒฐ๋์๋ค๊ณ ํ๋จํ์ด์.
๊ทผ๋ฐ ์ด ๋ฐฉ์์ ๋๋ช
๋๋ช
์ธ๋ช
์ด๋ ๊ฒ ๋จ์ด์ ธ์๋ ๊ฒฝ์ฐ์๋ true๋ฅผ ๋ฐํํ๋ ์ค๋ฅ๊ฐ ์์์ต๋๋ค.
๊ทธ๋์ ์ ๋ ๊ท ํธ๋์ด๋ ๊ฑฐ์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๊ตฌํ์ ํ ๊ฒ ๊ฐ์์.
์ฅ๋ณด๊ณ ๊ฐ๋๋ค.
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int N = 5;
const int M = 5;
const int OFFSET[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<string> board(N);
bool isVaild(vector<int>& candi) {
vector<bool> visited(7, false);
queue<int> q;
q.push(0);
visited[0] = true;
int Y = 0, cnt = 0;
while (!q.empty() && ++cnt) {
int now = q.front();
q.pop();
int y = candi[now] / N;
int x = candi[now] % M;
if (board[y][x] == 'Y') Y++;
for (int dir = 0; dir < 4; dir++) {
int y_ = y + OFFSET[dir][0];
int x_ = x + OFFSET[dir][1];
for (int i = 0; i < 7; i++) {
if (!visited[i] && y_ == candi[i] / N && x_ == candi[i] % M) {
visited[i] = true;
q.push(i);
}
}
}
}
return cnt == 7 && Y < 4;
}
int comb(vector<int>& candi, int k) {
if (candi.size() == 7) {
if (isVaild(candi)) return 1;
return 0;
}
int cnt = 0;
for (int index = k; index < N * M; index++) {
candi.push_back(index);
cnt += comb(candi, index + 1);
candi.pop_back();
}
return cnt;
}
int main() {
for (int y = 0; y < N; y++) {
cin >> board[y];
}
vector<int> candi;
cout << comb(candi, 0) << '\n';
return 0;
}
9kyo-hwang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋์ถฉ ๋ณด๊ณ "์ ๋ฐฑํธ๋ํน ๋ธ๊นํ๋ฉด ๋๊ฒ ๋ค"ํ๊ณ ๋๋ค๋ค๋ค 10๋ถ ์ฝ๋ ์ง๊ณ ์ ์ถํ๋๋ฐ 11% ์ฒ ํด ใ ใ ์ ๋ง ๊ทธ๋ฐ ๊ฒ ์๋์ฌ์ ์๋...
๋ค์ ๋ฌธ์ ์์๋ฅผ ๋ณด๋๊น,
.....
SYSYS
.Y...
.S...
.....
์ด ๊ฒฝ์ฐ๋ ๋ฐฑํธ๋ํน์ผ๋ก ๋ง๋ค์ด์ง ์ ์๋ ๋ชจ์์ด๋๊ตฐ์. ์ ํ ํธ๋ฆฌ์ค ์กฐ๊ฐ ์๊ธฐ ํ์ จ๋์ง ๋ฑ ๊นจ๋ฌ์์ต๋๋ค.
์ฌ๊ท ํ์์ ํ๋ฉด์ ์กฐ๊ฑด์ ๋ง๋ ํฌ๊ธฐ๊ฐ 7์ธ ์ฐ๊ฒฐ ์์๋ฅผ ๋ง๋๋ ๊ฒ ์๋๋ผ, ๊ฑฐ๊พธ๋ก ๊ฐ๋ฅํ ์ฐ๊ฒฐ ์์๋ฅผ ๋ค ํ์ด๋ณธ ๋ค ์กฐ๊ฑด์ ๋ถํฉํ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์นด์ดํ ํ๋ ์์ผ๋ก ์ ๊ทผํ์ต๋๋ค.
์๋๋ next_permutation์ ์จ์ ์ฝ๋๋ฅผ ๊ธ๋ฐฉ ์งฐ๋๋ฐ ์๊ฐ์ด ๋๋ฌด ์ค๋ ๊ฑธ๋ ค์ ์งฑ๊ตฌ๋ฅผ ๋ ๊ตด๋ ค๋ดค์ต๋๋ค. ์ต์ด์ ํ ๋ฒ ๊ฐ ์นธ๋ง๋ค "์ธ์ ํ ์นธ" ์ ๋ณด๋ฅผ ๋ฏธ๋ฆฌ ์บ์ฑํ๋ฉด ๋๊ฒ ๋๋ผ๊ตฌ์. ๊ทธ๋์ ์ธ์ ์นธ ๊ฒ์ฌ ๋ถ๋ถ์ด ๊ฐ๊ฒฐํด์ก๋ค์ ใ
#include <bits/stdc++.h>
using namespace std;
array<char, 25> board;
vector<int> adj[25];
array<bool, 25> selected;
bool IsConnectedComponent() {
queue<int> q;
array<bool, 25> visited = {false, };
for(int i = 0; i < 25; ++i) {
if(selected[i]) {
q.emplace(i);
visited[i] = true;
break;
}
}
int count = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
for(int v : adj[u]) {
if(selected[v] && !visited[v]) {
visited[v] = true;
q.emplace(v);
++count;
}
}
}
// ๊ณผ๋ฐ์์ Combination ํจ์์์ ์ฌ๊ธฐ๋ก ๋ค์ด์ฌ ๋ ์ด๋ฏธ ๋ณด์ฅ๋ ์ํ. ๋ฐ๋ผ์ 7๊ฐ ์นธ์ด ์ฐ๊ฒฐ๋๋์ง๋ง ํ์ธ
return count == 7;
}
int Combination(int index = 0, int student = 0, int dasom = 0) {
if(dasom + (7 - student) < 4) { // ๋จ์ ๋ชจ๋ ํ์์ด ์ด๋ค์ ํ๊ฐ ๋์ด๋ ๊ณผ๋ฐ์ด ์๋๋ ๊ฒฝ์ฐ
return 0;
}
if(student == 7) {
return IsConnectedComponent();
}
int ans = 0;
for(int i = index; i < 25; ++i) {
selected[i] = true;
ans += Combination(i + 1, student + 1, dasom + (board[i] == 'S'));
selected[i] = false;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
for(int i = 0; i < 5; ++i) {
string row; cin >> row;
for(int j = 0; j < 5; ++j) {
board[i * 5 + j] = row[j];
}
}
for (int i = 0; i < 25; i++) {
int r = i / 5, c = i % 5;
// ์ํ์ข์ฐ ์ธ์ ํ ์นธ
if (r > 0) adj[i].emplace_back(i-5);
if (r < 4) adj[i].emplace_back(i+5);
if (c > 0) adj[i].emplace_back(i-1);
if (c < 4) adj[i].emplace_back(i+1);
}
cout << Combination();
return 0;
}
kokeunho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์คํฌ ๋นํด์ ์กฐํฉํ ํ์ bfs๋ก ํธ๋ ๊ฒ์ ์์๋ฒ๋ ธ์ต๋๋ค.
๋ง์ฝ ๋ชฐ๋๋๋ผ๋ฉด ์ ๋ dfs๋ก ์๋ํด๋ณด๋ค ์คํจํ์ ๊ฒ ๊ฐ์ต๋๋ค.
์
์ถ๋ ฅ ์์์ ๊ฒฝ๋ก๋ฅผ dfs๋ก ์ฐพ์ ์ ์๋ค๋ ๊ฒ์ ์๊ฐํ์ง ๋ชปํ๊ณ ์์๊ธฐ ๋๋ฌธ์
๋๋ค.
์กฐํฉ์ผ๋ก ์ฐ์ 7๋ช
์ ๊ณ ๋ฅด๊ณ
bfs ์์ ์ง์ (๊ฐ์ฅ ์ฒซ๋ฒ์งธ selected ์ง์ )์ ์ฐพ์ ๋ค
ํด๋น ์ง์ ์์ bfs๋ฅผ ํธ์ถํ์ฌ selected ๋ ์ง์ ๋ค ๊ฒฝ๋ก๋ก ํ์ธํ๋ฉด์
๊ฒฝ๋ก๊ฐ ์ด 7์นธ์ธ์ง, ์ด๋ค์ํ๊ฐ 4๋ช
์ด์์ธ์ง ์ฒดํฌํ์์ต๋๋ค.
์๊ณ ํ์ จ์ต๋๋ค~
Details
import java.util.*;
public class Main {
static char[][] map = new char[5][5];
static boolean[][] selected = new boolean[5][5];
static boolean[][] visited = new boolean[5][5];
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int totalCount = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
String line = sc.nextLine();
for (int j = 0; j < 5; j++) {
map[i][j] = line.charAt(j);
}
}
sevenPrincesses(0, 0, 0);
System.out.print(totalCount);
}
static void sevenPrincesses(int row, int col, int count) {
if (row == 5 || count == 7) {
if (count == 7) startBfs();
return;
}
int newCol = (col + 1) % 5;
int newRow = row;
if (newCol == 0) newRow++;
selected[row][col] = true;
sevenPrincesses(newRow, newCol, count+1);
selected[row][col] = false;
sevenPrincesses(newRow, newCol, count);
}
static void startBfs() {
for (int i = 0; i < 5; i++) {
Arrays.fill(visited[i], false);
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if(selected[i][j]) {
bfs(i, j);
return;
}
}
}
}
static void bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{x, y});
visited[x][y] = true;
int sCount = 0, yCount = 0;
while (!queue.isEmpty()) {
int[] current = queue.poll();
int cx = current[0], cy = current[1];
if (map[cx][cy] == 'S') {
sCount++;
} else yCount++;
for (int i = 0; i < 4; i++) {
int nx = cx + dx[i];
int ny = cy + dy[i];
if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5
&& !visited[nx][ny] && selected[nx][ny]) {
visited[nx][ny] = true;
queue.add(new int[]{nx, ny});
}
}
}
if (sCount+yCount==7 && sCount >= 4) totalCount++;
}
}
๐ ๋ฌธ์ ๋งํฌ
์๋ฌธ๋ ์น ๊ณต์ฃผ
โ๏ธ ์์๋ ์๊ฐ
1h
โจ ์๋ ์ฝ๋
์ฒ์์ ๊ณจ๋3์ธ ๊ฒ ์น๊ณ ์ฌ์์ ๋นจ๋ฆฌ ํ๊ณ ๋ค๋ฅธ ๋ฌธ์ ๋ฅผ ์๊ฐํ๋ คํ์ต๋๋ค. ํ์ง๋ง ์ฒ์ ์ ๊ทผ์ด ๋๋ฌด๋ ์๋ชป๋๋ค๋ณด๋.. ์๊ฐ์ ๋ง์ด ์ฐ๊ฒ ๋์๊ณ ๋ ๊ทธ๋ ๊ฒ ์ฌ์ด๊ฒ ๊ฐ์ง๋ ์์์ ๊ณต์ ํ๊ฒ ๋์์ต๋๋ค.
์์ ํ ํธ๋ฆฌ์ค ๋ฌธ์ ๊ธฐ์ตํ์๋์? dfs๋ก 5๊ฐ์ ํ ํธ๋ฆฌ์ค ๋ชจํ์ ๋ง๋ค๊ณ ์ด์ฉ๊ตฌ ์ ์ฉ๊ตฌ ํ๋๊ฑฐ.. ๊ฐ์๊ธฐ ์ด ๋ฌธ์ ๋ฅผ ๋ณด๊ณ ๊ทธ ๋ฐฉ๋ฒ์ด ๋ ์ฌ๋์ต๋๋ค. dfs๋ก 7๊ฐ์ ์นธ์ ๋ง๋ค๊ณ 'S'์ ๊ฐ์๊ฐ 4๊ฐ ์ด์์ธ์ง ์ฒดํฌํ์ต๋๋ค. dfs๋ก ์ธ์ ํ ์นธ์ ์ ๊ทผํ์ผ๋ ๋น์ฐํ ์ฐ์ํ๊ตฌ์. ํ์ง๋ง ๊ธฐ๋ณธ ํ ์ผ๋ ํต๊ณผ ๋ชปํ์ต๋๋ค... ๊ธฐ๋ณธ ํ ์คํธ ์ผ์ด์ค์ ํํธ๋ฅผ ๋ณด๋ ์๋์ ๊ฒฝ์ฐ๊ฐ ์์๋๋ฐ ์ด๊ฑด dfs๋ก๋ ์ ๋๋ก ๋ง๋ค ์ ์๋ ๋ชจํ์ด์ฃ ..^^; ์ด๊ฑธ ๋ฉ๋ํ๋๋ฐ ์๊ฐ์ด ์ข ๊ฑธ๋ ธ์ต๋๋ค.!
๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ ํ์ด์ผํ ๊น์? ์ ๋ต์ ์กฐํฉ์ ๋๋น :)
์๋ง ๋ค๋ค ์ ๊ทผ์ ์กฐํฉ์ผ ๊ฑฐ์์. ์กฐํฉ์ ์์ฃผ ๋ง๋ค์์ผ๋ ๋์ด๊ฐ๊ณ , ์ธ์ ํ๋ฉด์ 'S'๊ฐ 4๊ฐ ์ด์์ธ์ง ํ์ธํด์ฃผ๋ฉด ๋ฉ๋๋ค.
์ ๋ bfs๋ก
๋ด๊ฐ ์ ํํ ์นธ๋ง ์ด๋ํ๋ฉด์'S'๊ฐ 4๊ฐ ์ด์์ธ์ง ํ์ธํ์ต๋๋ค. bfs๋ก ํ๋๊น ์ฐ์์ ์ธ๊ฑด ๋น์ฐํ๊ตฌ์.๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ ํ์ด๋ฅผ ๋ณด๋ฉด select์ ์ด์ํ ๊ฐ์ ๋ฃ๊ณ ์์ต๋๋ค. ์ด๊ฑด ์ ๋ฒ ๋ฆฌ๋ทฐํ๋ฉด์ ์ผ๋ ๋ฐฉ์์ธ๋ฐ N * N ๋ฐฐ์ด์
0๋ถํฐN*N - 1๊น์ง์ ์๋ฅผ ์ฑ์ฐ๊ณ ๋ฐฐ์ด์ ๊ฐ์ N์ผ๋ก ๋๋๊ณ N์ผ๋ก ๋๋จธ์ง๋ฅผ ๊ตฌํ๋ฉด ํ๊ณผ ์ด์index๋ฅผ ๊ตฌํ ์ ์์ต๋๋ค.์ค์ํ๋ฉด ์๋๋๊ฑด 0๋ถํฐ ์์ํด์ผํฉ๋๋ค!!