Skip to content

Commit 951fb11

Browse files
authored
Merge pull request #183 from yeahdy/main
[13주차] 이예진
2 parents 39abee8 + aef4beb commit 951fb11

File tree

7 files changed

+408
-0
lines changed

7 files changed

+408
-0
lines changed

BOJ/1000-5000번/YJ_1194.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
//bfs + 비트마스킹
5+
//처음에 dfs 로 접근했는데, 그럼 모든 경로를 다 탐색 해 버리더라구요 최단거리 문제는 bfs 로 풀기!
6+
public class YJ_1194 {
7+
static class Pos {
8+
int x;
9+
int y;
10+
int distance;
11+
int hasKey;
12+
13+
public Pos(int x, int y, int distance, int hasKey) {
14+
this.x = x;
15+
this.y = y;
16+
this.distance = distance;
17+
this.hasKey = hasKey;
18+
}
19+
}
20+
21+
static final char EXIT = '1';
22+
static final char CURRENT = '0';
23+
static final char WALL = '#';
24+
25+
static int N;
26+
static int M;
27+
static char[][] maze;
28+
29+
public static void main(String[] args) throws IOException {
30+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
31+
StringTokenizer st = new StringTokenizer(br.readLine());
32+
N = Integer.parseInt(st.nextToken());
33+
M = Integer.parseInt(st.nextToken());
34+
maze = new char[N][M];
35+
visited = new boolean[N][M][64]; //★키 6개 2^6 = 64
36+
37+
int x = 0;
38+
int y = 0;
39+
for(int i=0; i<N; i++) {
40+
String data = br.readLine();
41+
for(int j=0; j<M; j++) {
42+
maze[i][j] = data.charAt(j);
43+
if(maze[i][j] == CURRENT) {
44+
x = i;
45+
y = j;
46+
}
47+
}
48+
}
49+
50+
System.out.println(escape(x,y));
51+
}
52+
53+
static boolean[][][] visited;
54+
static int escape(int x, int y){
55+
Deque<Pos> pq = new ArrayDeque<>();
56+
int[] dx = {0,1,0,-1};
57+
int[] dy = {1,0,-1,0};
58+
59+
pq.offer(new Pos(x,y,0,0));
60+
visited[x][y][0] = true;
61+
62+
while(!pq.isEmpty()) {
63+
Pos pos = pq.poll();
64+
if(maze[pos.x][pos.y] == EXIT) {
65+
return pos.distance;
66+
}
67+
68+
for(int d=0; d<4; d++){
69+
int nx = pos.x + dx[d];
70+
int ny = pos.y + dy[d];
71+
if(stop(nx,ny, pos.hasKey)){
72+
continue;
73+
}
74+
75+
int key = pos.hasKey;
76+
if(maze[nx][ny] >= 'A' && maze[nx][ny] <= 'F') {
77+
if((key & 1<<(maze[nx][ny] - 'A')) > 0){ //가지고 있는 키와 일치하는 경우 (A=65)
78+
visited[nx][ny][key] = true;
79+
}else{
80+
continue;
81+
}
82+
}else if(maze[nx][ny] >= 'a' && maze[nx][ny] <= 'f'){ //키를 주운 경우
83+
key |= 1<<(maze[nx][ny] - 'a'); //a=97
84+
visited[nx][ny][key] = true;
85+
}else{
86+
visited[nx][ny][key] = true; //'.'
87+
}
88+
89+
pq.offer(new Pos(nx,ny,pos.distance+1,key));
90+
}
91+
}
92+
93+
return -1;
94+
}
95+
96+
static private boolean stop(int x, int y, int hasKey){
97+
return x < 0 || x >= N || y < 0 || y >= M || maze[x][y] == WALL || visited[x][y][hasKey];
98+
}
99+
100+
}

BOJ/1000-5000번/YJ_1749.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class YJ_1749 {
5+
public static void main(String[] args) throws IOException {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
StringTokenizer st = new StringTokenizer(br.readLine());
8+
int N = Integer.parseInt(st.nextToken())+1;
9+
int M = Integer.parseInt(st.nextToken())+1;
10+
int [][] game = new int[N][M];
11+
12+
for(int i = 1; i < N; i++) {
13+
st = new StringTokenizer(br.readLine());
14+
for(int j = 1; j < M; j++) {
15+
game[i][j] = Integer.parseInt(st.nextToken());
16+
}
17+
}
18+
//누적합
19+
for(int i = 1; i < N; i++) {
20+
for(int j = 1; j < M; j++) {
21+
game[i][j] = game[i-1][j] + game[i][j-1] - game[i-1][j-1] + game[i][j];
22+
}
23+
}
24+
25+
int max = game[1][1];
26+
for(int i = 1; i < N; i++) {
27+
for(int j = 1; j < M; j++) {
28+
for(int r = 0; r < i; r++) {
29+
for(int k = 0; k < j; k++) {
30+
max = Math.max(max, game[i][j] - game[r][j] - game[i][k] + game[r][k]);
31+
}
32+
}
33+
}
34+
}
35+
System.out.println(max);
36+
}
37+
}

BOJ/5001-10000번/YJ_8979.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class YJ_8979 {
5+
static class Nation implements Comparable<Nation> {
6+
int name;
7+
int gold;
8+
int silver;
9+
int bronze;
10+
11+
public Nation(int name, int gold, int silver, int bronze) {
12+
this.name = name;
13+
this.gold = gold;
14+
this.silver = silver;
15+
this.bronze = bronze;
16+
}
17+
18+
@Override
19+
public int compareTo(Nation o) {
20+
if(this.gold == o.gold){
21+
if(this.silver == o.silver){
22+
return o.bronze - this.bronze;
23+
}
24+
return o.silver - this.silver;
25+
}
26+
return o.gold - this.gold;
27+
}
28+
}
29+
30+
static PriorityQueue<Nation> pq = new PriorityQueue<>();
31+
public static void main(String[] args) throws IOException {
32+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
33+
StringTokenizer st = new StringTokenizer(br.readLine());
34+
int N = Integer.parseInt(st.nextToken());
35+
int K = Integer.parseInt(st.nextToken());
36+
37+
for(int i=0; i<N; i++){
38+
st = new StringTokenizer(br.readLine());
39+
int nation = Integer.parseInt(st.nextToken());
40+
int gold = Integer.parseInt(st.nextToken());
41+
int silver = Integer.parseInt(st.nextToken());
42+
int bronze = Integer.parseInt(st.nextToken());
43+
pq.offer(new Nation(nation, gold, silver, bronze));
44+
}
45+
46+
System.out.println(findRank(K));
47+
}
48+
49+
static int findRank(int K){
50+
int rank = 1;
51+
Nation prev = pq.poll();
52+
if(prev.name == K){
53+
return rank;
54+
}
55+
56+
int same = 0;
57+
while(!pq.isEmpty()){
58+
Nation nation = pq.poll();
59+
//동점일 경우
60+
if(prev.gold == nation.gold && prev.silver == nation.silver && prev.bronze == nation.bronze){
61+
same++;
62+
}else {
63+
rank++;
64+
rank += same;
65+
same = 0;
66+
}
67+
68+
if(nation.name == K){
69+
break;
70+
}
71+
prev = nation;
72+
}
73+
return rank;
74+
}
75+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class YJ_술래잡기_체스 {
6+
static class Chess implements Comparable<Chess>{
7+
int x;
8+
int y;
9+
int number;
10+
int direction;
11+
12+
Chess (int x, int y, int number, int direction){
13+
this.x = x;
14+
this.y = y;
15+
this.number = number;
16+
this.direction = direction;
17+
}
18+
19+
@Override
20+
public int compareTo(Chess c){
21+
return this.number - c.number;
22+
}
23+
}
24+
25+
static final int NUM = 4;
26+
static final Chess EMPTY = new Chess(0,0,0,0);
27+
static Chess[][] game = new Chess[NUM][NUM];
28+
static PriorityQueue<Chess> pq = new PriorityQueue<>();
29+
static int score = 0;
30+
static Chess tagger;
31+
32+
public static void main(String[] args) throws IOException {
33+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
34+
//초기값 세팅
35+
for(int i=0; i<NUM; i++){
36+
StringTokenizer st = new StringTokenizer(br.readLine());
37+
for(int j=0; j<NUM; j++){
38+
int number = Integer.parseInt(st.nextToken());
39+
int direction = Integer.parseInt(st.nextToken())-1;
40+
Chess chess = new Chess(i,j,number,direction);
41+
game[i][j] = chess;
42+
pq.offer(chess);
43+
}
44+
}
45+
//술래 위치 세팅
46+
score = game[0][0].number;
47+
tagger = game[0][0];
48+
game[0][0] = null;
49+
50+
//체스 시작
51+
boolean flag = true;
52+
while(flag){
53+
moveThiefs(); //말 이동
54+
flag = moveTagger(); //술래 이동
55+
}
56+
System.out.println(score);
57+
}
58+
59+
// ↑, ↖, ←, ↙, ↓, ↘, →, ↗
60+
static int[] nx = {-1,-1,0,1,1,1,0,-1};
61+
static int[] ny = {0,-1,-1,-1,0,1,1,1};
62+
public static void moveThiefs(){
63+
while(!pq.isEmpty()){
64+
Chess thief = pq.poll(); //체스 자리 찾을 때까지 이동
65+
int x = thief.x;
66+
int y = thief.y;
67+
//FIXME 자리찾기
68+
findSpace(thief);
69+
70+
//FIXME 자리교체
71+
Chess temp = game[thief.x][thief.y];
72+
game[thief.x][thief.y] = thief;
73+
game[x][y] = temp;
74+
}
75+
//pq 채우기
76+
for(Chess[] rows : game){
77+
for(Chess chess : rows){
78+
pq.offer(chess);
79+
}
80+
}
81+
}
82+
83+
private static void findSpace(Chess chess){
84+
int tempD = chess.direction;
85+
for (int i=0; i<NUM*2; i++){
86+
tempD %= 8;
87+
int tempX = chess.x + nx[tempD];
88+
int tempY = chess.y + ny[tempD];
89+
if(stop(tempX,tempY) || Objects.isNull(game[tempX][tempY])){
90+
tempD++;
91+
continue;
92+
}
93+
chess.x = tempX;
94+
chess.y = tempY;
95+
return;
96+
}
97+
}
98+
99+
//bfs 탐색을 통해 갈 수 있는 곳 중 가장 큰 말 찾기
100+
public static boolean moveTagger(){
101+
Deque<Chess> deque = new ArrayDeque<>();
102+
Chess maxThief = EMPTY;
103+
104+
boolean[][] visited = new boolean[NUM][NUM];
105+
deque.offer(tagger);
106+
visited[tagger.x][tagger.y] = true;
107+
108+
while(!deque.isEmpty()){
109+
Chess tagger = deque.poll();
110+
for (int i=0; i<8; i++){
111+
int tempX = tagger.x + nx[i];
112+
int tempY = tagger.y + ny[i];
113+
//술래말은 도둑말이 없는 곳으로는 이동할 수 없습니다
114+
if(stop(tempX,tempY) || visited[tagger.x][tagger.y] || game[tempX][tempY].number == 0){
115+
continue;
116+
}
117+
118+
visited[tagger.x][tagger.y] = true;
119+
Chess thief = game[tempX][tempY];
120+
deque.offer(thief);
121+
if(thief.number > maxThief.number){
122+
maxThief = thief;
123+
}
124+
}
125+
}
126+
127+
if(maxThief.number != 0){
128+
game[tagger.x][tagger.y] = EMPTY; //기존에 술래가 있던 위치 빈공간으로 만들기
129+
//술래 위치 이동
130+
score += maxThief.number;
131+
tagger = game[maxThief.x][maxThief.y];
132+
game[maxThief.x][maxThief.y] = null;
133+
return true;
134+
}
135+
return false; //도둑말을 잡지 못했다면 게임 종료
136+
}
137+
138+
139+
private static boolean stop(int x, int y){
140+
return x < 0 || x >= NUM || y < 0 || y >= NUM;
141+
}
142+
}

Programmers/Level2/YJ_250135.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class YJ_250135 {
2+
public int solution(int h1, int m1, int s1, int h2, int m2, int s2) {
3+
int answer = getAlarms(h2,m2,s2)-getAlarms(h1,m1,s1);
4+
return s1==0 && m1==0? answer+1 : answer;
5+
}
6+
7+
int getAlarms(int h, int m, int s){
8+
int alarms = 0;
9+
10+
int mCount = h * (60-1) + m; //1시간에 59번(60분 제외) + 1분당 1번
11+
int hCount = h * 60 + m;
12+
if(h>=12) {
13+
hCount--; //24시인 경우 -1
14+
}
15+
16+
17+
//초침과 분침이 겹칠 경우
18+
if(s*6 >= m*6 + s*0.1){ // 초침의 각도 = s * 360/60 , 분침의 각도 = m * 360/60 + s * 360/(60*60)
19+
mCount++;
20+
}
21+
//초침과 시침이 겹칠 경우
22+
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)
23+
hCount++;
24+
}
25+
26+
alarms = mCount + hCount;
27+
return h>=12? alarms-1 : alarms;
28+
}
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SELECT
2+
s.student_id, s.student_name, su.subject_name, COUNT(e.subject_name) attended_exams
3+
FROM Students s
4+
CROSS JOIN Subjects su
5+
LEFT JOIN Examinations e
6+
ON s.student_id = e.student_id AND su.subject_name = e.subject_name
7+
GROUP BY s.student_id, s.student_name, su.subject_name
8+
ORDER BY s.student_id, su.subject_name

0 commit comments

Comments
 (0)