코드 1, 성공, 230814
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
static int[][] value;
static int[][] stone;
static boolean checkBoundary(int x, int y){
if(0 <= x && x<stone.length && 0 <= y && y < stone[0].length) {
return true;
}
return false;
}
static void changeBlock(int x, int y) {
if(checkBoundary(x, y)){
value[x][y] = 15;
}
}
static void findMax(int x, int y){
int[][] dist = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {0, 0}};
for(int i = 0; i<dist.length; i++){
if (checkBoundary(x+dist[i][0], y+dist[i][1])) {
if(value[x][y] -1 > value[x+dist[i][0]][y+dist[i][1]]){
value[x+dist[i][0]][y+dist[i][1]] = value[x][y] -1;
}
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] arr = br.readLine().split(" ");
int x = Integer.parseInt(arr[0]);
int y = Integer.parseInt(arr[1]);
stone = new int[x][y];
value = new int[x][y];
int repeat = Integer.parseInt(br.readLine());
ArrayList<int[]> lampList = new ArrayList<int[]>();
for(int i = 0; i < repeat; i++){
String[] str = br.readLine().split(" ");
if (str[0].equals("redstone_dust")) {
stone[Integer.parseInt(str[1])][Integer.parseInt(str[2])] = 1;
} else if(str[0].equals("redstone_block")){
stone[Integer.parseInt(str[1])][Integer.parseInt(str[2])] = 2;
} else {
stone[Integer.parseInt(str[1])][Integer.parseInt(str[2])] = 3;
lampList.add(new int[]{Integer.parseInt(str[1]), Integer.parseInt(str[2])});
}
}
for(int i = 0; i < stone.length; i++){
for(int j = 0; j < stone[i].length; j++){
if(stone[i][j] == 2) {
changeBlock(i+1, j);
changeBlock(i, j+1);
changeBlock(i-1, j);
changeBlock(i, j-1);
}
}
}
for(int i = 0; i < stone.length; i++){
for(int j = 0; j < stone[i].length; j++){
if(stone[i][j] == 1){
findMax(i, j);
}
}
}
for(int i = 0; i<lampList.size(); i++){
if(value[lampList.get(i)[0]][lampList.get(i)[1]] <= 0){
System.out.println("failed");
return;
}
}
System.out.println("success");
}
}
0 : 아무것도 없음
1 : dust
2 : block
3 : lamp
2차원 점수 배열 생성
block => 상, 하, 좌, 우 15로 교체
dust => dust 수와 상, 하, 좌, 우 비교하며 더 큰 수로 상, 하, 좌, 우 교체
처음 백트래킹으로 풀려했는데 종료 조건에서 어떻게 핸들링할지 몰라 2차원 점수 배열 생성하는 방향으로 변경했다
처음에 dust가 dust 기준 상, 하, 좌, 우의 값을 전달한다고 이해해 진행하는데 52퍼에서 끊겼다
- dust 기준 상, 하, 좌, 우가 아닌 dust 자체의 값을 상, 하, 좌, 우로 전달하는 것이 맞는 논리
'Algorithm' 카테고리의 다른 글
[백준] 1790 (0) | 2023.08.29 |
---|---|
[백준] 22238 (0) | 2023.08.29 |
[백준] 14503 (0) | 2023.08.11 |
[백준] 15686 (0) | 2023.08.11 |
[백준] 1389 (0) | 2023.08.09 |
댓글