Skip to content
Open
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
115 changes: 115 additions & 0 deletions 정재우/week8/Problem_14502_Gold4_BFS_And_DFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package week8;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Problem_14502_Gold4_BFS_And_DFS {
static int[][] map;
static boolean[][] visited;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int H, W;
static int result = 0;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st1 = new StringTokenizer(br.readLine());
H = Integer.parseInt(st1.nextToken());
W = Integer.parseInt(st1.nextToken());

map = new int[H][W];
visited = new boolean[H][W];

for (int i = 0; i < H; i++) {
StringTokenizer st2 = new StringTokenizer(br.readLine());
for (int j = 0; j < W; j++) {
map[i][j] = Integer.parseInt(st2.nextToken());
}
}

wallDfs(0);

System.out.println(result);
}

// 아무데나 벽 세워보기
private static void wallDfs(int count) {
// 벽이 3개 되면 바이러스 퍼뜨리고, 퍼진 지도를 기반으로 안전지역 구해서 최댓값과 비겨하기
if (count == 3) {
int safeAreaCount = proceedVirusAndCountSafeArea();
if (safeAreaCount > result) { // 안전 구역 최댓값 갱신
result = safeAreaCount;
}
return;
}

for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (map[i][j] == 0) {
map[i][j] = 1;
wallDfs(count + 1);
map[i][j] = 0;
}
}
}
}

private static int countSafeArea(int[][] map) {
int safeAreaCount = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (map[i][j] == 0) {
safeAreaCount++;
}
}
}

return safeAreaCount;
}

private static int proceedVirusAndCountSafeArea() {
// 원본 훼손을 방지하기 위해 복사된 지도
int[][] copiedMap = new int[H][W];
for (int i = 0; i < H; i++) {
copiedMap[i] = map[i].clone();
}
visited = new boolean[H][W];

// 곧곧에 흩어져 있는 바이러스들을 조사해서 큐에 넣는 작업
Queue<int[]> queue = new LinkedList<>();
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (copiedMap[i][j] == 2) {
queue.offer(new int[]{i, j});
visited[i][j] = true;
}
}
}

while(!queue.isEmpty()) {
int[] poll = queue.poll();
int curY = poll[0];
int curX = poll[1];
for (int i = 0; i < 4; i++) {
int nextY = curY + dy[i];
int nextX = curX + dx[i];

if (nextX >= 0 && nextY >= 0 && nextX < W && nextY < H && !visited[nextY][nextX]) {
// 빈 칸으로 바이러스가 침투
if (copiedMap[nextY][nextX] == 0) {
copiedMap[nextY][nextX] = 2;
visited[nextY][nextX] = true;
queue.offer(new int[]{nextY, nextX});
}
}
}
}

return countSafeArea(copiedMap);
}
}
76 changes: 76 additions & 0 deletions 정재우/week9/Problem_13549_Gold5_BFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package week9;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Problem_13549_Gold5_BFS {

static int[] map = new int[100001];
static boolean[] visited = new boolean[100001];
static int start, end;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st = new StringTokenizer(br.readLine());
start = Integer.parseInt(st.nextToken());
end = Integer.parseInt(st.nextToken());

// logic
bfs(start);
if (start == end) {
System.out.println(0);
} else {
System.out.println(map[end]);
}
}

private static void bfs(int start) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(start);

while (!queue.isEmpty()) {
Integer cur = queue.poll();

Move[] moves = new Move[3];
moves[0] = new Move(cur * 2, 0);
moves[1] = new Move(cur - 1, 1);
moves[2] = new Move(cur + 1, 1);

for (int i = 0; i < moves.length; i++) {
Move move = moves[i];
int next = move.next;

if (next >= 0 && next <= 100000) {
// 내가 방문한 곳 중에 최소 이동거리인 곳은 +1을 하지 않고 스킵한다.
// 순간이동일 경우 이동해도 시간이 늘지 않기 때문에, 현재 위치보다 작거나 같으면 최소 이동거리인 것으로 판단한다. (move.time)
if (map[next] <= map[cur] + move.time && visited[next]) {
continue;
}

map[next] = map[cur] + move.time;
visited[next] = true;
if (next == end) {
break;
}
queue.offer(next);
}
}
}
}

static class Move {

int next;
int time;

public Move(int next, int time) {
this.next = next;
this.time = time;
}
}
}
67 changes: 67 additions & 0 deletions 정재우/week9/Problem_7562_Silver1_BFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package week9;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Problem_7562_Silver1_BFS {
static int[] dx = {-1, -1, 1, 1, -2, -2, 2, 2};
static int[] dy = {-2, 2, -2, 2, 1, -1, 1, -1};
static int[][] map;
static boolean[][] visited;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
int l = Integer.parseInt(br.readLine());
map = new int[l][l];
visited = new boolean[l][l];

// knight
StringTokenizer st1 = new StringTokenizer(br.readLine());
int startI = Integer.parseInt(st1.nextToken());
int startJ = Integer.parseInt(st1.nextToken());

StringTokenizer st2 = new StringTokenizer(br.readLine());
int endI = Integer.parseInt(st2.nextToken());
int endJ = Integer.parseInt(st2.nextToken());

// bfs
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{startI, startJ});
visited[startI][startJ] = true;

while(!queue.isEmpty()) {
int[] poll = queue.poll();
int curI = poll[0];
int curJ = poll[1];

for (int k = 0; k < 8; k++) {
int nextI = poll[0] + dy[k];
int nextJ = poll[1] + dx[k];

if (nextI >= 0 && nextJ >= 0 && nextI < l && nextJ < l) {
if (!visited[nextI][nextJ]) {
// 최소 이동 거리를 갱신하거나, 방문한 적이 없을 경우 +1을 한다.
if (map[nextI][nextJ] <= map[curI][curJ] + 1 && map[nextI][nextJ] != 0) {
continue;
}
map[nextI][nextJ] = map[curI][curJ] + 1;
if (nextI == endI && nextJ == endJ) {
break;
}
queue.offer(new int[]{nextI, nextJ});
}
}
}
}

System.out.println(map[endI][endJ]);
}
}
}