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
100 changes: 100 additions & 0 deletions BOJ/1000-5000번/YJ_1194.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import java.io.*;
import java.util.*;

//bfs + 비트마스킹
//처음에 dfs 로 접근했는데, 그럼 모든 경로를 다 탐색 해 버리더라구요 최단거리 문제는 bfs 로 풀기!
public class YJ_1194 {
static class Pos {
int x;
int y;
int distance;
int hasKey;

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

static final char EXIT = '1';
static final char CURRENT = '0';
static final char WALL = '#';

static int N;
static int M;
static char[][] maze;

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());
maze = new char[N][M];
visited = new boolean[N][M][64]; //★키 6개 2^6 = 64

int x = 0;
int y = 0;
for(int i=0; i<N; i++) {
String data = br.readLine();
for(int j=0; j<M; j++) {
maze[i][j] = data.charAt(j);
if(maze[i][j] == CURRENT) {
x = i;
y = j;
}
}
}

System.out.println(escape(x,y));
}

static boolean[][][] visited;
static int escape(int x, int y){
Deque<Pos> pq = new ArrayDeque<>();
int[] dx = {0,1,0,-1};
int[] dy = {1,0,-1,0};

pq.offer(new Pos(x,y,0,0));
visited[x][y][0] = true;

while(!pq.isEmpty()) {
Pos pos = pq.poll();
if(maze[pos.x][pos.y] == EXIT) {
return pos.distance;
}

for(int d=0; d<4; d++){
int nx = pos.x + dx[d];
int ny = pos.y + dy[d];
if(stop(nx,ny, pos.hasKey)){
continue;
}

int key = pos.hasKey;
if(maze[nx][ny] >= 'A' && maze[nx][ny] <= 'F') {
if((key & 1<<(maze[nx][ny] - 'A')) > 0){ //가지고 있는 키와 일치하는 경우 (A=65)
visited[nx][ny][key] = true;
}else{
continue;
}
}else if(maze[nx][ny] >= 'a' && maze[nx][ny] <= 'f'){ //키를 주운 경우
key |= 1<<(maze[nx][ny] - 'a'); //a=97
visited[nx][ny][key] = true;
}else{
visited[nx][ny][key] = true; //'.'
}

pq.offer(new Pos(nx,ny,pos.distance+1,key));
}
}

return -1;
}

static private boolean stop(int x, int y, int hasKey){
return x < 0 || x >= N || y < 0 || y >= M || maze[x][y] == WALL || visited[x][y][hasKey];
}

}
37 changes: 37 additions & 0 deletions BOJ/1000-5000번/YJ_1749.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.io.*;
import java.util.*;

public class YJ_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())+1;
int M = Integer.parseInt(st.nextToken())+1;
int [][] game = new int[N][M];

for(int i = 1; i < N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 1; j < M; j++) {
game[i][j] = Integer.parseInt(st.nextToken());
}
}
//누적합
for(int i = 1; i < N; i++) {
for(int j = 1; j < M; j++) {
game[i][j] = game[i-1][j] + game[i][j-1] - game[i-1][j-1] + game[i][j];
}
}

int max = game[1][1];
for(int i = 1; i < N; i++) {
for(int j = 1; j < M; j++) {
for(int r = 0; r < i; r++) {
for(int k = 0; k < j; k++) {
max = Math.max(max, game[i][j] - game[r][j] - game[i][k] + game[r][k]);
}
}
}
}
System.out.println(max);
}
}
75 changes: 75 additions & 0 deletions BOJ/5001-10000번/YJ_8979.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.io.*;
import java.util.*;

public class YJ_8979 {
static class Nation implements Comparable<Nation> {
int name;
int gold;
int silver;
int bronze;

public Nation(int name, int gold, int silver, int bronze) {
this.name = name;
this.gold = gold;
this.silver = silver;
this.bronze = bronze;
}

@Override
public int compareTo(Nation o) {
if(this.gold == o.gold){
if(this.silver == o.silver){
return o.bronze - this.bronze;
}
return o.silver - this.silver;
}
return o.gold - this.gold;
}
}

static PriorityQueue<Nation> pq = new PriorityQueue<>();
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 K = Integer.parseInt(st.nextToken());

for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
int nation = Integer.parseInt(st.nextToken());
int gold = Integer.parseInt(st.nextToken());
int silver = Integer.parseInt(st.nextToken());
int bronze = Integer.parseInt(st.nextToken());
pq.offer(new Nation(nation, gold, silver, bronze));
}

System.out.println(findRank(K));
}

static int findRank(int K){
int rank = 1;
Nation prev = pq.poll();
if(prev.name == K){
return rank;
}

int same = 0;
while(!pq.isEmpty()){
Nation nation = pq.poll();
//동점일 경우
if(prev.gold == nation.gold && prev.silver == nation.silver && prev.bronze == nation.bronze){
same++;
}else {
rank++;
rank += same;
same = 0;
}

if(nation.name == K){
break;
}
prev = nation;
}
return rank;
}
}
142 changes: 142 additions & 0 deletions CodeTree/2019-2020년/YJ_술래잡기_체스.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@

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

public class YJ_술래잡기_체스 {
static class Chess implements Comparable<Chess>{
int x;
int y;
int number;
int direction;

Chess (int x, int y, int number, int direction){
this.x = x;
this.y = y;
this.number = number;
this.direction = direction;
}

@Override
public int compareTo(Chess c){
return this.number - c.number;
}
}

static final int NUM = 4;
static final Chess EMPTY = new Chess(0,0,0,0);
static Chess[][] game = new Chess[NUM][NUM];
static PriorityQueue<Chess> pq = new PriorityQueue<>();
static int score = 0;
static Chess tagger;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//초기값 세팅
for(int i=0; i<NUM; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j=0; j<NUM; j++){
int number = Integer.parseInt(st.nextToken());
int direction = Integer.parseInt(st.nextToken())-1;
Chess chess = new Chess(i,j,number,direction);
game[i][j] = chess;
pq.offer(chess);
}
}
//술래 위치 세팅
score = game[0][0].number;
tagger = game[0][0];
game[0][0] = null;

//체스 시작
boolean flag = true;
while(flag){
moveThiefs(); //말 이동
flag = moveTagger(); //술래 이동
}
System.out.println(score);
}

// ↑, ↖, ←, ↙, ↓, ↘, →, ↗
static int[] nx = {-1,-1,0,1,1,1,0,-1};
static int[] ny = {0,-1,-1,-1,0,1,1,1};
public static void moveThiefs(){
while(!pq.isEmpty()){
Chess thief = pq.poll(); //체스 자리 찾을 때까지 이동
int x = thief.x;
int y = thief.y;
//FIXME 자리찾기
findSpace(thief);

//FIXME 자리교체
Chess temp = game[thief.x][thief.y];
game[thief.x][thief.y] = thief;
game[x][y] = temp;
}
//pq 채우기
for(Chess[] rows : game){
for(Chess chess : rows){
pq.offer(chess);
}
}
}

private static void findSpace(Chess chess){
int tempD = chess.direction;
for (int i=0; i<NUM*2; i++){
tempD %= 8;
int tempX = chess.x + nx[tempD];
int tempY = chess.y + ny[tempD];
if(stop(tempX,tempY) || Objects.isNull(game[tempX][tempY])){
tempD++;
continue;
}
chess.x = tempX;
chess.y = tempY;
return;
}
}

//bfs 탐색을 통해 갈 수 있는 곳 중 가장 큰 말 찾기
public static boolean moveTagger(){
Deque<Chess> deque = new ArrayDeque<>();
Chess maxThief = EMPTY;

boolean[][] visited = new boolean[NUM][NUM];
deque.offer(tagger);
visited[tagger.x][tagger.y] = true;

while(!deque.isEmpty()){
Chess tagger = deque.poll();
for (int i=0; i<8; i++){
int tempX = tagger.x + nx[i];
int tempY = tagger.y + ny[i];
//술래말은 도둑말이 없는 곳으로는 이동할 수 없습니다
if(stop(tempX,tempY) || visited[tagger.x][tagger.y] || game[tempX][tempY].number == 0){
continue;
}

visited[tagger.x][tagger.y] = true;
Chess thief = game[tempX][tempY];
deque.offer(thief);
if(thief.number > maxThief.number){
maxThief = thief;
}
}
}

if(maxThief.number != 0){
game[tagger.x][tagger.y] = EMPTY; //기존에 술래가 있던 위치 빈공간으로 만들기
//술래 위치 이동
score += maxThief.number;
tagger = game[maxThief.x][maxThief.y];
game[maxThief.x][maxThief.y] = null;
return true;
}
return false; //도둑말을 잡지 못했다면 게임 종료
}


private static boolean stop(int x, int y){
return x < 0 || x >= NUM || y < 0 || y >= NUM;
}
}
29 changes: 29 additions & 0 deletions Programmers/Level2/YJ_250135.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class YJ_250135 {
public int solution(int h1, int m1, int s1, int h2, int m2, int s2) {
int answer = getAlarms(h2,m2,s2)-getAlarms(h1,m1,s1);
return s1==0 && m1==0? answer+1 : answer;
}

int getAlarms(int h, int m, int s){
int alarms = 0;

int mCount = h * (60-1) + m; //1시간에 59번(60분 제외) + 1분당 1번
int hCount = h * 60 + m;
if(h>=12) {
hCount--; //24시인 경우 -1
}


//초침과 분침이 겹칠 경우
if(s*6 >= m*6 + s*0.1){ // 초침의 각도 = s * 360/60 , 분침의 각도 = m * 360/60 + s * 360/(60*60)
mCount++;
}
//초침과 시침이 겹칠 경우
if(30*(h%12) + 0.5*m + s * ((double) 1 / 120) <= s*6){ // 시침의 각도 = (h%12) * 360/12 + m * 360/(12*60) + s * 360 / (12*60*60)
hCount++;
}

alarms = mCount + hCount;
return h>=12? alarms-1 : alarms;
}
}
8 changes: 8 additions & 0 deletions SQL/13주차/YJ_Students and Examinations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT
s.student_id, s.student_name, su.subject_name, COUNT(e.subject_name) attended_exams
FROM Students s
CROSS JOIN Subjects su
LEFT JOIN Examinations e
ON s.student_id = e.student_id AND su.subject_name = e.subject_name
GROUP BY s.student_id, s.student_name, su.subject_name
ORDER BY s.student_id, su.subject_name
Loading