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

Commit 89266f1

Browse files
authored
Merge pull request #182 from GreatAlgorithm-Study/xubin
[13주차] 배수빈
2 parents 951fb11 + 5ab0fed commit 89266f1

File tree

8 files changed

+481
-0
lines changed

8 files changed

+481
-0
lines changed

‎BOJ/1000-5000번/SB_1194.java‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class SB_1194 {
7+
static int N, M;
8+
static char[][] board;
9+
static Node start;
10+
static int ans = Integer.MAX_VALUE;
11+
static int[][][] visited;
12+
static int[] dx = {-1, 1, 0, 0};
13+
static int[] dy = {0, 0, -1, 1};
14+
15+
private static void bfs() {
16+
Queue<Node> que = new ArrayDeque<>();
17+
visited[start.x][start.y][0] = 0;
18+
que.offer(new Node(start.x, start.y, 0));
19+
20+
while (!que.isEmpty()) {
21+
Node cur = que.poll();
22+
23+
if (board[cur.x][cur.y] == '1') {
24+
ans = Math.min(ans, visited[cur.x][cur.y][cur.keys]);
25+
continue;
26+
}
27+
28+
for (int i = 0; i < 4; i++) {
29+
int nx = cur.x + dx[i];
30+
int ny = cur.y + dy[i];
31+
int nk = cur.keys;
32+
if (!isValid(nx, ny) || board[nx][ny]=='#' || visited[nx][ny][nk]!=-1) continue;
33+
34+
char nxt = board[nx][ny];
35+
// 이동할 곳이 알파벳일 경우
36+
if (Character.isAlphabetic(nxt)){
37+
if (Character.isLowerCase(nxt)) nk |= (1 << (nxt - 'a')); // 키면 키 줍기
38+
else if (Character.isUpperCase(nxt)) {
39+
if ((nk & (1<< (nxt-'A')))==0) continue; // 키 없는 문은 못감
40+
}
41+
}
42+
43+
// 이동 가능할 경우 이동
44+
que.offer(new Node(nx, ny, nk));
45+
visited[nx][ny][nk] = visited[cur.x][cur.y][cur.keys] + 1;
46+
}
47+
}
48+
}
49+
50+
private static boolean isValid(int x, int y) {
51+
return 0 <= x && x < N && 0 <= y && y < M;
52+
}
53+
public static void main(String[] args) throws IOException {
54+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
55+
StringTokenizer st = new StringTokenizer(br.readLine());
56+
57+
N = Integer.parseInt(st.nextToken());
58+
M = Integer.parseInt(st.nextToken());
59+
60+
board = new char[N][M];
61+
visited = new int[N][M][1 << 6]; // 키 6개 존재 2^6
62+
for (int i = 0; i < N; i++) {
63+
String line = br.readLine();
64+
for (int j = 0; j < M; j++) {
65+
board[i][j] = line.charAt(j);
66+
if (board[i][j]=='0'){
67+
start = new Node(i, j, 0);
68+
board[i][j] = '.';
69+
}
70+
}
71+
}
72+
73+
for (int i = 0; i < N; i++) { // 방문배열 초기화
74+
for (int j = 0; j < M; j++) {
75+
Arrays.fill(visited[i][j], -1);
76+
}
77+
}
78+
79+
bfs();
80+
System.out.println(ans==Integer.MAX_VALUE ? -1 : ans);
81+
}
82+
static class Node{
83+
int x, y, keys;
84+
85+
public Node(int x, int y, int keys) {
86+
this.x = x;
87+
this.y = y;
88+
this.keys = keys;
89+
}
90+
91+
@Override
92+
public String toString() {
93+
return "Node{" +
94+
"x=" + x +
95+
", y=" + y +
96+
", keys=" + keys +
97+
'}';
98+
}
99+
}
100+
}

‎BOJ/1000-5000번/SB_1749.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class SB_1749 {
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
StringTokenizer st = new StringTokenizer(br.readLine());
10+
11+
int N = Integer.parseInt(st.nextToken());
12+
int M = Integer.parseInt(st.nextToken());
13+
14+
int[][] board = new int[N][M];
15+
long[][] dp = new long[N + 1][M + 1];
16+
for (int i = 0; i < N; i++) {
17+
st = new StringTokenizer(br.readLine());
18+
for (int j = 0; j < M; j++) {
19+
board[i][j] = Integer.parseInt(st.nextToken());
20+
}
21+
}
22+
23+
// 누적합 계산
24+
for (int i = 1; i < N + 1; i++) {
25+
for (int j = 1; j < M + 1; j++) {
26+
dp[i][j] = board[i-1][j-1] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
27+
}
28+
}
29+
30+
// 부분 행렬의 최대값 찾기
31+
long mx = Long.MIN_VALUE;
32+
for (int r1 = 1; r1 < N + 1; r1++) {
33+
for (int c1 = 1; c1 <= M + 1; c1++) {
34+
for (int r2 = r1; r2 <= N; r2++) {
35+
for (int c2 = c1; c2 <= M; c2++) {
36+
long tmp = dp[r2][c2] - dp[r1 - 1][c2] - dp[r2][c1 - 1] + dp[r1 - 1][c1 - 1];
37+
mx = Math.max(mx, tmp);
38+
}
39+
}
40+
}
41+
}
42+
43+
System.out.println(mx);
44+
}
45+
}

‎BOJ/5001-10000번/SB_8979.java‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class SB_8979 {
7+
static int N, K;
8+
static List<Nation> nations = new ArrayList<>();
9+
private static int medal2Int(int g, int s, int b){
10+
return g * 1000000 + s * 1000 + b;
11+
}
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
StringTokenizer st = new StringTokenizer(br.readLine());
15+
16+
N = Integer.parseInt(st.nextToken());
17+
K = Integer.parseInt(st.nextToken());
18+
19+
for (int i = 0; i < N; i++) {
20+
st = new StringTokenizer(br.readLine());
21+
int idx = Integer.parseInt(st.nextToken());
22+
int g = Integer.parseInt(st.nextToken());
23+
int s = Integer.parseInt(st.nextToken());
24+
int b = Integer.parseInt(st.nextToken());
25+
nations.add(new Nation(idx, medal2Int(g, s, b)));
26+
}
27+
28+
Collections.sort(nations);
29+
30+
int rank = 1;
31+
int tmp = 1; // 동점자 처리를 위한 변수(매번 증가)
32+
int preScore = nations.get(0).score;
33+
for (Nation cur : nations) {
34+
if (cur.score != preScore) rank = tmp; // 서로 점수가 다르면 순위 갱신
35+
if (cur.idx==K){ // 찾는 나라면 순위 출력
36+
System.out.println(rank);
37+
return;
38+
}
39+
tmp++;
40+
preScore = cur.score;
41+
}
42+
}
43+
44+
static class Nation implements Comparable<Nation>{
45+
int idx, score;
46+
47+
public Nation(int idx, int score) {
48+
this.idx = idx;
49+
this.score = score;
50+
}
51+
52+
@Override
53+
public int compareTo(Nation o) {
54+
return o.score - this.score;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "Nation{" +
60+
"idx=" + idx +
61+
", score=" + score +
62+
'}';
63+
}
64+
}
65+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class SB_술래잡기_체스 {
7+
static int mx = -1;
8+
static int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1};
9+
static int[] dy = {0, -1, -1, -1, 0, 1, 1, 1};
10+
11+
private static int[][] copyBoard(int[][] board) {
12+
int[][] tmp = new int[4][4];
13+
for (int i = 0; i < 4; i++) {
14+
for (int j = 0; j < 4; j++) {
15+
tmp[i][j] = board[i][j];
16+
}
17+
}
18+
return tmp;
19+
}
20+
21+
private static List<Piece> copyPiece(List<Piece> pieces) {
22+
List<Piece> tmpList = new ArrayList<>();
23+
for (Piece p : pieces) {
24+
tmpList.add(new Piece(p.idx, p.x, p.y, p.dir, p.isDie));
25+
}
26+
return tmpList;
27+
}
28+
private static void dfs(Tagger it, int[][] board, List<Piece> pieces) {
29+
// 도둑말들 움직이기
30+
moveThief(board, pieces);
31+
32+
// 현재 방향으로 잡을 수 있는 말 잡기(최대 3번 이동 가능)
33+
for (int i = 1; i < 4; i++) {
34+
int nx = it.x + dx[it.dir] * i;
35+
int ny = it.y + dy[it.dir] * i;
36+
37+
if (isValid(nx, ny) && board[nx][ny] != 0) {
38+
int[][] tmpBoard = copyBoard(board);
39+
List<Piece> tmpPiece = copyPiece(pieces);
40+
41+
tmpBoard[it.x][it.y] = 0;
42+
Piece thief = tmpPiece.get(board[nx][ny]);
43+
Tagger nit = new Tagger(thief.x, thief.y, thief.dir, it.cnt + thief.idx);
44+
thief.isDie = true;
45+
tmpBoard[thief.x][thief.y] = -1;
46+
47+
dfs(nit, tmpBoard, tmpPiece);
48+
}
49+
}
50+
mx = Math.max(mx, it.cnt);
51+
}
52+
53+
54+
private static void moveThief(int[][] board, List<Piece> pieces) {
55+
for (Piece cur : pieces) {
56+
if (cur.idx == 0 || cur.isDie) continue; // 죽었거나 술래면 스킵
57+
for (int d = 0; d < 8; d++) {
58+
int nd = (cur.dir + d) % 8;
59+
int nx = cur.x + dx[nd];
60+
int ny = cur.y + dy[nd];
61+
if (!isValid(nx, ny) || board[nx][ny] == -1) continue;
62+
board[cur.x][cur.y] = 0;
63+
if (board[nx][ny] == 0) cur.setPos(nx, ny); // 빈칸이면 바로 이동
64+
else { // 다른말있으면 교환
65+
Piece target = pieces.get(board[nx][ny]);
66+
target.setPos(cur.x, cur.y);
67+
board[cur.x][cur.y] = target.idx;
68+
cur.setPos(nx, ny);
69+
}
70+
board[nx][ny] = cur.idx; // 움직일 보드에 현재 말 놓기
71+
cur.dir = nd; // 현재 말의 방향 갱신
72+
break;
73+
}
74+
}
75+
}
76+
77+
private static boolean isValid(int x, int y) {
78+
return 0 <= x && x < 4 && 0 <= y && y < 4;
79+
}
80+
public static void main(String[] args) throws IOException {
81+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
82+
StringTokenizer st;
83+
84+
int[][] board = new int[4][4]; // 말들의 번호로 저장
85+
List<Piece> pieces = new ArrayList<>();
86+
87+
for (int i = 0; i < 4; i++) {
88+
st = new StringTokenizer(br.readLine());
89+
for (int j = 0; j < 4; j++) {
90+
int num = Integer.parseInt(st.nextToken());
91+
int dir = Integer.parseInt(st.nextToken()) - 1;
92+
Piece piece = new Piece(num, i, j, dir, false);
93+
pieces.add(piece);
94+
board[i][j] = num;
95+
}
96+
}
97+
98+
// 도둑말 인덱스 순으로 정렬
99+
pieces.add(new Piece(0, -1, -1, 0, false)); // 인덱스 0맞추기 위해 더미 객체 넣기
100+
pieces.sort(((o1, o2) -> o1.idx - o2.idx));
101+
102+
// 초기 0,0 도둑말 잡기
103+
Piece first = pieces.get(board[0][0]);
104+
Tagger tagger = new Tagger(0, 0, first.dir, first.idx);
105+
board[0][0] = -1;
106+
first.isDie = true;
107+
108+
// 술래말의 재귀 시작
109+
dfs(tagger, board, pieces);
110+
111+
System.out.println(mx);
112+
}
113+
114+
static class Tagger{
115+
int x, y, dir, cnt;
116+
117+
public Tagger(int x, int y, int dir, int cnt) {
118+
this.x = x;
119+
this.y = y;
120+
this.dir = dir;
121+
this.cnt = cnt;
122+
}
123+
}
124+
125+
static class Piece{
126+
int idx, x, y, dir;
127+
boolean isDie;
128+
129+
public Piece(int idx, int x, int y, int dir, boolean isDie) {
130+
this.idx = idx;
131+
this.x = x;
132+
this.y = y;
133+
this.dir = dir;
134+
this.isDie = isDie;
135+
}
136+
137+
private void setPos(int x, int y) {
138+
this.x = x;
139+
this.y = y;
140+
}
141+
}
142+
}

0 commit comments

Comments
(0)

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