結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー marimom7marimom7
提出日時 2016-01-26 19:39:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,280 bytes
コンパイル時間 692 ms
コンパイル使用メモリ 70,396 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 16:22:22
合計ジャッジ時間 1,395 ms
ジャッジサーバーID
(参考情報)
judge9 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<stdio.h>
#include<iostream>
#include<queue>
#include<memory.h>

using namespace std;

const int fieldSize = 16;

enum FieldVect {
	UP,
	RIGHT,
	DOWN,
	LEFT
};

struct block {
	int num = 0;
	bool move = false;
};

void CommandAdd(FieldVect vec,vector<FieldVect> *buf,queue<vector<FieldVect>> *commandList) {
	buf->push_back(vec);
	commandList->push(*buf);
	buf->pop_back();
}

int main() {

	int field_input[fieldSize];
	block field_basic[fieldSize];
	block field[fieldSize];

	queue<vector<FieldVect>> commandList;
	vector<FieldVect> buf;
	int index = -1;
	int oldindex = -1;
	FieldVect gv;
	bool moved = false;
	bool complate = false;
	block box;

	for (int i = 0; i < fieldSize; i++)field[i].num = field_basic[i].num = (i + 1)% fieldSize;
	for (int i = 0; i < fieldSize; i++)cin >> field_input[i];

	CommandAdd(UP, &buf, &commandList);
	CommandAdd(LEFT, &buf, &commandList);

	while (!commandList.empty())
	{
		buf = commandList.front();
		commandList.pop();

		complate = true;
		for (int i = 0; i < fieldSize; i++)complate &= field[i].num == field_input[i];

		if (!complate) {

			memcpy(field, field_basic, sizeof(field));

			index = fieldSize - 1;
			moved = true;
			for (int i = 0; i < buf.size(); i++) {

				gv = buf[i];
				oldindex = index;

				switch (gv) {
				case UP: {
					index += -4;
					if (index < 0)index = -1;
					break;
				}
				case RIGHT: {
					if (index % 4 + 1 >= 4) {
						index = -1;
					}
					else {
						index += 1;
					}
					break;
				}
				case DOWN: {
					index += 4;
					if (index >= fieldSize)index = -1;
					break;
				}
				case LEFT: {
					if (index % 4 - 1 < 0) {
						index = -1;
					}
					else {
						index += -1;
					}
					break;
				}
				default:index = -1; break;
				}

				if (index < 0 || index > fieldSize - 1)index = -1;

				if (index != -1 && !field[index].move) {

					field[index].move = true;

					box = field[index];
					field[index] = field[oldindex];
					field[oldindex] = box;
				}
				else {
					moved = false;
				}
			}

			if (moved) {

				CommandAdd(UP, &buf, &commandList);
				CommandAdd(RIGHT, &buf, &commandList);
				CommandAdd(LEFT, &buf, &commandList);
				CommandAdd(DOWN, &buf, &commandList);

			}
		}
	}

	cout << (complate ? "Yes" : "No") << endl;

	return 0;
}
0