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

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

/**
* SW Expert Academy - [S/W 문제해결 기본] 10일차 - Contact
* @author YeJun, Jung
*
* [분석]
* - BFS를 하는데, 마지막에 연락을 받는 노드들 중에서 가장 큰 숫자를 출력
*
* [전략]
* - BFS 사용
* - visited 배열을 사용해서 방문 처리(양방향 통신이 가능한 경우를 처리하기 위함)
* - 마지막에 연락을 받는 노드들 중에서 가장 큰 숫자를 출력
*/
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();
}
}

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

static final int MAX_NODE_LEN = 101;

int testCase;
int answer;
int nodeLen;
int startNode;
List<List<Integer>> graph;
boolean[] visited;

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());
startNode = Integer.parseInt(input.nextToken());

int fromNode, toNode;
graph = new ArrayList<>(MAX_NODE_LEN);
for (int cnt = 0; cnt < MAX_NODE_LEN; cnt++) {
graph.add(new ArrayList<>());
}

getLine();
while (input.hasMoreTokens()) {
fromNode = Integer.parseInt(input.nextToken());
toNode = Integer.parseInt(input.nextToken());

graph.get(fromNode).add(toNode);
}
}

private void solve() {
visited = new boolean[MAX_NODE_LEN];
Deque<Context> q = new ArrayDeque<>();
answer = startNode;
int time = 0;

q.offer(new Context(startNode, time));
visited[startNode] = true;

while (!q.isEmpty()) {
Context peek = q.poll();

if (time == peek.time) {
answer = Math.max(answer, peek.node);
} else if (time < peek.time) {
answer = peek.node;
time = peek.time;
}

for (int node : graph.get(peek.node)) {
if (visited[node]) continue;
visited[node] = true;

q.offer(new Context(node, peek.time + 1));
}
}
}

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

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

private static class Context {
int node;
int time;

public Context(int node, int time) {
this.node = node;
this.time = time;
}
}

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

private static void getLine() throws IOException {
input = new StringTokenizer(reader.readLine().trim());
}
}
144 changes: 144 additions & 0 deletions problems/SWEA/p7733/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* (7733) 치즈 도둑
* https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWrDOdQqRCUDFARG&categoryId=AWrDOdQqRCUDFARG&categoryType=CODE&problemTitle=7733&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1
*/

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

/**
* SW Expert Academy - 치즈 도둑
* @author YeJun, Jung
*
* [분석]
* - 0...100일이 지나는 동안 매일 오늘 날짜에 해당하는 치즈가 삭제된다.
* - 0일 최초의 치즈는 하나의 덩어리이다.
* - 남아있는 치즈들이 상하좌우로 이웃해 있으면 하나의 그룹으로 본다.
*
* [전략]
* - 매일 해당 날짜의 치즈를 0으로 설정해 삭제 처리한다.
* - 삭제한 치즈의 개수를 카운트해서 프로그램의 조기 종료를 가능하게 한다.
* - 각 행렬 요소에 대해서 DFS를 하여 그룹의 개수를 센다.
* - DFS할때 상하좌우로 이웃한 요소로 가지를 펼친다.
*/
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 = Integer.parseInt(reader.readLine().trim());

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

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

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

int testCase;
int answer;
int boardSize;
int[][] board;
boolean[][] visited;

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

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

private void input() throws IOException {
boardSize = Integer.parseInt(reader.readLine().trim());
board = new int[boardSize][boardSize];

for (int y = 0; y < boardSize; y++) {
getLine();

for (int x = 0; x < boardSize; x++) {
board[y][x] = Integer.parseInt(input.nextToken());
}
}
}

private void solve() {
answer = 1; // 최초에 치즈는 하나의 덩어리이다.
int restBlocks = boardSize * boardSize;

for (int day = 1; day <= 100 && restBlocks > 0; day++) {
restBlocks -= eating(day);
answer = Math.max(answer, countGroup());
}
}

private int eating(int num) {
int eatCount = 0;

for (int y = 0; y < boardSize; y++) {
for (int x = 0; x < boardSize; x++) {
if (board[y][x] != num) continue;

board[y][x] = 0;
eatCount++;
}
}

return eatCount;
}

private int countGroup() {
visited = new boolean[boardSize][boardSize];
int result = 0;

for (int y = 0; y < boardSize; y++) {
for (int x = 0; x < boardSize; x++) {
if (dfs(x, y)) result++;
}
}

return result;
}

private boolean dfs(int x, int y) {
if (board[y][x] == 0 || visited[y][x]) return false;
visited[y][x] = 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)) continue;

dfs(nx, ny);
}

return true;
}

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

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

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

private static void getLine() throws IOException {
input = new StringTokenizer(reader.readLine().trim());
}
}
107 changes: 107 additions & 0 deletions problems/baekjoon/p1987/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* (1987) 알파벳
* https://www.acmicpc.net/problem/1987
*/

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

/**
* Baekjoon - 알파벳
* @author YeJun, Jung
*
* [분석]
* - 보드를 상하좌우로 이동하면서 방문한다.
* - 방문한 곳에 있는 알파벳을 기억해 두었다가, 해당 알파벳이 적힌 다른 타일을 방문할 수 없다.
* - 초기위치(0, 0)에 있는 알파벳에도 동일하게 적용된다.
*
* [전략]
* - DFS하면서 상하좌우 탐색한다.
* - visitied 배열에 알파벳을 키로 하여 방문여부를 저장한다.
* - 위치가 아닌 값을 이용해서 방문 처리하므로 DFS시 자식 노드 탐색 후 방문을 해제해 주어야 한다.
*/
public class Main {
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 {
new Main().run();
}

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

static final int[] DIR_X = { -1, 1, 0, 0 };
static final int[] DIR_Y = { 0, 0, -1, 1 };
static final int DIR_LEN = 4;
static final int ALPHABET_LEN = 'Z' - 'A' + 1;

int answer;
int boardWidth;
int boardHeight;
char[][] board;
boolean[] visited;

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

private void input() throws IOException {
getLine();
boardHeight = Integer.parseInt(input.nextToken());
boardWidth = Integer.parseInt(input.nextToken());

board = new char[boardHeight][];

for (int y = 0; y < boardHeight; y++) {
board[y] = reader.readLine().trim().toCharArray();
}
}

private void solve() {
visited = new boolean[ALPHABET_LEN];

visited[board[0][0] - 'A'] = true;
answer = dfs(0, 0);
}

private int dfs(int x, int y) {
int result = 0;

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

if (!isInsideBoard(nx, ny)) continue;

char ch = board[ny][nx];
int key = ch - 'A';
if (visited[key]) continue;

visited[key] = true;
result = Math.max(result, dfs(nx, ny));
visited[key] = false;
}

return 1 + result;
}

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

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

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

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