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

유형 : 구현 , 자료구조
새로운 게임 2와의 차이점 :
- 새로운 게임 2와의 차이점은 체스판에서 가장 아래에 있는 말만 움직일 수 있다는 것이다.
접근 방식 :
- moveW (흰색 지점 이동) , moveR(빨간색 지점 이동) , moveB(파란색 지점 이동) 의 함수를 구현한다.
- moveB의 경우에는 특수하게 반대 방향으로 이동하기 때문에 메소드 내부적으로 moveW , moveR을 호출하도록 작성한다.
- moveR의 경우 들어가는 말이 거꾸로 가기 때문에 스택을 사용하였다.
- 말이 한개 이동했을 때 이동 지점의 말의 개수가 4개가 넘는지 확인한다. -> 종료 조건
전체 코드
import java.util.*;
import java.io.*;
public class Main {
static int n = 0;
static int m = 0;
static int[][] board;
static Node[] cur;
static ArrayList<Integer>[][] chess;
static int[] dx = {0,0,0,-1,1};
static int[] dy = {0,1,-1,0,0};
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());
m = Integer.parseInt(st.nextToken());
cur = new Node[m+1];
board = new int[n+1][n+1];
chess = new ArrayList[n+1][n+1];
for(int i=1;i<=n;i++){
st = new StringTokenizer(br.readLine()," ");
for(int j=1;j<=n;j++){
board[i][j] = Integer.parseInt(st.nextToken());
chess[i][j] = new ArrayList<>();
}
}
for(int i=1;i<=m;i++){
st = new StringTokenizer(br.readLine()," ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int dir = Integer.parseInt(st.nextToken());
cur[i] = new Node(x,y,dir);
chess[x][y].add(i);
}
boolean tmp = true;
for(int i=1;i<=1000;i++){
tmp = turn();
if(!tmp)
{
System.out.println(i);
break;
}
}
if(tmp)
System.out.println(-1);
br.close();
}
static int changeDir(int cur){
if(cur == 1)
return 2;
else if(cur == 2)
return 1;
else if(cur == 3)
return 4;
else
return 3;
}
static boolean turn(){
boolean re = true;
for(int i=1;i<=m;i++){
int x = cur[i].x;
int y = cur[i].y;
int d = cur[i].dir;
if(chess[x][y].get(0) != i)
continue;
int nx = x + dx[d];
int ny = y + dy[d];
if(nx < 1 || nx > n || ny < 1 || ny > n){
re = moveB(x,y,d,i);
}
else if(board[nx][ny] == 2){
re = moveB(x,y,d,i);
}
else if(board[nx][ny] == 0){
re = moveW(x,y,nx,ny,d,i);
}
else if(board[nx][ny] == 1){
re = moveR(x,y,nx,ny,d,i);
}
if(!re)
return false;
}
return true;
}
static boolean moveW(int x,int y,int nx,int ny,int dir,int num){
ArrayList<Integer> tmp = new ArrayList<>();
for(int next : chess[x][y]){
chess[nx][ny].add(next);
cur[next] = new Node(nx,ny,cur[next].dir);
}
if(chess[nx][ny].size() >= 4)
return false;
chess[x][y] = tmp;
cur[num] = new Node(nx,ny,dir);
return true;
}
static boolean moveR(int x,int y,int nx,int ny,int dir,int num){
ArrayList<Integer> tmp = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
for(int cur : chess[x][y]){
stack.push(cur);
}
while(!stack.isEmpty()){
int next = stack.pop();
chess[nx][ny].add(next);
cur[next] = new Node(nx,ny,cur[next].dir);
}
if(chess[nx][ny].size() >= 4)
return false;
chess[x][y] = tmp;
cur[num] = new Node(nx,ny,dir);
return true;
}
static boolean moveB(int x,int y,int dir,int num){
int nd = changeDir(dir);
int nx = x + dx[nd];
int ny = y + dy[nd];
if(nx < 1 || nx > n || ny < 1 || ny > n){
cur[num] = new Node(x,y,nd);
if(chess[x][y].size() >= 4)
return false;
}
else if(board[nx][ny] == 2){
cur[num] = new Node(x,y,nd);
if(chess[x][y].size() >= 4)
return false;
}
else if(board[nx][ny] == 0){
return moveW(x,y,nx,ny,nd,num);
}
else if(board[nx][ny] == 1){
return moveR(x,y,nx,ny,nd,num);
}
return true;
}
static void print(){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
System.out.print(chess[i][j].size()+" ");
}
System.out.println();
}
System.out.println();
}
}
class Node{
int x;
int y;
int dir;
Node(int x,int y,int dir){
this.x = x;
this.y = y;
this.dir = dir;
}
}'알고리즘 > 백준' 카테고리의 다른 글
| 백준 14499 (주사위 굴리기) - java (0) | 2023.09.07 |
|---|---|
| 백준 10653 (마라톤 2) - java (0) | 2023.09.07 |
| 백준 11054 (가장 긴 바이토닉 부분 수열) - java (0) | 2023.09.04 |
| 백준 3184 (양) - java (0) | 2023.08.29 |
| 백준 11403 (경로 찾기) - java (1) | 2023.08.29 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백준 #4963 #섬의 개수
- 백준 #5721 #사탕 줍기 대회 #java #자바
- 백준 #28140 #빨강~ 빨강~ 파랑! 파랑! 달콤한 솜사탕! #java #자바
- 백준 #17940 #주식 #자바 #java
- 백준 #16973 #직사각형 탈출
- 행복합시다.
- 올해보다
- 17394
- 백준
- 백준 #1987 #알파벳 #자바 #java
- 17218
- 백준 #치즈 #2638
- 백준 #12014 #주식 #자바 #java
- 백준 #2580 #스도쿠
- 백준 #1584 #게임 #java #자바
- 백준 #다리 만들기 #2146
- 백준 #인구 이동 #16234
- 백준 #1759 #암호 만들기
- 백준 #25603 #짱해커 이동식 #java #자바
- 백준 #15686 #치킨 배달
- 백준 #14863 #서울에서 경산까지 #java #자바
- 자바 #JAVA
- 백준 #1325 #효율적인 해킹
- 백준 #3980 #선발 명단
- 백준 #
- 백준 #18405 #경쟁적 전염
- 백준 #13549 #숨바꼭질3
- 백준 #2636 #치즈
- 자바
- Java
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
글 보관함