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

[10주차] 고다혜 #141

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 9 commits into main from dahye
Nov 16, 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
75 changes: 75 additions & 0 deletions BOJ/1000-5000번/DH_1613.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.io.*;
import java.util.*;

/*
* 역사
*/

public class DH_1613 {
static final int INF = Integer.MAX_VALUE;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int[][] arr;

public static void main(String[] args) throws Exception {
initInput();
solution();
getOrder();
}

static void getOrder() throws Exception {
int n = Integer.parseInt(br.readLine());

StringBuilder sb = new StringBuilder();

for(int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());

// s지점에서 e지점으로 가거나, e지점에서 s지점으로 가는 길이 둘 다 없는 경우는 선후 관계를 알 수 없음
if(arr[s][e] == INF && arr[e][s] == INF) sb.append(0);
else if(arr[s][e] != Integer.MAX_VALUE) sb.append(-1); // s에서 e지점으로 갈 수 있다면 s가 더 빠른 사건
else sb.append(1); // 반대의 경우라면 e가 더 빠른 사건
sb.append("\n");
}

System.out.println(sb);
}

// 플로이도워셜을 통해 각 지점간 거리 구하기
static void solution() {
for(int k = 1; k < arr.length; k++) {
for(int s = 1; s < arr.length; s++) {
if(s == k || arr[s][k] == INF) continue;
for(int e = 1; e < arr.length; e++) {
if(e == k || e == s || arr[k][e] == INF) continue;

if(arr[s][e] > arr[s][k] + arr[k][e])
arr[s][e] = arr[s][k] + arr[k][e];
}
}
}
}

static void initInput() throws Exception {
st = new StringTokenizer(br.readLine());

int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());

arr = new int[n + 1][n + 1];

// 초기 지점 간 거리 무한대로 초기화해주기
for(int r = 0; r < arr.length; r++) Arrays.fill(arr[r], INF);

for(int i = 0; i < k; i++) {
st = new StringTokenizer(br.readLine());

int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());

arr[s][e] = 1;
}
}
}
45 changes: 45 additions & 0 deletions BOJ/1000-5000번/DH_2096.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.io.*;
import java.util.*;

/*
* 내려가기
*/

public class DH_2096 {
static int[][] map, max, min;

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

int N = Integer.parseInt(br.readLine());
map = new int[N + 1][3];
max = new int[N + 1][3]; // 각 줄마다 최댓값을 저장할 변수
min = new int[N + 1][3]; // 각 줄마다 최솟값을 저장할 변수

// 최솟값을 저장하는 변수는 무한으로 초기화
for(int r = 1; r < min.length; r++) Arrays.fill(min[r], Integer.MAX_VALUE >> 2);
for(int r = 1; r < map.length; r++) {
st= new StringTokenizer(br.readLine());
for(int c = 0; c < 3; c++) map[r][c] = Integer.parseInt(st.nextToken());

for(int pc = 0; pc < 3; pc++) {
for(int dc = -1; dc < 2; dc++) {
int nc = pc + dc;
if(nc < 0 || nc >= 3) continue;

max[r][nc] = Math.max(max[r - 1][pc] + map[r][nc], max[r][nc]);
min[r][nc] = Math.min(min[r - 1][pc] + map[r][nc], min[r][nc]);
}
}
}

int maxValue = Integer.MIN_VALUE, minValue = Integer.MAX_VALUE;
for(int c = 0; c < 3; c++) {
maxValue = Math.max(maxValue, max[N][c]);
minValue = Math.min(minValue, min[N][c]);
}

System.out.println(maxValue + " " + minValue);
}
}
121 changes: 121 additions & 0 deletions BOJ/1000-5000번/DH_2955.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import java.io.*;
import java.util.*;

/*
* 스도쿠 풀기
*/

public class DH_2955 {
static boolean error; // 스도쿠에 모순이 일어나는지 확인하는 변수
static char[][] map;
static boolean[][][] v; // 1부터 9까지 위치할 수 있는 곳을 표시하는 변수
static HashSet<Integer>[] set; // 스코쿠 판을 3 X 3으로 나누었을 때, 1 ~ 9번째 칸에서 어떤 수가 있는지 저장하는 변수

public static void main(String[] args) throws Exception {
initInput();
System.out.println(solution() ? printMap(): "ERROR");
}

static boolean solution() {
if(error) return false;
L: while(true) {
int putCnt = 0;

// 1부터 9까지 차례로 입력
for(int k = 1; k < 10; k++) {
// map에서 3 X 3크기의 배열씩 확인함
for(int r = 0; r < 7; r += 3) {
for(int c = 0; c < 7; c += 3) {
// 3 X 3 배열에 k가 있으면 continue;
int idx = (r / 3) * 3 + c / 3;
if(set[idx].contains(k)) continue;

// 3 X 3 배열에 k가 없으면 있을 수 있는 지점 개수 세기
int canExistPoint = 0; // 있을 수 있는 지점 개수
int pr = 0, pc = 0;
for(int sr = r; sr < r + 3; sr++) {
for(int sc = c; sc < c + 3; sc++) {
if(!v[k][sr][sc] && map[sr][sc] == '.') {
canExistPoint += 1;
pr = sr;
pc = sc;
}
}
}

// 있을 수 있는 곳이 없으면 잘못된 스도쿠
if(canExistPoint == 0) {
error = true;
break L;
}

// 있을 수 있는 곳이 한 곳이라면
// 숫자 입력하기
if(canExistPoint == 1) {
map[pr][pc] = (char) (k + '0');
// 숫자 입력하고 가로, 세로 표시 남기기
markCross(pr, pc, k);
// 구역에 해당 숫자 저장해주기
set[idx].add(k);
// map에 입력한 숫자 개수 + 1
putCnt += 1;
}
}
}
}

// map에 입력한 숫자가 없다면 더이상 스도쿠 탐색 끝!
if(putCnt == 0) break;

}
return !error;
}

static String printMap() {
StringBuilder sb = new StringBuilder();
for(int r = 0; r < map.length; r++) {
for(int c = 0; c < map[0].length; c++) sb.append(map[r][c]);
sb.append("\n");
}

return sb.toString();
}

static void initInput() throws Exception {
System.setIn(new FileInputStream("./input/BOJ2955.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
v = new boolean[10][9][9];
map = new char[9][9];
set = new HashSet[9];

for(int i = 0; i < 9; i++) set[i] = new HashSet<Integer>();
for(int r = 0; r < map.length; r++) {
String s = br.readLine();
for(int c = 0; c < map[0].length; c++) {
map[r][c] = s.charAt(c);

if(map[r][c] == '.') continue;
int num = map[r][c] - '0';

// map을 입력받으면서 숫자가 입력받은 지점 기준 가로, 세로 표시 남기기
markCross(r, c, num);

// map을 3 X 3으로 나누었을 때, 해당 칸에 어떤 수가 저장되어 있는지 저장
// 숫자를 입력 받았는데, 해당 구역에 이미 그 숫자가 있으면 모순이 일어나는 스도쿠
if(!putNumSet(r, c, num)) error = true;
}
}
}

static boolean putNumSet(int r, int c, int num) {
int idx = (r / 3) * 3 + c / 3;
if(set[idx].contains(num)) return false;
set[idx].add(num);
return true;
}

static void markCross(int r, int c, int num) {
for(int rr = 0; rr < 9; rr++) v[num][rr][c] = true;
for(int cc = 0; cc < 9; cc++) v[num][r][cc] = true;
}
}
Loading

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