結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー masamasa
提出日時 2015-06-19 23:37:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 925 bytes
コンパイル時間 438 ms
コンパイル使用メモリ 62,136 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-21 10:18:33
合計ジャッジ時間 1,432 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:29:21: warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  while (x != 3 || y != 3) {
                   ~~^~~~
main.cpp:29:11: warning: ‘x’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  while (x != 3 || y != 3) {
         ~~^~~~

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>

using namespace std;

int main() {
	int board[4][4];
	int num;
	int x, y;

	for (int i = 0; i < 16; i++) {
		cin >> num;
		if (num == 0) {
			x = i % 4;
			y = i / 4;
		}

		board[i / 4][i % 4] = num;
	}

	int dx[4] = {0,1,0,-1};
	int dy[4] = {1,0,-1,0};
	int nx, ny;

	while (x != 3 || y != 3) {
		num = x + y * 4 + 1;

		bool change = false;
		for (int j = 0; j < 4; j++) {
			nx = x + dx[j];
			ny = y + dy[j];

			if (nx < 0 || 3 < nx || ny < 0 || 3 < ny) {
				continue;
			}

			if (board[ny][nx] == num) {
				swap(board[y][x], board[ny][nx]);
				change = true;
				x = nx;
				y = ny;
				break;
			}
		}

		if (!change) {
			break;
		}
	}

	for (int i = 0; i < 16; i++) {
		if (board[i / 4][i % 4] != (i + 1) % 16) {
			cout << "No" << endl;
			return 0;
		}
	}

	cout << "Yes" << endl;
	return 0;
}
0