3-Puzzle

시간 제한1초메모리 제한2048 MB

요약
1부터 3까지의 타일과 빈칸 하나가 있는 2x2 슬라이딩 퍼즐이 주어질 때, 완성 상태까지 필요한 최소 이동 횟수를 구한다.
난이도

쉬움10점 중 3점

유형
BFS, 그래프, 해시맵
정답자
아직 제출이 없습니다

문제

Your friend needs help solving a 1515-Puzzle, so to warm up, you solve the 33-Puzzle instead. A 33-Puzzle consists of a 2×22 \times 2 grid containing 33 tiles numbered 11 through 33 and one empty space. The goal is to slide the tiles around so that they are in ascending row-major order and the empty space is on the bottom right like this:

1122
33

Given the starting position of a 33-Puzzle, find the minimum number of moves it takes to solve the puzzle. Here's an example of how sample input 11 can be solved in 33 moves:

Starting position:

22
1133

After 11 move:

22
1133

After 22 moves:

1122
33

After 33 moves:

1122
33

입력

The input will consist of exactly 22 lines, each containing exactly 22 characters.

Each character is either a number 11 through 33 (representing one of the tiles) or a dash (-) (the empty space).

The puzzle state represented by the input is guaranteed to be a solvable configuration.

출력

Output a singe integer, indicating the minimum number of moves required to solve the puzzle from the provided starting position, or 00 if it's already in the solved position.

예제2

  1. 예제 1

    입력
    2-
    13
    
    예상 출력
    3
    
  2. 예제 2

    입력
    -3
    21
    
    예상 출력
    6