Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[13주차] 배수빈 #182

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

Merged
KodaHye merged 8 commits into main from xubin
Dec 7, 2024
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
100 changes: 100 additions & 0 deletions BOJ/1000-5000번/SB_1194.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class SB_1194 {
static int N, M;
static char[][] board;
static Node start;
static int ans = Integer.MAX_VALUE;
static int[][][] visited;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};

private static void bfs() {
Queue<Node> que = new ArrayDeque<>();
visited[start.x][start.y][0] = 0;
que.offer(new Node(start.x, start.y, 0));

while (!que.isEmpty()) {
Node cur = que.poll();

if (board[cur.x][cur.y] == '1') {
ans = Math.min(ans, visited[cur.x][cur.y][cur.keys]);
continue;
}

for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
int nk = cur.keys;
if (!isValid(nx, ny) || board[nx][ny]=='#' || visited[nx][ny][nk]!=-1) continue;

char nxt = board[nx][ny];
// 이동할 곳이 알파벳일 경우
if (Character.isAlphabetic(nxt)){
if (Character.isLowerCase(nxt)) nk |= (1 << (nxt - 'a')); // 키면 키 줍기
else if (Character.isUpperCase(nxt)) {
if ((nk & (1<< (nxt-'A')))==0) continue; // 키 없는 문은 못감
}
}

// 이동 가능할 경우 이동
que.offer(new Node(nx, ny, nk));
visited[nx][ny][nk] = visited[cur.x][cur.y][cur.keys] + 1;
}
}
}

private static boolean isValid(int x, int y) {
return 0 <= x && x < N && 0 <= y && y < M;
}
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 char[N][M];
visited = new int[N][M][1 << 6]; // 키 6개 존재 2^6
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
board[i][j] = line.charAt(j);
if (board[i][j]=='0'){
start = new Node(i, j, 0);
board[i][j] = '.';
}
}
}

for (int i = 0; i < N; i++) { // 방문배열 초기화
for (int j = 0; j < M; j++) {
Arrays.fill(visited[i][j], -1);
}
}

bfs();
System.out.println(ans==Integer.MAX_VALUE ? -1 : ans);
}
static class Node{
int x, y, keys;

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

@Override
public String toString() {
return "Node{" +
"x=" + x +
", y=" + y +
", keys=" + keys +
'}';
}
}
}
45 changes: 45 additions & 0 deletions BOJ/1000-5000번/SB_1749.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

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

int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());

int[][] board = new int[N][M];
long[][] dp = new long[N + 1][M + 1];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}

// 누적합 계산
for (int i = 1; i < N + 1; i++) {
for (int j = 1; j < M + 1; j++) {
dp[i][j] = board[i-1][j-1] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
}
}

// 부분 행렬의 최대값 찾기
long mx = Long.MIN_VALUE;
for (int r1 = 1; r1 < N + 1; r1++) {
for (int c1 = 1; c1 <= M + 1; c1++) {
for (int r2 = r1; r2 <= N; r2++) {
for (int c2 = c1; c2 <= M; c2++) {
long tmp = dp[r2][c2] - dp[r1 - 1][c2] - dp[r2][c1 - 1] + dp[r1 - 1][c1 - 1];
mx = Math.max(mx, tmp);
}
}
}
}

System.out.println(mx);
}
}
65 changes: 65 additions & 0 deletions BOJ/5001-10000번/SB_8979.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class SB_8979 {
static int N, K;
static List<Nation> nations = new ArrayList<>();
private static int medal2Int(int g, int s, int b){
return g * 1000000 + s * 1000 + b;
}
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());
K = Integer.parseInt(st.nextToken());

for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int idx = Integer.parseInt(st.nextToken());
int g = Integer.parseInt(st.nextToken());
int s = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
nations.add(new Nation(idx, medal2Int(g, s, b)));
}

Collections.sort(nations);

int rank = 1;
int tmp = 1; // 동점자 처리를 위한 변수(매번 증가)
int preScore = nations.get(0).score;
for (Nation cur : nations) {
if (cur.score != preScore) rank = tmp; // 서로 점수가 다르면 순위 갱신
if (cur.idx==K){ // 찾는 나라면 순위 출력
System.out.println(rank);
return;
}
tmp++;
preScore = cur.score;
}
}

static class Nation implements Comparable<Nation>{
int idx, score;

public Nation(int idx, int score) {
this.idx = idx;
this.score = score;
}

@Override
public int compareTo(Nation o) {
return o.score - this.score;
}

@Override
public String toString() {
return "Nation{" +
"idx=" + idx +
", score=" + score +
'}';
}
}
}
142 changes: 142 additions & 0 deletions CodeTree/2019-2020년/SB_술래잡기_체스.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class SB_술래잡기_체스 {
static int mx = -1;
static int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1};
static int[] dy = {0, -1, -1, -1, 0, 1, 1, 1};

private static int[][] copyBoard(int[][] board) {
int[][] tmp = new int[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
tmp[i][j] = board[i][j];
}
}
return tmp;
}

private static List<Piece> copyPiece(List<Piece> pieces) {
List<Piece> tmpList = new ArrayList<>();
for (Piece p : pieces) {
tmpList.add(new Piece(p.idx, p.x, p.y, p.dir, p.isDie));
}
return tmpList;
}
private static void dfs(Tagger it, int[][] board, List<Piece> pieces) {
// 도둑말들 움직이기
moveThief(board, pieces);

// 현재 방향으로 잡을 수 있는 말 잡기(최대 3번 이동 가능)
for (int i = 1; i < 4; i++) {
int nx = it.x + dx[it.dir] * i;
int ny = it.y + dy[it.dir] * i;

if (isValid(nx, ny) && board[nx][ny] != 0) {
int[][] tmpBoard = copyBoard(board);
List<Piece> tmpPiece = copyPiece(pieces);

tmpBoard[it.x][it.y] = 0;
Piece thief = tmpPiece.get(board[nx][ny]);
Tagger nit = new Tagger(thief.x, thief.y, thief.dir, it.cnt + thief.idx);
thief.isDie = true;
tmpBoard[thief.x][thief.y] = -1;

dfs(nit, tmpBoard, tmpPiece);
}
}
mx = Math.max(mx, it.cnt);
}


private static void moveThief(int[][] board, List<Piece> pieces) {
for (Piece cur : pieces) {
if (cur.idx == 0 || cur.isDie) continue; // 죽었거나 술래면 스킵
for (int d = 0; d < 8; d++) {
int nd = (cur.dir + d) % 8;
int nx = cur.x + dx[nd];
int ny = cur.y + dy[nd];
if (!isValid(nx, ny) || board[nx][ny] == -1) continue;
board[cur.x][cur.y] = 0;
if (board[nx][ny] == 0) cur.setPos(nx, ny); // 빈칸이면 바로 이동
else { // 다른말있으면 교환
Piece target = pieces.get(board[nx][ny]);
target.setPos(cur.x, cur.y);
board[cur.x][cur.y] = target.idx;
cur.setPos(nx, ny);
}
board[nx][ny] = cur.idx; // 움직일 보드에 현재 말 놓기
cur.dir = nd; // 현재 말의 방향 갱신
break;
}
}
}

private static boolean isValid(int x, int y) {
return 0 <= x && x < 4 && 0 <= y && y < 4;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

int[][] board = new int[4][4]; // 말들의 번호로 저장
List<Piece> pieces = new ArrayList<>();

for (int i = 0; i < 4; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < 4; j++) {
int num = Integer.parseInt(st.nextToken());
int dir = Integer.parseInt(st.nextToken()) - 1;
Piece piece = new Piece(num, i, j, dir, false);
pieces.add(piece);
board[i][j] = num;
}
}

// 도둑말 인덱스 순으로 정렬
pieces.add(new Piece(0, -1, -1, 0, false)); // 인덱스 0맞추기 위해 더미 객체 넣기
pieces.sort(((o1, o2) -> o1.idx - o2.idx));

// 초기 0,0 도둑말 잡기
Piece first = pieces.get(board[0][0]);
Tagger tagger = new Tagger(0, 0, first.dir, first.idx);
board[0][0] = -1;
first.isDie = true;

// 술래말의 재귀 시작
dfs(tagger, board, pieces);

System.out.println(mx);
}

static class Tagger{
int x, y, dir, cnt;

public Tagger(int x, int y, int dir, int cnt) {
this.x = x;
this.y = y;
this.dir = dir;
this.cnt = cnt;
}
}

static class Piece{
int idx, x, y, dir;
boolean isDie;

public Piece(int idx, int x, int y, int dir, boolean isDie) {
this.idx = idx;
this.x = x;
this.y = y;
this.dir = dir;
this.isDie = isDie;
}

private void setPos(int x, int y) {
this.x = x;
this.y = y;
}
}
}
Loading

AltStyle によって変換されたページ (->オリジナル) /