끄적끄적 코딩
article thumbnail

시뮬레이션 문제입니다.

R연산 - 행의 개수가 열의 개수보다 크거나 같은 경우
C연산 - 열의 개수가 행의 개수보다 큰 경우

R연산은 행에 대하여 정렬을 진행합니다.
C연산은 열에 대하여 정렬을 진행합니다.

정렬을 예로들면 

각 수의 개수를 구합니다.
1 2 1 => 숫자 1이 2개 / 숫자 2가 1개
오름차순으로 개수를 기준으로 정렬 해줍니다.
숫자 2가 1개, 숫자 1이 2개
=> 2 1 1 2
R연산이었다면 모든 행에 위의 정렬을 해줍니다.

모든 연산이 끝나고 행의 길이를 갱신해줍니다.

행과 열의 길이를 비교해서 R연산 C연산을 진행합니다.
시간이 100초가 넘어가거나, 값을 찾으면 종료합니다.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
  static int n;
  static int m;
  static int num;
  static int ySize;
  static int xSize;
  static int tempSize;
  static int[][] map;
  static int[] cnt;
  static int result;
  static ArrayList<Data> al;

  public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    StringTokenizer st = new StringTokenizer(br.readLine());

    n = Integer.parseInt(st.nextToken()) - 1;
    m = Integer.parseInt(st.nextToken()) - 1;
    num = Integer.parseInt(st.nextToken());
    ySize = 3;
    xSize = 3;
    map = new int[101][101];
    cnt = new int[101];
    result = 0;

    for (int i = 0; i < 3; ++i) {
      st = new StringTokenizer(br.readLine());
      for (int j = 0; j < 3; ++j) {
        map[i][j] = Integer.parseInt(st.nextToken());
      }
    }

    while (true) {
      if (map[n][m] == num) {
        break;
      }

      if (result > 100) {
        result = -1;
        break;
      }

      ++result;

      int size;
      int d;
      if (xSize <= ySize) {
        size = ySize;
        d = 0;
      } else {
        size = xSize;
        d = 1;
      }

      tempSize = 0;
      for (int i = 0; i < size; ++i) {
        init();

        count(i, d);

        alCount(d);

        alSort();

        change(i, d);
      }

      if (d == 0) {
        xSize = tempSize;
      } else {
        ySize = tempSize;
      }
    }

    bw.write(result + "\n");
    bw.close();

  }

  public static void alSort() {
    al.sort((a, b) -> {
      if (a.num == b.num) {
        return a.index - b.index;
      } else {
        return a.num - b.num;
      }
    });
  }

  public static void init() {
    al = new ArrayList<>();
    for (int i = 0; i < 101; ++i) {
      cnt[i] = 0;
    }
  }

  public static void count(int i, int d) {
    if (d == 0) {
      for (int j = 0; j < xSize; ++j) {
        if (map[i][j] == 0) {
          continue;
        }
        ++cnt[map[i][j]];
        map[i][j] = 0;
      }
    } else {
      for (int j = 0; j < ySize; ++j) {
        if (map[j][i] == 0) {
          continue;
        }
        ++cnt[map[j][i]];
        map[j][i] = 0;
      }
    }
  }

  public static void alCount(int d) {
    int size = 0;

    for (int i = 0; i < 101; ++i) {
      if (cnt[i] == 0) {
        continue;
      }
      al.add(new Data(cnt[i], i));
      ++size;
    }

    tempSize = Math.max(tempSize, size * 2);
  }

  public static void change(int index, int d) {
    if (d == 0) {
      for (int i = 0; i < al.size(); ++i) {
        map[index][2 * i] = al.get(i).index;
        map[index][2 * i + 1] = al.get(i).num;
      }

      int next = al.size() * 2;
      for (int i = next; i < 101; ++i) {
        if (map[index][i] == 0) {
          break;
        } else {
          map[index][i] = 0;
        }
      }
    } else {
      for (int i = 0; i < al.size(); ++i) {
        map[2 * i][index] = al.get(i).index;
        map[2 * i + 1][index] = al.get(i).num;
      }

      int next = al.size() * 2;
      for (int i = next; i < 101; ++i) {
        if (map[i][index] == 0) {
          break;
        } else {
          map[i][index] = 0;
        }
      }
    }
  }

  public static class Data {
    int num;
    int index;

    public Data(int num, int index) {
      this.num = num;
      this.index = index;
    }
  }
}

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

[Java] 백준 11404번 플로이드  (0) 2023.07.07
[Java] 백준 12931번 두 배 더하기  (0) 2023.07.06
[Java] 백준 12946번 육각 보드  (0) 2023.07.06
[Java] 백준 2580번 스도쿠  (0) 2023.07.05
[Java] 백준 9663번 N-Queen  (0) 2023.07.04

검색 태그