이취코테 118p 게임개발

이동한·2023년 4월 19일
0

Algorithm

목록 보기
1/12
import java.io.*;
import java.util.StringTokenizer;

class Main {
  static int[][] dp,map;
  static int dir,x,y;
  static int[] dx={-1,0,1,0},dy={0,-1,0,1};
  
  public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());

    int N = Integer.parseInt(st.nextToken()),M = Integer.parseInt(st.nextToken());
    dp = new int[N][M];
    map = new int[N][M];
    st = new StringTokenizer(br.readLine());
    
    // 초기위치 x,y ,초기 방향 dir 초기화
    x = Integer.parseInt(st.nextToken()); y = Integer.parseInt(st.nextToken());
    dir = Integer.parseInt(st.nextToken());
    dp[x][y] = 1;
    // map초기화
    for (int i=0; i<N; i++){
      st = new StringTokenizer(br.readLine());
      for(int j=0;j<M; j++){
        int cur = Integer.parseInt(st.nextToken());
        if(cur == 1) map[i][j] =1;
      }
    }
    int res=1,turnCnt=0;
    while(true){
      turnLeft();
      int nx = x+dx[dir], ny = y+dy[dir];
      if(dp[nx][ny] == 0 && map[nx][ny]==0){
        //방문 안하고, 육지인 경우(map[nx][ny] == 0)
        dp[nx][ny] =1;
        res+=1;
        x = nx; y= ny;
        turnCnt=0;
        continue;
      }else{
        //방문 할 수없는 경우 회전수 증가
        turnCnt+=1;
      }
      
      if(turnCnt == 4){
        nx = x-dx[dir]; ny = y-dy[dir];
        if (map[nx][ny] ==1) break;
        x = nx; y = ny;
        turnCnt=0;
      }
    }
    System.out.println(res);
    br.close();
  }
  public static void turnLeft(){
    if(--dir == -1) dir=3;
    return;
  }
}
profile
Pragmatic, Productive, Positivist

0개의 댓글