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 970501f

Browse files
authored
Merge pull request #140 from yeahdy/main
[10์ฃผ์ฐจ] ์ด์˜ˆ์ง„
2 parents 6fb85b3 + 753537d commit 970501f

9 files changed

+589
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class YJ_1613 {
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 k = Integer.parseInt(st.nextToken());
10+
11+
//graph ๋ชจ๋ธ๋ง
12+
boolean[][] graph = new boolean[n+1][n+1];
13+
for(int i=0; i<k; i++){
14+
st = new StringTokenizer(br.readLine());
15+
int to = Integer.parseInt(st.nextToken());
16+
int from = Integer.parseInt(st.nextToken());
17+
graph[to][from] = true;
18+
}
19+
//ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์ถ”๊ฐ€ ์ค‘๊ฐ„๊ฒฝ๋กœ ์ฐพ๊ธฐ
20+
for(int d=1; d<=n; d++){
21+
for(int i=1; i<=n; i++){
22+
for(int j=1; j<=n; j++){
23+
if(graph[i][d] && graph[d][j]){ //๊ฒฝ์œ ๊ฐ€ ๊ฐ€๋Šฅํ•  ๊ฒฝ์šฐ i > d > j ์‚ฌ๊ฑด์œผ๋กœ ์—ฐ๊ฒฐ๋จ
24+
graph[i][j] = true;
25+
}
26+
}
27+
}
28+
}
29+
//์ „ํ›„๊ด€๊ณ„ ์ฐพ๊ธฐ
30+
int s = Integer.parseInt(br.readLine());
31+
while(s-- > 0){
32+
int result = 0;
33+
st = new StringTokenizer(br.readLine());
34+
int before = Integer.parseInt(st.nextToken());
35+
int after = Integer.parseInt(st.nextToken());
36+
37+
if(before > n || after > n){
38+
System.out.println(result);
39+
continue;
40+
}
41+
42+
if(graph[before][after]){
43+
result = -1; //์•ž์— ์žˆ๋Š” ๋ฒˆํ˜ธ์˜ ์‚ฌ๊ฑด์ด ๋จผ์ € ์ผ์–ด๋‚ฌ์œผ๋ฉด -1
44+
}else if(graph[after][before]){
45+
result = 1; //๋’ค์— ์žˆ๋Š” ๋ฒˆํ˜ธ์˜ ์‚ฌ๊ฑด์ด ๋จผ์ € ์ผ์–ด๋‚ฌ์œผ๋ฉด 1
46+
}
47+
System.out.println(result);
48+
}
49+
50+
}
51+
52+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class YJ_2096 {
5+
public static void main(String[] args) throws IOException {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
8+
final int LIMIT = 3;
9+
int n = Integer.parseInt(br.readLine());
10+
int[][] board = new int[n][3];
11+
int[][] min = new int[n][3];
12+
int[][] max = new int[n][3];
13+
//์ž…๋ ฅ๊ฐ’ ์ดˆ๊ธฐํ™”
14+
for(int i=0; i<n; i++){
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
for(int j=0; j<LIMIT; j++){
17+
board[i][j] = Integer.parseInt(st.nextToken());
18+
}
19+
}
20+
21+
//์ตœ์†Œ,์ตœ๋Œ€ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
22+
for(int[] m :min){
23+
Arrays.fill(m, 999_999);
24+
}
25+
min[0] = board[0];
26+
max[0] = board[0];
27+
28+
//dp
29+
for(int i=0; i<n-1; i++){
30+
for(int j=0; j<LIMIT; j++){
31+
if(j-1 >= 0){ //์™ผ์ชฝ์‚ฌ์ด๋“œ
32+
min[i+1][j-1] = Math.min(min[i+1][j-1] , min[i][j] + board[i+1][j-1]);
33+
max[i+1][j-1] = Math.max(max[i+1][j-1] , max[i][j] + board[i+1][j-1]);
34+
}
35+
if(j+1 < LIMIT){ //์˜ค๋ฅธ์ชฝ์‚ฌ์ด๋“œ
36+
min[i+1][j+1] = Math.min(min[i+1][j+1] , min[i][j] + board[i+1][j+1]);
37+
max[i+1][j+1] = Math.max(max[i+1][j+1] , max[i][j] + board[i+1][j+1]);
38+
}
39+
//๋ฐ”๋กœ์•„๋ž˜
40+
min[i+1][j] = Math.min(min[i+1][j] , min[i][j] + board[i+1][j]);
41+
max[i+1][j] = Math.max(max[i+1][j] , max[i][j] + board[i+1][j]);
42+
}
43+
}
44+
45+
//์ตœ๋Œ€์ ์ˆ˜
46+
Arrays.sort(max[n-1]);
47+
//์ตœ์†Œ์ ์ˆ˜
48+
Arrays.sort(min[n-1]);
49+
System.out.printf("%d %d",max[n-1][LIMIT-1],min[n-1][0]);
50+
}
51+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import java.io.*;
2+
3+
public class YJ_2955 {
4+
static final int NUM = 9;
5+
static final String ERROR = "ERROR";
6+
7+
static int[][] board;
8+
static boolean error = false;
9+
10+
public static void main(String[] args) throws IOException{
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
board = new int[NUM][NUM];
13+
14+
for(int i=0; i<NUM; i++){
15+
String[] data = br.readLine().split("");
16+
for(int j=0; j<NUM; j++){
17+
board[i][j] = ".".equals(data[j])? 0 : Integer.parseInt(data[j]);
18+
}
19+
}
20+
21+
//cross-hatching
22+
boolean flag;
23+
do {
24+
flag = false;
25+
for (int target = 1; target <= NUM; target++) {
26+
if (crossHatching(target)) {
27+
flag = true;
28+
}
29+
}
30+
} while (flag);
31+
32+
//๊ฒฐ๊ณผ ์ถœ๋ ฅ
33+
if(error){
34+
System.out.println(ERROR);
35+
}else{
36+
for(int[] b : board){
37+
for (int j : b) {
38+
System.out.print(j == 0? "." : j);
39+
}
40+
System.out.println();
41+
}
42+
}
43+
}
44+
45+
static boolean[] rowCheck;
46+
static boolean[] colCheck;
47+
static boolean[][] gridCheck;
48+
static boolean crossHatching (int target){
49+
rowCheck = new boolean[NUM];
50+
colCheck = new boolean[NUM];
51+
gridCheck = new boolean[3][3];
52+
53+
for(int i=0; i<NUM; i++) {
54+
for (int j=0; j < NUM; j++) {
55+
//ํŠน์ • ์ˆซ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋ฐฐ์—ด์˜ ๊ฐ€๋กœ,์„ธ๋กœ์ค„ ๋ชจ๋‘ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
56+
if(target == board[i][j]){
57+
if (rowCheck[i] || colCheck[j] || gridCheck[i / 3][j / 3]) {
58+
error = true;
59+
return false;
60+
}
61+
rowCheck[i] = true;
62+
colCheck[j] = true;
63+
gridCheck[i / 3][j / 3] = true;
64+
}
65+
}
66+
}
67+
return findSpace(target);
68+
}
69+
70+
static boolean findSpace(int target){
71+
boolean flag = false;
72+
73+
for(int i=0; i<3; i++){
74+
for(int j=0; j<3; j++){
75+
if(gridCheck[i][j]){
76+
continue;
77+
}
78+
if (findInGrid(i*3, j*3, target)) { //๊ฐ 3*3 ๋ฐ•์Šค์˜ ์™ผ์ชฝ๋ชจ์„œ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‹œ์ž‘
79+
flag = true;
80+
}
81+
}
82+
}
83+
//์ž๋ฆฌ๊ฐ€ ๋„ˆ๋ฌด๋งŽ์€ ๊ฒฝ์šฐ, ํƒ€๊ฒŸ ์ˆซ์ž๊ฐ€ ์–ด๋–ค ๊ฐ€๋กœ์ค„, 3*3๋ฐ•์Šค, ์„ธ๋กœ์ค„์— ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ๊ณณ์ด ์ „ํ˜€ ์—†๋Š” ๊ฒฝ์šฐ
84+
return flag;
85+
}
86+
87+
private static boolean findInGrid(int sr, int sc, int target) {
88+
int nr = -1;
89+
int nc = -1;
90+
int cnt = 0;
91+
//sr,sc 3*3 ๋ฐ•์Šค ์™ผ์ชฝ๊ผญ์ง€์  ์‹œ์ž‘์ ์„ ๊ธฐ์ค€์œผ๋กœ ์„œ๋ธŒ๊ทธ๋ฆฌ๋“œ ์ˆœํšŒ
92+
for (int i = 0; i < 3; i++) {
93+
for (int j = 0; j < 3; j++) {
94+
if(rowCheck[i + sr] || colCheck[j + sc]){
95+
continue;
96+
}
97+
if(board[i + sr][j + sc] == 0){
98+
if (cnt == 0) {
99+
nr = i + sr;
100+
nc = j + sc;
101+
cnt++;
102+
} else { //์ž๋ฆฌ๊ฐ€ ๋„ˆ๋ฌด๋งŽ์€ ๊ฒฝ์šฐ
103+
return false;
104+
}
105+
}
106+
}
107+
}
108+
//ํƒ€๊ฒŸ ์ˆซ์ž๊ฐ€ ์–ด๋–ค ๊ฐ€๋กœ์ค„, 3*3๋ฐ•์Šค, ์„ธ๋กœ์ค„์— ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ๊ณณ์ด ์ „ํ˜€ ์—†๋Š” ๊ฒฝ์šฐ
109+
if (cnt == 0) {
110+
error = true;
111+
return false;
112+
}
113+
//์Šค๋„์ฟ  ์ถ”๊ฐ€ ๋ฐ ๋ฐฉ๋ฌธ์ฒดํฌ
114+
board[nr][nc] = target;
115+
rowCheck[nr] = true;
116+
colCheck[nc] = true;
117+
118+
return true;
119+
}
120+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
//๊ตฌํ˜„ + BFS
5+
public class YJ_ํšŒ์ „ํ•˜๋Š”๋น™ํ•˜ {
6+
static int[][] glacier;
7+
static int[] levels;
8+
static int size = 0;
9+
static Deque<int[]> deque = new ArrayDeque<>();
10+
11+
public static void main(String[] args) throws IOException{
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st;
14+
String[] data = br.readLine().split("\\s");
15+
int n = Integer.parseInt(data[0]);
16+
int q = Integer.parseInt(data[1]);
17+
18+
size = (int) Math.pow(2,n);
19+
glacier = new int[size][size];
20+
for(int i=0; i<size; i++){
21+
st = new StringTokenizer(br.readLine());
22+
for(int j=0; j<size; j++){
23+
glacier[i][j] = Integer.parseInt(st.nextToken());
24+
}
25+
}
26+
27+
levels = new int[q];
28+
st = new StringTokenizer(br.readLine());
29+
for(int i=0; i<q; i++){
30+
levels[i] = Integer.parseInt(st.nextToken());
31+
}
32+
33+
34+
for(int l : levels){
35+
//์–ผ์Œ ํšŒ์ „ํ•˜๊ธฐ
36+
if(l > 0){
37+
rotate(l);
38+
}
39+
//์–ผ์Œ ๋…น์ด๊ธฐ
40+
meltingGlaciers();
41+
}
42+
43+
//BFS ํƒ์ƒ‰: ํฐ ์–ผ์Œ ๊ตฐ์ง‘ ํฌ๊ธฐ ๊ตฌํ•˜๊ธฐ
44+
int total = 0;
45+
int iceSet = 0;
46+
boolean[][] visited = new boolean[size][size];
47+
for(int i=0; i<size; i++){
48+
for(int j=0; j<size; j++){
49+
if(glacier[i][j] < 1 || visited[i][j]){ //โ˜…์–ผ์Œ์ด 0 ์ด๊ฑฐ๋‚˜ ๋ฐฉ๋ฌธํ–ˆ์„ ๊ฒฝ์šฐ ์–ผ์Œ ์ง‘ํ•ฉX
50+
continue;
51+
}
52+
//โ˜…๊ฐ€๋Šฅํ•œ ์ง‘ํ•ฉ ์ดˆ๊ธฐํ™”
53+
deque.offer(new int[]{i,j});
54+
visited[i][j] = true;
55+
total += glacier[i][j];
56+
57+
int[] results = bfs(visited);
58+
total += results[0];
59+
iceSet = Math.max(iceSet,results[1]);
60+
}
61+
}
62+
System.out.printf("%d%n%d",total,iceSet);
63+
}
64+
65+
static int[] nx = {0,1,0,-1};
66+
static int[] ny = {1,0,-1,0};
67+
static int[] bfs(boolean[][] visited){
68+
int total = 0;
69+
int set = 0;
70+
71+
while(!deque.isEmpty()){
72+
int[] pos = deque.poll();
73+
set++;
74+
for(int i=0; i<4; i++){
75+
int x = pos[0] + nx[i];
76+
int y = pos[1] + ny[i];
77+
78+
if(stop(x,y) || visited[x][y]){
79+
continue;
80+
}
81+
82+
deque.offer(new int[]{x,y});
83+
visited[x][y] = true;
84+
total += glacier[x][y];
85+
}
86+
}
87+
return new int[]{total,set};
88+
}
89+
90+
91+
static int[][] temp;
92+
static void rotate(int level){
93+
temp = new int[size][size];
94+
int range = (int) Math.pow(2,level);
95+
int levelSize = (int) Math.pow(2,level-1); //2^Lโˆ’1* 2^Lโˆ’1 ๋งŒํผ ์ž˜๋ผ 4๋“ฑ๋ถ„
96+
97+
for(int i=0; i<size; i+=range){
98+
for(int j=0; j<size; j+=range){
99+
//โ˜…4๋“ฑ๋ถ„(๋‚˜๋ˆด์„ ๋•Œ ํšŒ์ „์„ ์œ„ํ•ด ๊ฐ ์™ผ์ชฝ์œ— ๋ชจ์„œ๋ฆฌ๋ฅผ ๊ธฐ์ค€์ ์œผ๋กœ ์žก์Œ)
100+
move(i,j ,levelSize,0); //์™ผ์ชฝ์œ„ > ์˜ค๋ฅธ์ชฝ์œ„์œผ๋กœ ์ด๋™
101+
move(i,j+levelSize ,levelSize,1); //์˜ค๋ฅธ์ชฝ์œ„ > ์˜ค๋ฅธ์ชฝ์•„๋ž˜๋กœ ์ด๋™
102+
move(i+levelSize ,j+levelSize ,levelSize,2); //์˜ค๋ฅธ์ชฝ์•„๋ž˜ > ์™ผ์ชฝ์•„๋ž˜
103+
move(i+levelSize ,j ,levelSize,3); //์™ผ์ชฝ์•„๋ž˜ > ์™ผ์ชฝ์œ„๋กœ ์ด๋™
104+
}
105+
}
106+
glacier = temp; //โ˜…์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ๋ณต์‚ฌ
107+
}
108+
109+
110+
private static void move(int row, int col, int levelSize, int direction){
111+
//๋‚˜๋‰œ ๊ตฌ๊ฐ„ ์•ˆ์—์„œ ๋ ˆ๋ฒจ๋งŒํผ ํšŒ์ „(1๋ ˆ๋ฒจ:1๋ฒˆ, 2๋ ˆ๋ฒจ:4๋ฒˆ, 3๋ ˆ๋ฒจ:8๋ฒˆ)
112+
for(int x=row; x<row+levelSize; x++){
113+
for(int y=col; y<col+levelSize; y++){
114+
int nextX = x + nx[direction] * levelSize; //๋ ˆ๋ฒจ์˜ ๊ฐ„๊ฒฉ๋งŒํผ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•ด levelSize ๊ณฑํ•˜๊ธฐ
115+
int nextY = y + ny[direction] * levelSize;
116+
117+
temp[nextX][nextY] = glacier[x][y];
118+
System.out.printf("glacier[%d][%d] = %d > ",x,y,glacier[x][y]);
119+
System.out.printf("temp[%d][%d] = %d%n",nextX,nextY,temp[nextX][nextY]);
120+
}
121+
}
122+
}
123+
124+
static void meltingGlaciers(){
125+
temp = new int[size][size];
126+
//๋…น๋Š” ๋น™ํ•˜ ๊ณ„์‚ฐํ•˜๊ธฐ
127+
for(int i=0; i<size; i++){
128+
for(int j=0; j<size; j++){
129+
if(canMelt(i,j)){
130+
temp[i][j]++;
131+
}
132+
}
133+
}
134+
//๋น™ํ•˜ ๋…น์ด๊ธฐ
135+
for(int i=0; i<size; i++){
136+
for(int j=0; j<size; j++){
137+
glacier[i][j] -= temp[i][j];
138+
}
139+
}
140+
}
141+
142+
//์ƒํ•˜์ขŒ์šฐ ์ธ์ ‘ํ•œ ์นธ์— ์–ผ์Œ์ด 3๊ฐœ ๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ ์–ผ์Œ ๋…น์Œ
143+
static final int LIMIT = 3;
144+
private static boolean canMelt(int x, int y){
145+
int count = 4;
146+
for(int i=0; i<4; i++){
147+
int currentX = x + nx[i];
148+
int currentY = y + ny[i];
149+
150+
if(stop(currentX,currentY)){
151+
count--;
152+
}
153+
}
154+
return count < LIMIT;
155+
}
156+
157+
private static boolean stop(int x, int y){
158+
return x < 0 || y < 0 || x >= size || y >= size || glacier[x][y] <= 0;
159+
}
160+
161+
}

0 commit comments

Comments
(0)

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /