Skip to content
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

[모디] - 3, 4주차 제출 #42

Open
wants to merge 7 commits into
base: jaehee329
Choose a base branch
from
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
51 changes: 51 additions & 0 deletions 1주차/모디/BOJ_1051.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import java.io.*;
import java.util.*;

public class BOJ_1051 {

private static int N;
private static int M;
private static int[][] board;

public static void main(String[] args) 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());

board = new int[N][M];
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
int num = line.charAt(j) + '0';
board[i][j] = num;
}
}

int maxSquareLength = 0;
for (int i = 0; i < N - maxSquareLength - 1; i++) {
for (int j = 0; j < M - maxSquareLength - 1; j++) {
int length = maxSquareLength + 1;
while (i + length < N && j + length < M) {
if (hasSameNumberedSquare(i, j, length)) {
maxSquareLength = length;
}
length++;
}
}
}
System.out.println((maxSquareLength + 1) * (maxSquareLength + 1));
}

private static boolean hasSameNumberedSquare(int topLeftY, int topLeftX, int length) {
if (topLeftY + length >= N || topLeftX + length >= M) {
return false;
}
int topLeft = board[topLeftY][topLeftX];
int topRight = board[topLeftY][topLeftX + length];
int bottomLeft = board[topLeftY + length][topLeftX];
int bottomRight = board[topLeftY + length][topLeftX + length];
return topLeft == topRight && topLeft == bottomLeft && topLeft == bottomRight;
}
}
30 changes: 30 additions & 0 deletions 1주차/모디/BOJ_1874.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.io.*;
import java.util.*;

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

int N = Integer.parseInt(br.readLine());
Deque<Integer> stack = new ArrayDeque<>(N);
int start = 0;
while(N-- > 0) {
int target = Integer.parseInt(br.readLine());
if (target > start) {
for(int i = start + 1; i <= target; i++) {
stack.push(i);
sb.append("+\n");
}
start = target;
} else if (stack.peek() != target) {
System.out.println("NO");
return;
}
stack.pop();
sb.append("-\n");
}
sb.setLength(sb.length() - 1);
System.out.println(sb);
}
}
114 changes: 114 additions & 0 deletions 2주차/모디/BOJ_13901.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import java.io.*;
import java.util.*;

public class BOJ_13901 {

private static final char OBSTACLE = 'x';
private static final char UNVISITED = '\u0000';
private static final char VISITED = 'v';
private static final int DIRECTION_COUNT = 4;

private static int height;
private static int width;
private static char[][] map;

private static class Point {

public int y;
public int x;

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

public boolean isMovableTo(int targetY, int targetX) {
return targetY >= 0 && targetY < height && targetX >= 0 && targetX < width
&& map[targetY][targetX] == UNVISITED;
}

@Override
public String toString() {
return y + " " + x;
}
}

private enum Direction {

UP(1, new Point(-1, 0)),
DOWN(2, new Point(1, 0)),
LEFT(3, new Point(0, -1)),
RIGHT(4, new Point(0, 1));

private final int input;
private final Point point;

Direction(int input, Point point) {
this.input = input;
this.point = point;
}

public static Direction getDirectionFromInput(int input) {
for (Direction direction : Direction.values()) {
if (direction.input == input) {
return direction;
}
}
throw new IllegalArgumentException();
}

public int getY() {
return point.y;
}

public int getX() {
return point.x;
}
}

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

st = new StringTokenizer(br.readLine());
height = Integer.parseInt(st.nextToken());
width = Integer.parseInt(st.nextToken());

map = new char[height][width];
int obstacleCount = Integer.parseInt(br.readLine());
for (int i = 0; i < obstacleCount; i++) {
st = new StringTokenizer(br.readLine());
int y = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
map[y][x] = OBSTACLE;
}

st = new StringTokenizer(br.readLine());
int y = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
map[y][x] = VISITED;

Queue<Direction> directions = new LinkedList<>();
st = new StringTokenizer(br.readLine());
for (int i = 0; i < DIRECTION_COUNT; i++) {
int input = Integer.parseInt(st.nextToken());
Direction inputDirection = Direction.getDirectionFromInput(input);
directions.add(inputDirection);
}

Point point = new Point(y, x);
int distance = 1;
for (int i = 0; i < DIRECTION_COUNT; i++) {
Direction direction = directions.poll();
while (point.isMovableTo(point.y + distance * direction.getY(), point.x + distance * direction.getX())) {
int targetY = point.y + distance * direction.getY();
int targetX = point.x + distance * direction.getX();
map[targetY][targetX] = VISITED;
point = new Point(targetY, targetX);
i = 0;
}
directions.add(direction);
}
System.out.println(point);
}
}
44 changes: 44 additions & 0 deletions 2주차/모디/BOJ_17298.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.io.*;
import java.util.*;

public class BOJ_17298 {

private static class NumberWithIdx {
public int number;
public int idx;
public NumberWithIdx(int number, int idx) {
this.number = number;
this.idx = idx;
}
}

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

int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
ArrayDeque<NumberWithIdx> stack = new ArrayDeque<>();

int[] NGE = new int[N];
for (int i = 0; i < N; i++) {
int number = Integer.parseInt(st.nextToken());
while (!stack.isEmpty() && stack.peekLast().number < number) {
NumberWithIdx previous = stack.pollLast();
NGE[previous.idx] = number;
}
stack.add(new NumberWithIdx(number, i));
}

while (!stack.isEmpty()) {
NumberWithIdx previous = stack.pollLast();
NGE[previous.idx] = -1;
}

for (int i = 0; i < N; i++) {
bw.write(NGE[i] + " ");
}
bw.flush();
bw.close();
}
}
41 changes: 41 additions & 0 deletions 3주차/모디/BOJ_11286.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.io.*;
import java.util.*;

public class BOJ_11286 {

private static class NumberWithAbs {
public int number;
public int abs;
public NumberWithAbs(int number, int abs) {
this.number = number;
this.abs = abs;
}
}

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

int count = Integer.parseInt(br.readLine());
PriorityQueue<NumberWithAbs> priorityQueue = new PriorityQueue<>(
Comparator.comparingInt((NumberWithAbs n) -> n.abs)
.thenComparingInt(n -> n.number));

for (int i = 0; i < count; i++) {
int number = Integer.parseInt(br.readLine());
if (number == 0) {
if (priorityQueue.size() == 0) {
bw.write("0\n");
} else {
NumberWithAbs target = priorityQueue.poll();
bw.write(target.number + "\n");
}
} else {
priorityQueue.add(new NumberWithAbs(number, Math.abs(number)));
}
}

bw.flush();
bw.close();
}
}
46 changes: 46 additions & 0 deletions 3주차/모디/BOJ_2799.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.io.*;
import java.util.*;

public class BOJ_2799 {

private static final int WINDOW_SIZE = 4;
private static final int BLIND_TYPE_COUNT = 5;
private static final char BLINDED = '*';

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int windowHeight = Integer.parseInt(st.nextToken());
int windowWidth = Integer.parseInt(st.nextToken());

int totalHeight = windowHeight * (WINDOW_SIZE + 1) + 1;
int totalWidth = windowWidth * (WINDOW_SIZE + 1) + 1;

char[][] state = new char[totalHeight][totalWidth];
for (int i = 0; i < totalHeight; i++) {
String line = br.readLine();
for (int j = 0; j < totalWidth; j++) {
state[i][j] = line.charAt(j);
}
}

int[] blindType = new int[BLIND_TYPE_COUNT];
for (int i = 0; i < windowHeight; i++) {
for (int j = 0; j < windowWidth; j++) {
int currentY = i * (WINDOW_SIZE + 1) + 1;
int currentX = j * (WINDOW_SIZE + 1) + 1;

int blindLength = 0;
while (state[currentY][currentX] == BLINDED) {
currentY++;
blindLength++;
}
blindType[blindLength]++;
}
}

for (int i = 0; i < BLIND_TYPE_COUNT; i++) {
System.out.print(blindType[i] + " ");
}
}
}
34 changes: 34 additions & 0 deletions 4주차/모디/BOJ_1377.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.io.*;
import java.util.*;

public class BOJ_1377 {

private static class ValueAndIdx {
public int value;
public int index;
public ValueAndIdx(int value, int index) {
this.value = value;
this.index = index;
}
}

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

List<ValueAndIdx> values = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
int value = Integer.parseInt(br.readLine());
values.add(new ValueAndIdx(value, i));
}

Collections.sort(values, Comparator.comparing(v -> v.value));

int maxForwardedDistance = 0;
for (int i = 0; i < n; i++) {
maxForwardedDistance = Math.max(values.get(i).index - i, maxForwardedDistance);
}

System.out.println(maxForwardedDistance + 1);
}
}
Loading