알고리즘

백준 17837 JAVA

infobox503 2024. 7. 15. 12:30

문제 유형

  • 시뮬레이션

문제 접근

    • 말이 4개 이상 겹칠 때까지 걸리는 시간
  • 주어지는 정보
    • 말 개수 10개
    • 말을 움직이는 최대 회수 1000
    ⇒ 시뮬레이션 가능

주의점

  • List<A>list
  • list에서 꺼낸 객체 a
  • ⇒ 이 2개는 모두 같은 장소를 고유함
  • (따라서, 값이 바뀌면 서로에게 다 적용됨)
import java.util.*;


class A
{
    public int a = 10;
}

public class HelloWorld {
    public static void main(String[] args) {
        List<A>list = new ArrayList<>();
        list.add(new A());
        
        A a = list.get(0);
        
        a.a = 99;
        
        System.out.println(list.get(0).a);
    }
}

 

 

 

코드

import java.util.*;
import java.io.*;

class Chess
{
    public int y,x,way;
    
    Chess(int y, int x, int way)
    {
        this.y = y;
        this.x = x;
        this.way = way;
    }
}

public class Main {
    public static int N,K;
    public static int[][] ColorMap;
    public static int[][][] ChessMap;
    public static int[][] move = {{0,0},{0,1},{0,-1},{-1,0},{1,0}};
    public static List<Chess> ChessList = new ArrayList<>();
    
    public static int FindChessHeight(Chess chess, int ChessOrder)
    {
        int Height = 0;
        for(int j = 0; j<4; j++)
        {
            if(ChessMap[chess.y][chess.x][j] == ChessOrder)
            {
                Height = j;
                break;
            }
        }
        
        return Height;
    }
    
    public static int FindChessBlank(int y, int x)
    {
        int Height = 0;
        for(int j = 0; j<4; j++)
        {
            if(ChessMap[y][x][j] == 0)
            {
                Height = j;
                break;
            }
        }
        return Height;
    }
    
    public static boolean Move()
    {
        for(int i = 0; i<K; i++)
        {
            Chess chess = ChessList.get(i);
            int ChessOrder = i+1;
            int MoveY = chess.y + move[chess.way][0];
            int MoveX = chess.x + move[chess.way][1];
            
            int CurHeight = 0;
            CurHeight = FindChessHeight(chess, ChessOrder);
            
            
            //wall
            if(MoveY < 0 || MoveY >= N || MoveX < 0 || MoveX >= N || ColorMap[MoveY][MoveX] == 2)
            {
                if(chess.way == 1 || chess.way == 3)
                {
                    chess.way = chess.way+1;
                }
                
                else
                {
                    chess.way = chess.way-1;
                }
                
                int ny = chess.y + move[chess.way][0];
                int nx = chess.x + move[chess.way][1];
                
                if(ny < 0 || ny >= N || nx < 0 ||nx >=N || ColorMap[ny][nx] == 2)
                {
                    continue;
                }
                
                
                MoveY = ny;
                MoveX = nx;
            }
            
            
            //white and red
            if(ColorMap[MoveY][MoveX] == 0 || ColorMap[MoveY][MoveX] == 1)
            {
                int CurChessIndex = i; 
                Chess CurChess = ChessList.get(CurChessIndex);
                int CurY = CurChess.y;
                int CurX = CurChess.x;
                
                
                int NextHeight = 0;
                NextHeight = FindChessBlank(MoveY,MoveX);
                
                //white
                if(ColorMap[MoveY][MoveX] == 0)
                {
                    for(int j = CurHeight; j<4; j++)
                    {
                        if(ChessMap[CurY][CurX][j] == 0)
                        {
                            break;
                        }
                        

                        CurChessIndex = ChessMap[CurY][CurX][j] - 1;
                        CurChess = ChessList.get(CurChessIndex);
                        CurChess.y = MoveY;
                        CurChess.x = MoveX;
                        
                        ChessMap[MoveY][MoveX][NextHeight++] = ChessMap[CurY][CurX][j];
                        ChessMap[CurY][CurX][j] = 0;
                        
                        //end
                        if(NextHeight == 4)
                        {
                            return true;
                        }
                    }
                }
                
                //red
                else
                {
                    for(int j = 3; j>=CurHeight; j--)
                    {
                        if(ChessMap[CurY][CurX][j] == 0)
                        {
                            continue;
                        }
                        
                        CurChessIndex = ChessMap[CurY][CurX][j] - 1;
                        CurChess = ChessList.get(CurChessIndex);
                        CurChess.y = MoveY;
                        CurChess.x = MoveX;
                        
                        ChessMap[MoveY][MoveX][NextHeight++] = ChessMap[CurY][CurX][j];
                        ChessMap[CurY][CurX][j] = 0;

                        //end
                        if(NextHeight == 4)
                        {
                            return true;
                        }
                    }
                }
                
            }
            
        }
        
        return false;
    }
    
    public static void main(String[] args) throws IOException{
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        
         N = Integer.parseInt(st.nextToken());
         K = Integer.parseInt(st.nextToken());
         
         ColorMap = new int[N][N];
         ChessMap = new int[N][N][4];
         
         for(int i = 0; i<N; i++)
         {
             st = new StringTokenizer(br.readLine());
             for(int j = 0; j<N; j++)
             {
                 ColorMap[i][j] = Integer.parseInt(st.nextToken());
             }
         }
         
         for(int i = 0; i<K; i++)
         {
             st = new StringTokenizer(br.readLine());
             int y = Integer.parseInt(st.nextToken());
             int x = Integer.parseInt(st.nextToken());
             int way = Integer.parseInt(st.nextToken());
             
             ChessList.add(new Chess(y-1,x-1,way));
             
             ChessMap[y-1][x-1][0] = i+1;
         }
         
         
         int t;
         for(t = 1; t<=1000; t++)
         {
             if(Move() == true)
             {
                 System.out.println(t);
                 break;
             }
         }
         
         if(t > 1000)
         {
             System.out.println(-1);
         }
        

    }
}

'알고리즘' 카테고리의 다른 글

백준 21609 JAVA  (1) 2024.07.16
백준 20061 JAVA  (2) 2024.07.16
백준 17144 JAVA  (0) 2024.07.12
백준 17142 JAVA  (0) 2024.07.11
백준 16235 JAVA  (0) 2024.07.10