結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー nanasilinanasili
提出日時 2015-10-08 12:04:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,147 bytes
コンパイル時間 1,475 ms
コンパイル使用メモリ 70,524 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 08:07:19
合計ジャッジ時間 1,439 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <string>

using namespace std;

int main() {
  int a[4][4], b[4][4];
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> a[j][i];
      b[j][i] = (i*4+j+1)%16;
    }
  }

  int px, py;
  px = py = 3;
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, -1, 0, 1};
  while(true) {
    if (b[px][py] != a[px][py]) {
      bool ok = false;
      for (int i = 0; i < 4; i++) {
	int nx = px+dx[i], ny = py+dy[i];
	if (nx >= 0 && ny >= 0 && nx < 4 && ny < 4 &&
	    b[nx][ny] == a[px][py]) {
	  ok = true;
	  swap(b[nx][ny], b[px][py]);
	  px = nx;
	  py = ny;
	  break;
	}
      }
      if (!ok) {
	puts("No");
	return 0;
      }
    }else {
      break;
    }
  }
  bool ok = true;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      if (a[j][i] != b[j][i]) {
	ok = false;
	break;
      }
    }
    if (!ok) break;
  }
  if (ok) {
    puts("Yes");
  }else {
    puts("No");
  }
}
0