Skip to content

Commit 700c085

Browse files
authored
28주차 (#24)
* 28주차 * 28주차
1 parent 13a0a52 commit 700c085

File tree

6 files changed

+422
-1
lines changed

6 files changed

+422
-1
lines changed

.idea/misc.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.example._28week;
2+
3+
import java.util.Arrays;
4+
5+
public class IntegerTriangle {
6+
7+
public static void main(String[] args) {
8+
int[][] triangle = {{7}, {3, 8}, {8, 1, 0}, {2, 7, 4, 4}, {4, 5, 2, 6, 5}};
9+
10+
int result = solution(triangle);
11+
System.out.println(result);
12+
}
13+
14+
private static int solution(int[][] triangle) {
15+
int rowSize = triangle.length;
16+
17+
for (int row = 1; row < rowSize; row++) {
18+
19+
for (int col = 0; col <= row; col++) {
20+
int left = col > 0 ? triangle[row - 1][col - 1] : 0;
21+
int right = row == col ? 0 : triangle[row - 1][col];
22+
23+
triangle[row][col] += Math.max(left, right);
24+
}
25+
}
26+
27+
return Arrays.stream(triangle[rowSize - 1]).max().getAsInt();
28+
}
29+
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.example._28week;
2+
3+
public class OnTheWaySchool {
4+
5+
private static final int PUDDLE = -1;
6+
7+
public static void main(String[] args) {
8+
9+
int m = 4;
10+
int n = 3;
11+
int[][] puddles = {{2, 2}};
12+
13+
int result = solution(m, n, puddles);
14+
System.out.println(result);
15+
}
16+
17+
public static int solution(int m, int n, int[][] puddles) {
18+
int[][] map = new int[m + 1][n + 1];
19+
20+
for (int[] puddle : puddles) {
21+
int row = puddle[0];
22+
int col = puddle[1];
23+
24+
map[row][col] = PUDDLE;
25+
}
26+
27+
map[1][1] = 1;
28+
29+
for (int row = 1; row <= m; row++) {
30+
for (int col = 1; col <= n; col++) {
31+
if (row == 1 && col == 1) {
32+
continue;
33+
}
34+
35+
int left;
36+
int top;
37+
38+
if (map[row][col] == PUDDLE) {
39+
continue;
40+
}
41+
42+
if (row <= 0 || row > m || col - 1 <= 0 || col - 1 > n || map[row][col - 1] == PUDDLE) {
43+
left = 0;
44+
} else {
45+
left = map[row][col - 1] % 1_000_000_007;
46+
}
47+
48+
if (row - 1 <= 0 || row - 1 > m || col <= 0 || col > n || map[row - 1][col] == PUDDLE) {
49+
top = 0;
50+
} else {
51+
top = map[row - 1][col] % 1_000_000_007;
52+
}
53+
54+
55+
map[row][col] = (left + top) % 1_000_000_007;
56+
}
57+
}
58+
59+
return map[m][n];
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.example._28week;
2+
3+
public class OnTheWaySchool2 {
4+
5+
private static final int PUDDLE = -1;
6+
7+
public static void main(String[] args) {
8+
9+
int m = 4;
10+
int n = 3;
11+
int[][] puddles = {{2, 2}};
12+
13+
int result = solution(m, n, puddles);
14+
System.out.println(result);
15+
}
16+
17+
public static int solution(int m, int n, int[][] puddles) {
18+
int[][] map = new int[m + 1][n + 1];
19+
20+
for (int[] puddle : puddles) {
21+
int row = puddle[0];
22+
int col = puddle[1];
23+
24+
map[row][col] = PUDDLE;
25+
}
26+
27+
map[1][1] = 1;
28+
29+
for (int row = 1; row <= m; row++) {
30+
for (int col = 1; col <= n; col++) {
31+
if (row == 1 && col == 1) {
32+
continue;
33+
}
34+
35+
if (map[row][col] == PUDDLE) {
36+
continue;
37+
}
38+
39+
int left = map[row][col - 1] == PUDDLE ? 0 : map[row][col - 1] % 1_000_000_007;
40+
int top = map[row - 1][col] == PUDDLE ? 0 : map[row - 1][col] % 1_000_000_007;
41+
42+
map[row][col] = (left + top) % 1_000_000_007;
43+
}
44+
}
45+
46+
return map[m][n];
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package org.example._28week;
2+
3+
import java.io.*;
4+
import java.util.Arrays;
5+
import java.util.StringTokenizer;
6+
7+
public class RollingDice {
8+
9+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
12+
private static final int RIGHT = 1;
13+
private static final int LEFT = 2;
14+
private static final int UP = 3;
15+
private static final int DOWN = 4;
16+
17+
private static final int[] dr = {0, 0, 0, -1, 1};
18+
private static final int[] dc = {0, 1, -1, 0, 0};
19+
20+
public static void main(String[] args) throws IOException {
21+
StringTokenizer st = new StringTokenizer(br.readLine());
22+
int rowSize = Integer.parseInt(st.nextToken());
23+
int colSize = Integer.parseInt(st.nextToken());
24+
int initialRow = Integer.parseInt(st.nextToken());
25+
int initailCol = Integer.parseInt(st.nextToken());
26+
int commandCount = Integer.parseInt(st.nextToken());
27+
28+
Dice dice = new Dice(initialRow, initailCol);
29+
30+
int[][] map = new int[rowSize][colSize];
31+
for (int i = 0; i < rowSize; i++) {
32+
map[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
33+
}
34+
35+
int[] commands = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
36+
37+
// printState(map, dice);
38+
39+
for (int i = 0; i < commands.length; i++) {
40+
int curRow = dice.row;
41+
int curCol = dice.col;
42+
int direction = commands[i];
43+
44+
int nextRow = curRow + dr[direction];
45+
int nextCol = curCol + dc[direction];
46+
47+
if (nextRow < 0 || nextRow >= rowSize || nextCol < 0 || nextCol >= colSize) {
48+
continue;
49+
}
50+
51+
dice.roll(direction);
52+
dice.move(nextRow, nextCol);
53+
54+
if (map[nextRow][nextCol] == 0) {
55+
int bottomNumber = dice.getBottomNumber();
56+
map[nextRow][nextCol] = bottomNumber;
57+
} else {
58+
dice.setBottom(map[nextRow][nextCol]);
59+
map[nextRow][nextCol] = 0;
60+
}
61+
62+
// printState(map, dice);
63+
bw.write(dice.getTopNumber() + "\n");
64+
}
65+
66+
bw.flush();
67+
}
68+
69+
private static void printState(int[][] map, Dice dice) {
70+
System.out.println("------------ 현재 상태 ------------");
71+
printMap(map);
72+
printDice(dice);
73+
}
74+
75+
private static void printMap(int[][] map) {
76+
for (int i = 0; i < map.length; i++) {
77+
for (int j = 0; j < map[0].length; j++) {
78+
System.out.print(map[i][j] + " ");
79+
}
80+
System.out.println();
81+
}
82+
}
83+
84+
private static void printDice(Dice dice) {
85+
System.out.println("현재 위치");
86+
System.out.println(dice.row + ", " + dice.col);
87+
88+
System.out.println("전개도");
89+
System.out.println(" "+dice.numbers[2]+" ");
90+
System.out.println(dice.numbers[4]+" "+dice.numbers[1]+" "+dice.numbers[3]);
91+
System.out.println(" "+dice.numbers[5]+" ");
92+
System.out.println(" "+dice.numbers[6]+" ");
93+
}
94+
95+
private static class Dice {
96+
int row;
97+
int col;
98+
int[] numbers;
99+
100+
public Dice(int row, int col) {
101+
this.row = row;
102+
this.col = col;
103+
this.numbers = new int[7];
104+
}
105+
106+
public int getTopNumber() {
107+
return numbers[1];
108+
}
109+
110+
public int getBottomNumber() {
111+
return numbers[6];
112+
}
113+
114+
public void setBottom(int num) {
115+
numbers[6] = num;
116+
}
117+
118+
public void move(int row, int col) {
119+
this.row = row;
120+
this.col = col;
121+
}
122+
123+
public void roll(int direction) {
124+
switch (direction) {
125+
case RIGHT:
126+
// System.out.println("RIGHT");
127+
rightRoll();
128+
break;
129+
case LEFT:
130+
// System.out.println("LEFT");
131+
leftRoll();
132+
break;
133+
case UP:
134+
// System.out.println("UP");
135+
upRoll();
136+
break;
137+
case DOWN:
138+
// System.out.println("DOWN");
139+
downRoll();
140+
break;
141+
default:
142+
throw new RuntimeException("망했어요~");
143+
}
144+
145+
146+
}
147+
148+
private void rightRoll() {
149+
int number1 = numbers[1];
150+
int number3 = numbers[3];
151+
int number4 = numbers[4];
152+
int number6 = numbers[6];
153+
numbers[1] = number4;
154+
numbers[3] = number1;
155+
numbers[4] = number6;
156+
numbers[6] = number3;
157+
}
158+
159+
private void leftRoll() {
160+
int number1 = numbers[1];
161+
int number3 = numbers[3];
162+
int number4 = numbers[4];
163+
int number6 = numbers[6];
164+
165+
numbers[1] = number3;
166+
numbers[3] = number6;
167+
numbers[4] = number1;
168+
numbers[6] = number4;
169+
}
170+
171+
private void upRoll() {
172+
int number1 = numbers[1];
173+
int number2 = numbers[2];
174+
int number5 = numbers[5];
175+
int number6 = numbers[6];
176+
177+
numbers[1] = number5;
178+
numbers[2] = number1;
179+
numbers[5] = number6;
180+
numbers[6] = number2;
181+
}
182+
183+
private void downRoll() {
184+
int number1 = numbers[1];
185+
int number2 = numbers[2];
186+
int number5 = numbers[5];
187+
int number6 = numbers[6];
188+
189+
numbers[1] = number2;
190+
numbers[2] = number6;
191+
numbers[5] = number1;
192+
numbers[6] = number5;
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)