結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー alpha_virginisalpha_virginis
提出日時 2015-06-20 21:46:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,041 bytes
コンパイル時間 1,350 ms
コンパイル使用メモリ 145,120 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-21 22:12:16
合計ジャッジ時間 2,343 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int board[4][4] = {};
bool moved[4][4];
int ex;
int ey;

bool solve(int x, int y) {

  bool b;
  int bx = x;
  int by = y;
  int bex = ex;
  int bey = ey;

  /* 
  std::cout << x << " " << y << std::endl;
  for(int i = 0; i < 4; ++i) {
    for(int j = 0; j < 4; ++j) {
      std::cout << board[i][j] << " ";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;
  */

  
  if( not ( 0 <= x and x < 4 and 0 <= y and y < 4 ) ) {
    return false;
  }

  if( moved[x][y] ) {
    return false;
  }

  if( board[x][y] != 0 ) {
    std::swap(board[ex][ey], board[x][y]);
    moved[ex][ey] = true;
    ex = x;
    ey = y;
  }

  b = true;
  for(int i = 0; i < 4; ++i) {
    for(int j = 0; j < 4; ++j) {
      if( board[i][j] != i*4+j+1 && not ( i == 3 and j == 3 and board[i][j] == 0 ) ) {
	b = false;
      }
    }
  }
  if( b ) {
    return true;
  }
  
  if( solve(ex+1, ey) ) {
    ex = bex;
    ey = bey;
    moved[ex][ey] = false;
    std::swap(board[bex][bey], board[bx][by]);
    return true;
  }
  if( solve(ex-1, ey) ) {
    ex = bex;
    ey = bey;
    moved[ex][ey] = false;
    std::swap(board[bex][bey], board[bx][by]);
    return true;
  }
  if( solve(ex, ey+1) ) {
    ex = bex;
    ey = bey;
    moved[ex][ey] = false;
    std::swap(board[bex][bey], board[bx][by]);
    return true;
  }
  if( solve(ex, ey-1) ) {
    ex = bex;
    ey = bey;
    moved[ex][ey] = false;
    std::swap(board[bex][bey], board[bx][by]);
    return true;
  }
  
  ex = bex;
  ey = bey;
  moved[ex][ey] = false;
  std::swap(board[bex][bey], board[bx][by]);
  return false;
}
  

int main() {

  bool ans;
  
  for(int i = 0; i < 4; ++i) {
    for(int j = 0; j < 4; ++j) {
      moved[i][j] = false;
    }
  }

  for(int i = 0; i < 4; ++i) {
    for(int j = 0; j < 4; ++j) {
      std::cin >> board[i][j];
      if( board[i][j] == 0 ) {
	ex = i;
	ey = j;
      }
    }
  }
  
  ans = solve(ex, ey);

  if( ans ) {
    std::cout << "Yes" << std::endl;
  }
  else {
    std::cout << "No" << std::endl;
  }
  
  return 0;
}
0