백준

[백준] 17837번 새로운 게임2 (java, 시뮬레이션, 구현)

해랑쓰 2021. 4. 20. 01:39
728x90
반응형

www.acmicpc.net/problem/17837

 

17837번: 새로운 게임 2

재현이는 주변을 살펴보던 중 체스판과 말을 이용해서 새로운 게임을 만들기로 했다. 새로운 게임은 크기가 N×N인 체스판에서 진행되고, 사용하는 말의 개수는 K개이다. 말은 원판모양이고, 하

www.acmicpc.net

말이 4개 이상 겹치면 탈출

chips라는 이차원 배열에 arraylist가 들어가게 해서 해당 위치에 칩 리스트를 만들어줬다.

 

package algo0419;

import java.util.ArrayList;
import java.util.Scanner;

class Horse{
	int y,x,dir;

	public Horse(int y, int x, int dir) {
		super();
		this.y = y;
		this.x = x;
		this.dir = dir;
	}
	
}

public class B_17837_새로운게임2_Main {
	static int n,k;//말 개수 k n체스판 크기 
	static int[][] map;
	static Horse[] horses;
	static int[] xpos= {1,-1,0,0};// 동서북남
	static int[] ypos= {0,0,-1,1};
	static ArrayList<Integer>[][] chips;
	static int time;
	static int[][] copy;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		n=sc.nextInt();
		k=sc.nextInt();
		map=new int[n][n];
		copy=new int[n][n];
		horses=new Horse[k+1];
		chips=new ArrayList[n][n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				chips[i][j]=new ArrayList<>();
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				map[i][j]=sc.nextInt(); //0 흰 1빨 2파
			}
		}
		
		for (int i = 0; i < k; i++) {
			int y=sc.nextInt();
			int x=sc.nextInt();
			int dir=sc.nextInt();
			horses[i+1]=new Horse(y-1,x-1,dir-1);
			chips[y-1][x-1].add(i+1);
		}
		while(time<=1000) {
			time++;
			move();
		}
		System.out.println(-1);
	}
	private static void move() {
		
		for (int i = 1; i <=k; i++) {
			int dir=horses[i].dir;
			int y=horses[i].y;
			int x=horses[i].x;
			int yy=y+ypos[dir];
			int xx=x+xpos[dir];
			//범위 벗어나는 경우, 파란색 영역 
			if(xx<0 || yy<0 || xx>=n || yy>=n || map[yy][xx]==2) {
				if(dir==0 || dir==1) {//반대 방향 전환
					horses[i].dir=1-dir;
				}else {
					horses[i].dir=5-dir;
				}
				//반대 바꾼 후 갈 수 있나 체크 
				int ny=y+ypos[horses[i].dir];
				int nx=x+xpos[horses[i].dir];
				if(ny<0 || nx<0 || ny>=n || nx>=n || map[ny][nx]==2)continue;
				if(map[ny][nx]!=2) {//파란색이 아닌 경우 이동 후 반복?
					i--;
					continue; //흰색이나 빨간색으로 다시 이동 시도
				}
			}else if(map[yy][xx]==0 || map[yy][xx]==1) {//칩 옮기는 거는 밑에서 함
				// 옮긴 후 현재 칩 위의 말들도 다 옮긴다.
				boolean start=false;
				ArrayList<Integer> temp=new ArrayList<>();
				for (int j = 0; j < chips[y][x].size(); j++) {
					int num=chips[y][x].get(j);//해당 위치에 있는 말의 번호
					if(num==i) {
						start=true;
						temp.add(i);
						chips[y][x].remove(j);
						j--;
						continue;
					}
					if(start) {// 그 위의 칩들의 좌표도 갱신해준다.
						horses[num].y=yy;
						horses[num].x=xx;
						temp.add(num);
						chips[y][x].remove(j);
						j--;
					}
				}
	
				//좌표 갱신 여기서 하기
				horses[i].y=yy;
				horses[i].x=xx;
				if(map[yy][xx]==0) {//순서대로 올림 
					for (int j = 0; j < temp.size(); j++) {				
						chips[yy][xx].add(temp.get(j));
					}
					
				}else if(map[yy][xx]==1) {//뒤집어서 거꾸로 올림
					for (int j = temp.size()-1; j >=0; j--) {
						chips[yy][xx].add(temp.get(j));
					}
					

				}
				
				if(chips[yy][xx].size()>=4) {
					System.out.println(time);
					System.exit(0);
				}
				
			}
			
			
		}

	}
	

}
728x90
반응형