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주차] 배수빈 #139

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
yeongleej merged 10 commits into main from xubin
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
10 commits
Select commit Hold shift + click to select a range
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
56 changes: 56 additions & 0 deletions BOJ/1000-5000번/SB_1613.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class SB_1613 {
static int N, K;
static int[][] order;
static int INF = Integer.MAX_VALUE >> 2;

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

N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());

order = new int[N + 1][N + 1];
for (int i = 1; i < N + 1; i++) {
Arrays.fill(order[i], INF);
order[i][i] = 0;
}

for (int i = 0; i < K; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
order[a][b] = 1;
}

for (int k = 1; k < N + 1; k++) {
for (int i = 1; i < N + 1; i++) {
for (int j = 1; j < N + 1; j++) {
if (order[i][j] > order[i][k] + order[k][j]) {
order[i][j] = order[i][k] + order[k][j];
}
}
}
}

int S = Integer.parseInt(br.readLine());
while (S-- > 0) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

if (order[a][b] != INF) sb.append(-1);
else if (order[b][a] != INF) sb.append(1);
else sb.append(0);
sb.append('\n');
}

System.out.println(sb);
}
}
54 changes: 54 additions & 0 deletions BOJ/1000-5000번/SB_2096.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class SB_2096 {
static int N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

N = Integer.parseInt(br.readLine());
int[][] dpMx = new int[2][3]; // 이전값, 현재값
int[][] dpMn = new int[2][3];

st = new StringTokenizer(br.readLine()); // 초기 0행 값 설정
for (int i = 0; i < 3; i++) {
int x = Integer.parseInt(st.nextToken());
dpMx[0][i] = x;
dpMx[1][i] = x;
dpMn[0][i] = x;
dpMn[1][i] = x;
}

for (int i = 1; i < N; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());

dpMx[1][0] = Math.max(dpMx[0][0], dpMx[0][1]) + a;
dpMx[1][1] = Math.max(dpMx[0][0], Math.max(dpMx[0][1], dpMx[0][2])) + b;
dpMx[1][2] = Math.max(dpMx[0][1], dpMx[0][2]) + c;

dpMn[1][0] = Math.min(dpMn[0][0], dpMn[0][1]) + a;
dpMn[1][1] = Math.min(dpMn[0][0], Math.min(dpMn[0][1], dpMn[0][2])) + b;
dpMn[1][2] = Math.min(dpMn[0][1], dpMn[0][2]) + c;

// 이전값을 업데이트된 현재값 업데이트
dpMx[0][0] = dpMx[1][0];
dpMx[0][1] = dpMx[1][1];
dpMx[0][2] = dpMx[1][2];

dpMn[0][0] = dpMn[1][0];
dpMn[0][1] = dpMn[1][1];
dpMn[0][2] = dpMn[1][2];
}

int mx = Math.max(dpMx[0][0], Math.max(dpMx[0][1], dpMx[0][2]));
int mn = Math.min(dpMn[0][0], Math.min(dpMn[0][1], dpMn[0][2]));

System.out.println(mx + " " + mn);
}
}
125 changes: 125 additions & 0 deletions BOJ/1000-5000번/SB_2955.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SB_2955 {
static int[][] board = new int[9][9];
static boolean[][] row = new boolean[9][10];
static boolean[][] col = new boolean[9][10];
static boolean[][] box = new boolean[9][10];


private static boolean crossHatching(int num) {
boolean flag = false;
for (int i = 0; i < 9; i++) { // 가로줄 놓을 수 있는지 확인
int cr = -1, cc = -1, cnt = 0;
for (int j = 0; j < 9; j++) {
if (board[i][j] == 0 && !row[i][num] && !col[j][num] && !box[boxIdx(i, j)][num]) {
cr = i;
cc = j;
cnt++;
}
}
if (cnt == 1) {
placeNumber(cr, cc, num);
flag = true;
}
}

for (int j = 0; j < 9; j++) { // 세로줄 놓을 수 있는지 확인
int cr = -1, cc = -1, cnt = 0;
for (int i = 0; i < 9; i++) {
if (board[i][j] == 0 && !row[i][num] && !col[j][num] && !box[boxIdx(i, j)][num]) {
cr = i;
cc = j;
cnt++;
}
}
if (cnt == 1) {
placeNumber(cr, cc, num);
flag = true;
}
}

for (int idx = 0; idx < 9; idx++) { // 박스 놓을 수 있는지 확인
int cr = -1, cc = -1, cnt = 0;
int sr = (idx / 3) * 3, sc = (idx % 3) * 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int r = sr + i;
int c = sc + j;
if (board[r][c] == 0 && !row[r][num] && !col[c][num] && !box[idx][num]) {
cr = r;
cc = c;
cnt++;
}
}
}
if (cnt == 1) {
placeNumber(cr, cc, num);
flag = true;
}
}
return flag;
}

private static void placeNumber(int r, int c, int num) {
board[r][c] = num;
row[r][num] = true;
col[c][num] = true;
box[boxIdx(r, c)][num] = true;
}

private static int boxIdx(int i, int j) {
return (i / 3) * 3 + j / 3;
}

private static void printBoard() {
StringBuilder sb = new StringBuilder();
for (int[] row : board) {
for (int c : row) {
sb.append(c == 0 ? "." : c);
}
sb.append("\n");
}
System.out.print(sb);
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 9; i++) {
String line = br.readLine();
for (int j = 0; j < 9; j++) {
int num = line.charAt(j) == '.' ? 0 : line.charAt(j) - '0';
board[i][j] = num;
if (num != 0) {
if (row[i][num] || col[j][num] || box[boxIdx(i, j)][num]) { // 입력값이 잘못됐을 경우 에러
System.out.println("ERROR");
return;
}
row[i][num] = true;
col[j][num] = true;
box[boxIdx(i, j)][num] = true;
}
}
}

boolean doTry = false; // 첫 시도 체크 변수
boolean updated = true; // 숫자 적은거 있는지 체크
while (updated) {
updated = false;
for (int num = 1; num <= 9; num++) {
boolean res = crossHatching(num);
updated |= res;
doTry |= res;
}
}

if (!doTry){ // 아예 시도조차 못할 경우 에러
System.out.println("ERROR");
return;
}

printBoard();
}
}
159 changes: 159 additions & 0 deletions CodeTree/2019-2020년/SB_회전하는_빙하.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

public class SB_회전하는_빙하 {
static int N, Q, M;
static int[][] board;
static boolean[][] visited;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int area = 0;
static int mx = 0;
private static void bfs(int x, int y) {
Queue<Node> que = new ArrayDeque<>();
que.offer(new Node(x, y));
visited[x][y] = true;

int size = 0;
while (!que.isEmpty()) {
Node cur = que.poll();
area += board[cur.x][cur.y];
size++;

for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (!isValid(nx, ny) || visited[nx][ny] || board[nx][ny]==0) continue;
que.offer(new Node(nx, ny));
visited[nx][ny] = true;
}
}
mx = Math.max(mx, size);
}

private static void calIceGroup() {
visited = new boolean[M][M];

for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (!visited[i][j] && board[i][j] > 0) {
bfs(i, j);
}
}
}
}
private static void meltIce() {
boolean[][] check = new boolean[M][M];

// 녹을 빙하 체크
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (board[i][j]==0) continue; // 얼음이 아닌 곳은 패쓰
int cnt = 0;
for (int k = 0; k < 4; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if (isValid(nx, ny) && board[nx][ny]>0) cnt++;
}
if (cnt < 3) check[i][j] = true;
}
}

// 체크된 빙하 녹이기
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
if (check[i][j]) board[i][j]--;
}
}
}

private static boolean isValid(int x, int y) {
return 0<=x && x<M && 0<=y && y<M;
}
private static void rotateIce(int sx, int sy, int l, int[][] tmp) { // 레벨 사각형안의 분할된 사각형의 회전
int s = 1<<l;
int ss = 1 << (l - 1);

for (int i = sx; i < sx + s; i+=ss) { // 현재 레벨 사각형의 시작 꼭짓점부터 진행
for (int j = sy; j < sy + s; j+=ss) {
int ox = i - sx; // 0점 맞추기
int oy = j - sy;
int rx = oy; // 회전 좌표
int ry = s-ox-ss;
for (int x = 0; x < ss; x++) { // 분할된 사각형 조각 이동
for (int y = 0; y < ss; y++) {
int cx = i + x; // 원본 배열에서 현재 위치
int cy = j + y;
int nx = rx + x; // 회전된 좌표에서 새로운 위치
int ny = ry + y;
tmp[nx+sx][ny+sy] = board[cx][cy]; // 회전된 위치엔 보정으로 뺐던 시작값 더해주기
}
}
}
}
}
private static void rotate(int l) {
int s = 1<<l;
// tmp 배열 복사
int[][] tmp = new int[M][M];
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
tmp[i][j] = board[i][j];
}
}

// 큰 사각형 회전 (레벨 사각형)
for (int i = 0; i < M; i+=s) {
for (int j = 0; j < M; j+=s) {
rotateIce(i, j, l, tmp);
}
}

// 회전한 내용 반영
board = tmp;
}
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());
Q = Integer.parseInt(st.nextToken());

M = 1<<N;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 제곱근 연산을 Math.pow로 해서 계속 int로 형변환을 해줬는데요,,!!
수빈님은 제곱근 연산을 모두 비트 연산으로 하니, 형변환도 안 해도 되고 간단하게 표현할 수 있어서 좋은 것 같습니다!!

yeahdy and yeongleej reacted with thumbs up emoji baexxbin reacted with heart emoji
board = new int[M][M];

for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}

// 회전 수행
st = new StringTokenizer(br.readLine());
while (Q-- > 0) {
int L = Integer.parseInt(st.nextToken());
if (L > 0) rotate(L);
meltIce(); // 얼음 녹기
}

// 얼음군집 구하기
calIceGroup();

System.out.println(area);
System.out.println(mx);
}

static class Node{
int x, y;

public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
}
Loading

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