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
134 changes: 134 additions & 0 deletions problems/SWEA/p1227/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* (1227) [S/W 문제해결 기본] 7일차 - 미로2
* https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14wL9KAGkCFAYD&categoryId=AV14wL9KAGkCFAYD&categoryType=CODE&problemTitle=1227&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1
*/

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

/**
* SW Expert Academy - [S/W 문제해결 기본] 7일차 - 미로2
* @author YeJun, Jung
*
* [분석]
* - 100x100 미로와, 출발지, 도착지가 주어졌을때 탈출가능 여부를 파악해야 한다.
* - 미로에는 벽, 길 두가지가 있으며 길로만 다닐 수 있다.
*
* [전략]
* - 미로의 출발지에서 DFS를 시작한다.
* - 상하좌우로 이동하면서 목적지에 도달하거나, 모든 장소를 방문할때가지 반복한다.
* - 목적지를 찾았다면 '1' 아니면 '0'을 화면에 출력한다.
*/
public class Solution {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

// ----------------------------------------------------------

public static void main(String[] args) throws IOException {
final int testCount = 10;

for (int testCase = 1; testCase <= testCount; testCase++) {
reader.readLine(); // 테스트케이스 입력

new Solution(testCase).run();
}
}

// ----------------------------------------------------------

static final int BOARD_SIZE = 100;
static final char ROAD = '0';
static final char WALL = '1';
static final char START = '2';
static final char GOAL = '3';

static final int DIR_LEN = 4;
static final int[] DIR_X = { 0, 0, -1, 1 };
static final int[] DIR_Y = { -1, 1, 0, 0 };

static char[][] board = new char[BOARD_SIZE][BOARD_SIZE];

int testCase;
int answer;
Pos start;
boolean[][] visited;

public Solution(int testCase) {
this.testCase = testCase;
}

public void run() throws IOException {
input();
solve();
print();
}

private void input() throws IOException {
char[] input;
for (int y = 0; y < BOARD_SIZE; y++) {
input = reader.readLine().trim().toCharArray();

for (int x = 0; x < BOARD_SIZE; x++) {
board[y][x] = input[x];

if (board[y][x] == START) {
start = new Pos(x, y);
}
}
}
}

private void solve() {
visited = new boolean[BOARD_SIZE][BOARD_SIZE];
visited[start.y][start.x] = true;

answer = dfs(start.x, start.y) ? 1 : 0;
}

private boolean dfs(int x, int y) {
if (board[y][x] == GOAL) return true;

for (int dir = 0; dir < DIR_LEN; dir++) {
int nx = x + DIR_X[dir];
int ny = y + DIR_Y[dir];

if (!isInsideBoard(nx, ny) ||
board[ny][nx] == WALL ||
visited[ny][nx]
) {
continue;
}

visited[ny][nx] = true;
if (dfs(nx, ny)) return true;
}

return false;
}

private boolean isInsideBoard(int x, int y) {
return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE;
}

private void print() throws IOException {
writer.write("#" + testCase);
writer.write(" " + answer);
writer.write("\n");
writer.flush();
}

// ----------------------------------------------------------

private static class Pos {
int x;
int y;

public Pos(int x, int y) {
this.x = x;
this.y = y;
}
}

// ----------------------------------------------------------
}
118 changes: 118 additions & 0 deletions problems/SWEA/p1267/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* (1267) [S/W 문제해결 응용] 10일차 - 작업순서
* https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV18TrIqIwUCFAZN&categoryId=AV18TrIqIwUCFAZN&categoryType=CODE&problemTitle=1267&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1
*/

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

/**
* SW Expert Academy - 1267. [S/W 문제해결 응용] 10일차 - 작업순서
* @author YeJun, Jung
*
* [분석]
* - 각 작업에는 선행작업이 있을 수 있다.
* - 간선이 없는 노드가 있을 수 있다.(즉 선행작업이 없는 노드가 존재할 수 있음)
*
* [전략]
* - 위상정렬을 사용해서 선행작업이 먼저 수행되도록 순서를 만든다.
* - 선행작업이 없는 노드의 작업 순서는 중요하지 않다.
*/
public class Solution {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;

// ----------------------------------------------------------

public static void main(String[] args) throws IOException {
final int testCount = 10;

for (int testCase = 1; testCase <= testCount; testCase++) {
new Solution(testCase).run();
}
}

// ----------------------------------------------------------

int testCase;
int[] workOrder;

int nodeLen;
int edgeLen;
List<List<Integer>> graph;
int[] inorder;

public Solution(int testCase) {
this.testCase = testCase;
}

public void run() throws IOException {
input();
solve();
print();
}

private void input() throws IOException {
getLine();
nodeLen = Integer.parseInt(input.nextToken());
edgeLen = Integer.parseInt(input.nextToken());

graph = new ArrayList<>(nodeLen + 1);
for (int node = 0; node <= nodeLen; node++) {
graph.add(new ArrayList<>());
}

int nodeA, nodeB;
inorder = new int[nodeLen + 1];
getLine();
while (input.hasMoreTokens()) {
nodeA = Integer.parseInt(input.nextToken());
nodeB = Integer.parseInt(input.nextToken());

graph.get(nodeA).add(nodeB);
inorder[nodeB]++;
}
}

private void solve() {
workOrder = new int[nodeLen];

topologicalSort();
}

private void topologicalSort() {
int size = 0;
Queue<Integer> q = new ArrayDeque<>();

for (int node = 1; node <= nodeLen; node++) {
if (inorder[node] == 0) q.offer(node);
}

while (!q.isEmpty()) {
int peek = q.poll();
workOrder[size++] = peek;

for (int child : graph.get(peek)) {
if (--inorder[child] == 0) q.offer(child);
}
}
}

private void print() throws IOException {
writer.write("#" + testCase + " ");

for (int node : workOrder) {
writer.write(node + " ");
}

writer.write("\n");
writer.flush();
}

// ----------------------------------------------------------

private static void getLine() throws IOException {
input = new StringTokenizer(reader.readLine().trim());
}
}
Loading