結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー TLwiegehttTLwiegehtt
提出日時 2015-07-16 02:06:59
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,373 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 25,204 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 16:37:50
合計ジャッジ時間 1,157 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>

#define WALL	(99)
int start[6][6] = {
	{WALL,WALL,WALL,WALL,WALL,WALL},
	{WALL,   1,   2,   3,   4,WALL},
	{WALL,   5,   6,   7,   8,WALL},
	{WALL,   9,  10,  11,  12,WALL},
	{WALL,  13,  14,  15,   0,WALL},
	{WALL,WALL,WALL,WALL,WALL,WALL}
};

int target[6][6];
int used[18];

int saiki(int x, int y){
	int i,j;
	int ret=0, flag=1;
	int px[] = {-1, 0, 1, 0};
	int py[] = { 0,-1, 0, 1};
	
	for(i=0;i<6;i++){
		for(j=0;j<6;j++){
			if(start[i][j] != target[i][j]){
				i=j=6;
				flag = 0;
				break;
			}
		}
	}
	
	if(flag==1){
		return 1;
	}
	
	for(i=0;i<4;i++){
		int tmp = start[y+py[i]][x+px[i]];
		int chg;
		if(tmp == WALL){
			continue;
		}
		
		if(used[tmp] == 1){
			continue;
		}
		
		chg = start[y+py[i]][x+px[i]];
		start[y+py[i]][x+px[i]] = start[y][x];
		start[y][x] = chg;
		
		used[tmp] = 1;
		ret= saiki(x+px[i], y+py[i]);
		used[tmp] = 0;
		
		chg = start[y+py[i]][x+px[i]];
		start[y+py[i]][x+px[i]] = start[y][x];
		start[y][x] = chg;
		
		if(ret == 1){
			break;
		}
	}
	
	return ret;
}

int main(void){
	int i,j;
	int ret;
	for(i=0;i<6;i++){
		for(j=0;j<6;j++){
			target[i][j] = WALL;
		}
	}
	
	for(i=0;i<18;i++){used[i] = 0;}
	
	for(i=1;i<=4;i++){
		for(j=1;j<=4;j++){
			int tmp;
			scanf("%d", &tmp);
			target[i][j] = tmp;
		}
	}
	
	ret = saiki(4,4);
	
	if(ret==1){
		printf("Yes\n");
	}else{
		printf("No\n");
	}
	return 0;
}
0