알고리즘/코테
[백준 1405] 미친 로봇
kiwiiiv
2022. 1. 7. 18:52
https://www.acmicpc.net/problem/1405
1405번: 미친 로봇
첫째 줄에 N, 동쪽으로 이동할 확률, 서쪽으로 이동할 확률, 남쪽으로 이동할 확률, 북쪽으로 이동할 확률이 주어진다. N은 14보다 작거나 같은 자연수이고, 모든 확률은 100보다 작거나 같은 자
www.acmicpc.net
대다수가 DFS로 푼 것 같고 나도 DFS 로 풀었음
신경썼던 점
- 이동 확률 고려
확률이 각기 다를 수 있음 -> 이동 시마다 계산하면서 탐색해야 함. 한꺼번에 계산 절대 ㄴ
- out of bounds
isVisited의 배열 부분에서 해당 에러가 나지 않게 조심해야 함
-> 배열의 크기를 넉넉하게 30*30 으로 정의한 후, count(이동 횟수)를 통하여 에러가 일어나지 않도록 함
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static double result = 1.0;
static int direction[] = new int[4+1];
static double N;
static boolean isVisited[][] = new boolean[30][30];
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input[] = br.readLine().split(" ");
N = Integer.valueOf(input[0]); //이동 횟수
for (int i = 0; i < 4; i++) {
direction[i] = Integer.valueOf(input[i + 1]);
}
direction[4] = 100; //초기값 ..
tracking(0, 0, 0, 4, 1); //(0,0)
System.out.println(result);
}
private static void tracking(int x, int y, int count, int d, double per) {
per *= (double) (direction[d] / 100.0);
if (per == 0) return;
if (isVisited[x + 14][y + 14]) { //notSimple
result -= per;
return;
}
if (count == N) return;
isVisited[x + 14][y + 14] = true;
//E
tracking(x + 1, y, count + 1, 0, per);
//W
tracking(x - 1, y, count + 1, 1, per);
//S
tracking(x, y - 1, count + 1, 2, per);
//N
tracking(x, y + 1, count + 1, 3, per);
isVisited[x + 14][y + 14] = false;
}
}