ABOUT ME

beck33333@naver.com

Today
Yesterday
Total
  • 백준(BOJ) 17837번 새로운 게임2 ( JAVA )
    알고리즘 풀이/백준(Boj) java 2020. 2. 12. 02:28

    설명:

    https://100100e.tistory.com/275?category=804940

     

    백준(BOJ) 17837번 새로운 게임 2

    문제 : https://www.acmicpc.net/problem/17837 17837번: 새로운 게임 2 재현이는 주변을 살펴보던 중 체스판과 말을 이용해서 새로운 게임을 만들기로 했다. 새로운 게임은 크기가 N×N인 체스판에서 진행되..

    100100e.tistory.com

     

    코드 ( JAVA )

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    public class Main {
    static int n,k;
    static int[][] table = new int[12][12];
    static ArrayList<Integer>[][] order = new ArrayList[12][12];
    static ArrayList<gr> a = new ArrayList<>();
    static int[] dy = {0,0,-1,1};
    static int[] dx = {1,-1,0,0};
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    k = sc.nextInt();
    for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) {
    table[i][j] = sc.nextInt();
    }
    for(int i=0; i<12; ++i)
    for(int j=0; j<12; ++j)
    order[i][j] = new ArrayList<>();
    for (int i = 0; i < k; ++i) {
    int y, x, d;
    y = sc.nextInt();
    x = sc.nextInt();
    d = sc.nextInt();
    y -= 1;
    x -= 1;
    d -= 1;
    a.add(new gr(y, x, d));
    order[y][x].add(i);
    }
    int turn = 0;
    while (true) {
    turn++;
    if (turn > 1000) break;
    for (int i = 0; i < k; ++i) {
    int y = a.get(i).y;
    int x = a.get(i).x;
    int ny = y + dy[a.get(i).d];
    int nx = x + dx[a.get(i).d];
    if (!(0 <= ny && ny < n && 0 <= nx && nx < n) || table[ny][nx] == 2) {
    if (a.get(i).d == 0)
    a.get(i).d = 1;
    else if (a.get(i).d == 1)
    a.get(i).d = 0;
    else if (a.get(i).d == 2)
    a.get(i).d = 3;
    else if (a.get(i).d == 3)
    a.get(i).d = 2;
    ny = y + dy[a.get(i).d];
    nx = x + dx[a.get(i).d];
    }
    if (!(0 <= ny && ny < n && 0 <= nx && nx < n) || table[ny][nx] == 2) ;
    else if (table[ny][nx] == 0) {
    int idx = -1;
    for (int j = 0; j < order[y][x].size(); ++j) {
    int cand = order[y][x].get(j);
    if (cand == i) {
    idx = j;
    }
    if (idx == -1)
    continue;
    a.get(cand).y = ny;
    a.get(cand).x = nx;
    order[ny][nx].add(cand);
    if (order[ny][nx].size() >= 4) {
    System.out.println(turn);
    System.exit(0);
    }
    }
    int cnt = order[y][x].size();
    for (int j = idx; j < cnt; ++j)
    order[y][x].remove(order[y][x].size() - 1);
    } else {
    int idx = -1;
    for (int j = order[y][x].size() - 1; j >= 0; --j) {
    int cand = order[y][x].get(j);
    if (cand == i) {
    idx = j;
    break;
    }
    }
    for (int j = order[y][x].size() - 1; j >= idx; --j) {
    int cand = order[y][x].get(j);
    a.get(cand).y = ny;
    a.get(cand).x = nx;
    order[ny][nx].add(cand);
    if (order[ny][nx].size() >= 4) {
    System.out.println(turn);
    System.exit(0);
    }
    }
    int cnt = order[y][x].size();
    for (int j = idx; j < cnt; ++j)
    order[y][x].remove(order[y][x].size() - 1);
    }
    }
    }
    System.out.println(-1);
    }
    public static class gr {
    int y;
    int x;
    int d;
    public gr(int y, int x, int d) {
    this.y = y;
    this.x = x;
    this.d = d;
    }
    }
    }
    view raw .java hosted with ❤ by GitHub

     

    '알고리즘 풀이 > 백준(Boj) java' 카테고리의 다른 글

Designed by Tistory.