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 7c0c57d

Browse files
committed
2 parents 33b6ec6 + 6748c47 commit 7c0c57d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2377
-5
lines changed

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 달이 차오른다, 가자.
6+
*/
7+
8+
public class DH_1194 {
9+
static class Point {
10+
int r, c;
11+
public Point(int r, int c) {
12+
this.r = r;
13+
this.c = c;
14+
}
15+
}
16+
17+
static class Node {
18+
Point p;
19+
int status, cnt;
20+
public Node(Point p, int status, int cnt) {
21+
this.p = p;
22+
this.status = status;
23+
this.cnt = cnt;
24+
}
25+
}
26+
static int N, M;
27+
static char[][] map;
28+
static boolean[][][] v;
29+
static Queue<Node> q;
30+
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
31+
32+
public static void main(String[] args) throws Exception {
33+
initInput();
34+
bfs();
35+
}
36+
37+
static void bfs() {
38+
int result = -1;
39+
40+
while(!q.isEmpty()) {
41+
Node current = q.poll();
42+
43+
if(map[current.p.r][current.p.c] == '1') {
44+
result = current.cnt;
45+
break;
46+
}
47+
48+
for(int d = 0; d < 4; d++) {
49+
int nr = current.p.r + dr[d];
50+
int nc = current.p.c + dc[d];
51+
int nstatus = current.status;
52+
53+
if(!check(nr, nc) || v[nstatus][nr][nc] || map[nr][nc] == '#') continue;
54+
// 다음 지점이 문인 경우: 키가 없다면 continue
55+
if(isDoor(map[nr][nc]) && ((current.status & (1 << map[nr][nc] - 'A')) == 0)) continue;
56+
// 다음 지점에 키가 있는 경우: status에 or 연산 해주기
57+
if(isKey(map[nr][nc])) nstatus |= (1 << map[nr][nc] - 'a');
58+
59+
q.add(new Node(new Point(nr, nc), nstatus, current.cnt + 1));
60+
v[nstatus][nr][nc] = true;
61+
}
62+
}
63+
64+
System.out.println(result);
65+
}
66+
67+
static boolean isDoor(char c) {
68+
return c >= 'A' && c <= 'F';
69+
}
70+
71+
static boolean isKey(char c) {
72+
return c >= 'a' && c <= 'f';
73+
}
74+
75+
static boolean check(int r, int c) {
76+
return r >= 0 && r < N && c >= 0 && c < M;
77+
}
78+
static void printMap() {
79+
for(int r = 0; r < map.length; r++) {
80+
System.out.println(Arrays.toString(map[r]));
81+
}
82+
}
83+
static void initInput() throws Exception {
84+
System.setIn(new FileInputStream("./input/BOJ1194.txt"));
85+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
86+
StringTokenizer st = new StringTokenizer(br.readLine());
87+
88+
N = Integer.parseInt(st.nextToken());
89+
M = Integer.parseInt(st.nextToken());
90+
91+
map = new char[N][M];
92+
v = new boolean[1 << 6][N][M];
93+
94+
q = new ArrayDeque<>();
95+
96+
for(int r = 0; r < N; r++) {
97+
String s = br.readLine();
98+
99+
for(int c = 0; c < M; c++) {
100+
map[r][c] = s.charAt(c);
101+
if(map[r][c] == '0') {
102+
q.add(new Node(new Point(r, c), 0, 0));
103+
v[0][r][c] = true;
104+
}
105+
}
106+
}
107+
}
108+
}

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 점수따먹기
6+
*/
7+
8+
public class DH_1749 {
9+
public static void main(String[] args) throws Exception {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
13+
int N = Integer.parseInt(st.nextToken());
14+
int M = Integer.parseInt(st.nextToken());
15+
16+
int[][] S = new int[N + 1][M + 1];
17+
18+
for(int r = 1; r < N + 1; r++) {
19+
st = new StringTokenizer(br.readLine());
20+
21+
for(int c = 1; c < M + 1; c++) {
22+
int current = Integer.parseInt(st.nextToken());
23+
S[r][c] = S[r - 1][c] + S[r][c - 1] - S[r - 1][c - 1] + current;
24+
}
25+
}
26+
27+
int max = Integer.MIN_VALUE;
28+
for(int r = 1; r < S.length; r++) {
29+
for(int c = 1; c < S[0].length; c++) {
30+
for(int rr = 1; rr < r + 1; rr++) {
31+
for(int cc = 1; cc < c + 1; cc++) {
32+
// arr[rr][cc]에서 arr[r][c] 까지 누적합 구하기
33+
// 제일 큰 직사각형
34+
int s1 = S[r][c];
35+
36+
// 아래 직사각형
37+
int s2 = S[rr - 1][c];
38+
39+
// 왼쪽 직사각형
40+
int s3 = S[r][cc - 1];
41+
42+
// 공통 직사각형
43+
int s4 = S[rr - 1][cc - 1];
44+
45+
int sum = s1 - s2 - s3 + s4;
46+
max = Math.max(max, sum);
47+
}
48+
}
49+
}
50+
}
51+
52+
System.out.println(max);
53+
}
54+
}

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
// 미로를 탈출하는데 드는 이동 횟수의 최솟값을 출력
5+
6+
public class HW_1194 {
7+
static int N, M;
8+
static char[][] board;
9+
static boolean[][][] visited; // 방문
10+
static int[] dx = {0, 1, 0, -1};
11+
static int[] dy = {1, 0, -1, 0};
12+
static class Node{
13+
int x, y, key, step;
14+
public Node(int x, int y, int key, int step) {
15+
this.x = x;
16+
this.y = y;
17+
this.key = key;
18+
this.step = step;
19+
}
20+
}
21+
22+
public static void main(String[] args) throws IOException {
23+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
24+
StringTokenizer st = new StringTokenizer(br.readLine());
25+
26+
N = Integer.parseInt(st.nextToken());
27+
M = Integer.parseInt(st.nextToken());
28+
29+
board = new char[N][M];
30+
visited = new boolean[N][M][64];
31+
32+
int startX = 0, startY = 0;
33+
for(int i=0; i<N; i++){
34+
String input = br.readLine();
35+
for(int j=0; j<M; j++){
36+
board[i][j] = input.charAt(j);
37+
if(board[i][j]=='0'){ // 시작 위치
38+
startX = i;
39+
startY = j;
40+
}
41+
}
42+
}
43+
int ans = bfs(startX, startY);
44+
System.out.println(ans);
45+
}
46+
static int bfs(int x, int y){
47+
Queue<Node> queue = new LinkedList<>();
48+
queue.add(new Node(x, y, 0, 0));
49+
visited[x][y][0] = true;
50+
board[x][y] = '.'; // 빈칸: 이동 가능
51+
52+
while(!queue.isEmpty()){
53+
Node cur = queue.poll();
54+
if(board[cur.x][cur.y]=='1'){
55+
return cur.step;
56+
}
57+
for(int i=0; i<4; i++){
58+
int nx = cur.x + dx[i];
59+
int ny = cur.y + dy[i];
60+
int nkey = cur.key;
61+
62+
if(!isValid(nx,ny) || visited[nx][ny][nkey] || board[nx][ny]=='#') {
63+
continue;
64+
}
65+
66+
if ('a' <= board[nx][ny] && board[nx][ny] <= 'f') {
67+
nkey |= (1 << (board[nx][ny] - 'a')); // 새로운 열쇠 획득
68+
}
69+
70+
if ('A' <= board[nx][ny]&& board[nx][ny] <= 'F') {
71+
if ((nkey & (1 << (board[nx][ny] - 'A'))) == 0) {
72+
continue; // 열쇠 없으면 이동 불가
73+
}
74+
}
75+
76+
if (!visited[nx][ny][nkey]) { // 새 열쇠 상태로 방문
77+
queue.add(new Node(nx, ny, nkey, cur.step + 1));
78+
visited[nx][ny][nkey] = true;
79+
}
80+
}
81+
}
82+
return -1;
83+
}
84+
static boolean isValid(int nx, int ny){
85+
return 0<=nx && nx<N && 0<=ny && ny <M;
86+
}
87+
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.io.*;
2+
import java.util.StringTokenizer;
3+
4+
public class HW_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());
9+
int M = Integer.parseInt(st.nextToken());
10+
11+
int[][] board = new int[N+1][M+1];
12+
int[][] dp = new int[N+1][M+1];
13+
for(int i=1; i<=N; i++){
14+
st = new StringTokenizer(br.readLine());
15+
for(int j=1; j<=M; j++){
16+
board[i][j] = Integer.parseInt(st.nextToken());
17+
}
18+
}
19+
20+
for(int i=1; i<=N; i++){
21+
for(int j=1; j<=M; j++){
22+
dp[i][j] = board[i][j] + dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1];
23+
}
24+
}
25+
26+
int max = Integer.MIN_VALUE;
27+
for(int i=1; i<=N; i++){
28+
for(int j=1; j<=M; j++){
29+
for(int r=1; r<=i; r++){ // (r1, c1) ~ (r2, c2)
30+
for(int c=1; c<=j; c++){
31+
max = Math.max(max, dp[i][j]-dp[r-1][j] - dp[i][c-1] + dp[r-1][c-1]);
32+
}
33+
}
34+
}
35+
}
36+
System.out.println(max);
37+
}
38+
}

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import java.io.BufferedReader;
2+
import java.io.InputStreamReader;
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.StringTokenizer;
6+
7+
public class JW_1194 {
8+
9+
static int n, m;
10+
static char[][] board;
11+
12+
static int[] dy = { 1, -1, 0, 0 };
13+
static int[] dx = { 0, 0, 1, -1 };
14+
15+
public static void main(String[] args) throws Exception {
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
n = Integer.parseInt(st.nextToken());
19+
m = Integer.parseInt(st.nextToken());
20+
board = new char[n][m];
21+
int sy = 0, sx = 0;
22+
for (int i = 0; i < n; i++) {
23+
String line = br.readLine();
24+
for (int j = 0; j < m; j++) {
25+
board[i][j] = line.charAt(j);
26+
// 초기 위치 설정
27+
if (board[i][j] == '0') {
28+
sy = i;
29+
sx = j;
30+
}
31+
}
32+
}
33+
// 열쇠 상태에 따라 방문 체크를 해주기 위해서 3차원 방문 체크 생성
34+
boolean[][][] visited = new boolean[n][m][1 << 6];
35+
Deque<int[]> dq = new ArrayDeque<>();
36+
dq.offer(new int[] { sy, sx, 0, 0 });
37+
visited[sy][sx][0] = true;
38+
39+
// BFS
40+
while (!dq.isEmpty()) {
41+
int[] cur = dq.poll();
42+
// 현재 좌표, 열쇠 상태, 움직임 횟수
43+
int y = cur[0], x = cur[1], keys = cur[2], moveCnt = cur[3];
44+
// 종료 조건
45+
if (board[y][x] == '1') {
46+
System.out.println(moveCnt);
47+
return;
48+
}
49+
for (int i = 0; i < 4; i++) {
50+
int ny = y + dy[i], nx = x + dx[i];
51+
int newKeys = keys;
52+
53+
// 이동 조건
54+
if (!isValid(ny, nx) || board[ny][nx] == '#') {
55+
continue;
56+
}
57+
58+
// 열쇠일 경우
59+
if (isKey(ny, nx))
60+
// 해당 열쇠 추가
61+
newKeys |= (1 << (board[ny][nx] - 'a'));
62+
63+
// 문일 경우, 열쇠가 없다면 이동 불가
64+
else if (isDoor(ny, nx) && (keys & (1 << (board[ny][nx] - 'A'))) == 0)
65+
continue;
66+
67+
// 현재 열쇠 상태에서 방문하지 않았다면 방문
68+
if (!visited[ny][nx][newKeys]) {
69+
dq.offer(new int[] { ny, nx, newKeys, moveCnt + 1 });
70+
visited[ny][nx][newKeys] = true;
71+
}
72+
}
73+
}
74+
System.out.println(-1);
75+
}
76+
77+
// 열쇠 체크
78+
private static boolean isKey(int y, int x) {
79+
return 'a' <= board[y][x] && board[y][x] <= 'f';
80+
}
81+
82+
// 문 체크
83+
private static boolean isDoor(int y, int x) {
84+
return 'A' <= board[y][x] && board[y][x] <= 'F';
85+
}
86+
87+
// 경계 체크
88+
private static boolean isValid(int y, int x) {
89+
return 0 <= y && y < n && 0 <= x && x < m;
90+
}
91+
}

0 commit comments

Comments
(0)

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