-
Notifications
You must be signed in to change notification settings - Fork 0
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
18-kokeunho #73
18-kokeunho #73
Conversation
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 bfs๋ฌธ์ ๋ ํญ์ ์ฌ๋ฐ๋๊ฒ ๊ฐ๋ค์. ์ข์ ๋ฌธ์ ๊ฐ์ฌํฉ๋๋ค.
CPP CODE
#include <iostream>
#include <vector>
#define PATH 0
#define WALL 1
#define VISITED 2
using namespace std;
int N, M, answer = 0;
vector<vector<int>> board;
int offset[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
void dfs(int y, int x, int d) {
if (board[y][x] == PATH) {
board[y][x] = VISITED;
answer++;
}
for (int i = 0; i < 4; i++) {
int d_ = (d + 3) % 4;
int y_ = y + offset[d_][0];
int x_ = x + offset[d_][1];
if (0 <= y_ && y_ < N && 0 <= x_ && x_ < M && board[y_][x_] == PATH) {
dfs(y_, x_, d_);
return;
}
d = d_;
}
int backD = (d + 2) % 4;
int backY = y + offset[backD][0];
int backX = x + offset[backD][1];
if (board[backY][backX] != WALL) dfs(backY, backX, d);
}
int main() {
cin >> N >> M;
int curY, curX, d;
cin >> curY >> curX >> d;
board = vector<vector<int>>(N, vector<int>(M));
for (int y = 0; y < N; y++) for (int x = 0; x < M; x++)
cin >> board[y][x];
dfs(curY, curX, d);
cout << answer << endl;
return 0;
}
public static void turnLeft() { | ||
d = (d + 3) % 4; | ||
} |
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.
์๊ณ ๋ฆฌ์ฆ ์ด์ง๋ง ํจ์๋ถ๋ฆฌ๋ฅผ ์ํด์ ๋ณด๊ธฐ ํธํ ๊ฒ ๊ฐ์์ ์ข๋ค์!
d = sc.nextInt(); | ||
|
||
map = new int[n][m]; | ||
cleaned = new boolean[n][m]; |
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.
๊ทผํธ๋ ์ฝ๋์์ ๋ฐ๋ก ์ ์ฉ๊ฐ๋ฅํ์ง๋ ๋ชจ๋ฅด๊ฐฐ์ง๋ง,
์ด๋ ๊ฒ ๋ถ๋ฆฌ์ธ ๋ฐฐ์ด์ ๋ฐ๋ก ์์ฑํ์ง ์๊ณ , map ๋ฐฐ์ด ์์ ๊ฐ์ ์์ ํด์ ๋ฐฉ๋ฌธ ํ์๋ฅผ ํ ์ ๋ ์์ ๊ฒ ๊ฐ์์!
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.
์น ๋ค์ ๋น์ทํ ์ ํ ํ์ด๋ณผ ๋ ์ ์ฉ์์ผ๋ณด๊ฒ ์ต๋๋ค!
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.
์ ๋ ฅ์ด (r, c) ์ธ์ค ๋ชฐ๋ผ์ 1์๊ฐ์ ํค๋งท๋ค์.. ๋ฌธ์ ๋ฅผ ์ฝ๊ณ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์ด ์ข ํ์ด์ ธ์ ์ ์ ํ์๋ ๋ฌธ์ ์ฟ๋๋ฐ๋ ์ ๊ฐ ์ด์ ์ ์์ฑํ ์ฝ๋์ ๋ง์ด ๋ค๋ฅด๋ค์.
์ฝ๋ ์์ฒด๋ ๊ฐ๋๋ ์ฒ๋ผ dfs๋ฅผ ์ฌ์ฉํ์ต๋๋ค..(์ฌ์ค ์ค๊ฐ์ dfs๊ฐ ์ ์๋์ ์ฐธ๊ณ ๋ฅผ ์ข ํ์ต๋๋ค^^;) ์ฝ๋๊ฐ ๊ธธ์ด์ง๊ณ ๋ถํ์ํ ๋ฉ์๋ ๋ถ๋ฆฌ๊ฐ ๋ค๋ถํ๊ฑฐ ๊ฐ์ ๋ค์๋ถํด ๋ ์ ํ์ด๋ณด๋ ค ํฉ๋๋ค.
๊ณ ์ํ์ จ์ต๋๋ค.
code
import java.io.*;
import java.util.*;
public class Sol14503 {
static int answer = 0;
static int n;
static int m;
static Pos cur;
static int dir; // 0: ์, 1: ์ฐ, 2: ํ, 3: ์ข
static int[] dx = {0, 1, 0, -1};
static int[] dy = {-1, 0, 1, 0};
static int[][] grid;
public static void main(String[] args) throws IOException {
input();
dfs();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(String.valueOf(answer));
bw.close();
}
private static void dfs() {
if (grid[cur.y][cur.x] == 0) {
cur.clean();
answer++;
}
for (int t = 0; t < 4; t++) {
dir = (dir + 3) % 4;
if (cur.canMove(dir)) {
cur.move(dir);
dfs();
return;
}
}
if (cur.backward(dir)) {
dfs();
}
}
private static void input() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
cur = new Pos(c, r);
dir = Integer.parseInt(st.nextToken());
grid = new int[n][m];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
grid[i][j] = Integer.parseInt(st.nextToken());
}
}
br.close();
}
private static class Pos {
public int x;
public int y;
Pos(int x, int y) {
this.x = x;
this.y = y;
}
public boolean canMove(int dir) {
int nx = x + dx[dir];
int ny = y + dy[dir];
return 0 <= nx && nx < m && 0 <= ny && ny < n && grid[ny][nx] == 0;
}
public void move(int dir) {
x += dx[dir];
y += dy[dir];
}
public boolean backward(int dir) {
dir = (dir + 2) % 4;
int nx = x + dx[dir];
int ny = y + dy[dir];
if(0 <= nx && nx < m && 0 <= ny && ny < n && grid[ny][nx] != 1) {
move(dir);
return true;
}
return false;
}
public void clean() {
grid[y][x] = 2;
}
}
}
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.
๋ฌธ์ ๋ฅผ ์ฝ์ด๋ณด๋ ์ ์ ์ ๊ฐ PRํ๋ ๋ฌธ์ ์ ๊ฑฐ์ ์ ์ฌํ ๊ตฌํ๋ฌธ์ ์์ต๋๋ค.
ํน ๊ธฐ๋ฅ์ ์์๋๋ก ๋์ดํด์ค๊ฒ์ ๋ํ์ ์ธ ๊ตฌํ๋ฌธ์ ์์ต๋๋ค.
๋ก์ง์ ๋ฌธ์ ์์ ์๋ ค์ฃผ๊ฒ๋๋ฌธ์ ์ฝ๊ฒ ๊ตฌํํด๋๊ฐ๋ฉด ๋ฉ๋๋ค.
์ ๊ฐ ๋ฌธ์ ๋ฅผ ํ๋ฉด์ ๊ณ ๋ฏผํ๋ ๋ถ๋ถ๋ค์ ๋ฐ์๊ณ๋ฐฉํฅ ํ์ ์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ๊ฒ์ธ๊ฐ?
4๊ฐ์ง ๋ฐฉํฅ์ ๋๊ณ ๋ ๋ค ์ฒญ์ํ ๋ฐฉ์ด ์๋ค๋ ๊ฒ์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ๊ฒ์ธ๊ฐ? ์์ต๋๋ค.
Found๋ผ๋ ๋ถ ๋ณ์๋ฅผ ํ์ฉํด 4๊ฐ์ง๋ฐฉ์ ๋๊ณ ๋ ๋ค found๊ฐ false๋ผ๋ฉด ๋์ด์ ์ ์งํ ๊ณณ์ด ์๋ค๊ณ ํ๋จํ๊ณ ํ์ง๊ฐ๋ฅ์ฑ์ ํ๋จํ๋ 3๋ฒ์งธ ๋ก์ง์ผ๋ก ๊ฐ๊ฒ๋ ๊ตฌํํ์์ต๋๋ค.
45๋ถ์ ๋ ๊ฑธ๋ฆฐ๊ฒ๊ฐ์ต๋๋ค!
N, M = map(int, input().split())
r, c, d = map(int, input().split())
grid = [list(map(int, input().split())) for _ in range(N)]
def clean_room(n, m, r, c, d, grid):
# ๋ถ, ๋, ๋จ, ์ ๋ฐฉํฅ
dr = [-1, 0, 1, 0]
dc = [0, 1, 0, -1]
cleaned = 0
while True:
#ํ์ฌ ์นธ ์ฒญ์
if grid[r][c] == 0:
grid[r][c] = 2 #์ฒญ์ ์๋ฃ ํ์
cleaned += 1
#์ฃผ๋ณ 4์นธ ์ค ์ฒญ์๋์ง ์์ ์นธ ํ์ธ
found = False #์ฒญ์ํ ๋น ์นธ์ด ์๋์ง ์ฒดํฌ
for i in range(4):
nd = (d + 3) % 4 #๋ฐ์๊ณ ๋ฐฉํฅ ํ์
nr, nc = r + dr[nd], c + dc[nd]
if 0 <= nr < n and 0 <= nc < m and grid[nr][nc] == 0:
d = nd #ํ์
r, c = nr, nc #์ ์ง
found = True
break #์ ์งํ์ผ๋ฏ๋ก ์ฆ์ ๋ฃจํ ์ข
๋ฃ
else:
d = nd #๋ฐฉํฅ๋ง ํ์
#์ฒญ์ํ ์นธ์ด ์์ผ๋ฉด ํ์ง ์๋
if not found:
br, bc = r - dr[d], c - dc[d]
if 0 <= br < n and 0 <= bc < m and grid[br][bc] != 1:
r, c = br, bc #ํ์ง
else:
break #ํ์ง ๋ถ๊ฐ ์ ์ข
๋ฃ
return cleaned
print(clean_room(N, M, r, c, d, grid))
๐ ๋ฌธ์ ๋งํฌ
[BOJ] ๋ก๋ด ์ฒญ์๊ธฐ https://www.acmicpc.net/problem/14503
โ๏ธ ์์๋ ์๊ฐ
1h
โจ ์๋ ์ฝ๋
์ ๋ฒ์ ์ค์ฉ๋ pr์ด์๋๊ฐ์.
๊ฑฐ์ ๋์ผํ ๋ฌธ์ ์ธ๋ฐ ํ๋ฒ ๋ค์ ์๊ธฐํด๋ณด๊ณ ์ ํ์ด๋ดค์ต๋๋ค.
๋ฐฉํฅ ๊ด๋ จํด์๋ ๊ธ๋ฐฉ ๊ตฌํํ๋๋ฐ
์ ์ง, 4๋ฐฉํฅ ํ์, ํ์ง ๊ตฌํ์ ์กฐ๊ธ ํค๋งธ์ต๋๋ค.
์๋ง ๋ค๋ฅธ ๋ถ๋ค์ ์ฝ๊ฒ ํ ์ ์์ ๊ฒ๋๋ค.
PCCP ์ฐ์ต ๊ฒธ ์์ผ๋ก ๊ตฌํ ๋ฌธ์ ์์ฃผ๋ก ํ์ด๋ด์ผ๊ฒ ์ต๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ