結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー ldsybldsyb
提出日時 2017-06-22 00:06:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,062 bytes
コンパイル時間 637 ms
コンパイル使用メモリ 66,952 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 10:04:41
合計ジャッジ時間 1,318 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:28:18: warning: 'pi' may be used uninitialized [-Wmaybe-uninitialized]
   28 |         while((4 * pi + pj + 1) % 16){
      |                ~~^~~~
main.cpp:9:22: note: 'pi' was declared here
    9 |         int a[4][4], pi, pj, dd[] = {0, 1, 0, -1, 0};
      |                      ^~
main.cpp:28:23: warning: 'pj' may be used uninitialized [-Wmaybe-uninitialized]
   28 |         while((4 * pi + pj + 1) % 16){
      |                ~~~~~~~^~~~
main.cpp:9:26: note: 'pj' was declared here
    9 |         int a[4][4], pi, pj, dd[] = {0, 1, 0, -1, 0};
      |                          ^~

ソースコード

diff #

#include <iostream>
using namespace std;

bool inside(int a, int b, int alim, int blim){
	return 0 <= a && a < alim && 0 <= b && b < blim;
}

int main(){
	int a[4][4], pi, pj, dd[] = {0, 1, 0, -1, 0};
	for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++){
		cin >> a[i][j];
		if(a[i][j] == 0){
			pi = i;
			pj = j;
			continue;
		}
		bool f = a[i][j] == (4 * i + j + 1) % 16;
		for(int k = 0; k < 4; k++){
			int ni = i + dd[k], nj = j + dd[k + 1];
			if(inside(ni, nj, 4, 4)) f |= a[i][j] == (4 * ni + nj + 1) % 16;
		}
		if(!f){
			cerr << i << ' ' << j << endl;
			cout << "No" << endl;
			return 0;
		}
	}
	while((4 * pi + pj + 1) % 16){
		bool f = true;
		for(int i = 0; i < 4; i++){
			int ni = pi + dd[i], nj = pj + dd[i + 1];
			if(inside(ni, nj, 4, 4) && a[ni][nj] == (4 * pi + pj + 1) % 16){
				swap(a[pi][pj], a[ni][nj]);
				pi = ni;
				pj = nj;
				f = false;
				break;
			}
		}
		if(f) break;
	}
	bool f = true;
	for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) f &= a[i][j] == (4 * i + j + 1) % 16;
	cout << (f ? "Yes" : "No") << endl;
}
0