Submission #363469


Source Code Expand

#include <stdio.h>

void copyBoard( int* src, int* dest )
{
	for( int i = 0; i < 64; ++i ){
		dest[i] = src[i];
	}
}
int getIndex(int x, int y)
{
	return x+y*8;
}
bool putBoard( int* dest, int putX, int putY )
{
	if( dest[getIndex(putX,putY)] ){
		return false;
	}
	// 横一列
	for( int i = 0; i < 8; ++i ){
		int x = i;
		int y = putY;
		if(dest[getIndex(x,y)]){continue;}
		dest[getIndex(x,y)] = 1;
	}
	// 縦一列
	for( int i = 0; i < 8; ++i ){
		int x = putX;
		int y = i;
		dest[getIndex(x,y)] = 1;
	}
	// -,-
	int x = putX;
	int y = putY;
	for( int i = 0; i < 8; ++i ){
		--x;
		--y;
		if( x < 0 || y < 0 || x >= 8 || x >= 8 ){ break; }
		if(dest[getIndex(x,y)]){continue;}
		dest[getIndex(x,y)] = 1;
	}
	// +,-
	x = putX;
	y = putY;
	for( int i = 0; i < 8; ++i ){
		++x;
		--y;
		if( x < 0 || y < 0 || x >= 8 || x >= 8 ){ break; }
		if(dest[getIndex(x,y)]){continue;}
		dest[getIndex(x,y)] = 1;
	}
	// -,+
	x = putX;
	y = putY;
	for( int i = 0; i < 8; ++i ){
		--x;
		++y;
		if( x < 0 || y < 0 || x >= 8 || x >= 8 ){ break; }
		if(dest[getIndex(x,y)]){continue;}
		dest[getIndex(x,y)] = 1;
	}
	// +,+
	x = putX;
	y = putY;
	for( int i = 0; i < 8; ++i ){
		++x;
		++y;
		if( x < 0 || y < 0 || x >= 8 || x >= 8 ){ break; }
		if(dest[getIndex(x,y)]){continue;}
		dest[getIndex(x,y)] = 1;
	}
	dest[getIndex(putX,putY)] = 2;
	return true;
}

void printBoard( int* src ){
	for( int y = 0; y < 8; ++y ){
		for( int x = 0; x < 8; ++x ){
			if( src[getIndex(x,y)] == 2 ){
				printf("Q");
			}
			else{
				printf(".");
			}
		}
		printf("\n");
	}
}

bool getPutPoint( int* src, int* outX, int* outY )
{
	for( int y = 0; y < 8; ++y ){
		for( int x = 0; x < 8; ++x ){
			if( src[getIndex(x,y)] == 0 ){
				*outX = x;
				*outY = y;
				return true;
			}
		}
	}
	return false;
}

void debugPrintBoard( int* src, int putX, int putY, int putCount )
{
	printf("PutCount:%d\n",putCount);
	for( int y = 0; y < 8; ++y ){
		for( int x = 0; x < 8; ++x ){
			if( x == putX && y == putY ){
				printf("!");
			}
			else{
				switch (src[getIndex(x,y)] ) {
					case 0:
						printf(".");
						break;
					case 1:
						printf("*");
						break;
					case 2:
						printf("Q");
						break;
					case 3:
						printf("X");
						break;
						
					default:
						printf(".");
						break;
				}
			}
		}
		printf("\n");
	}
	printf("\n");
}

bool checkBoard( int* src, int putCount )
{
	if( putCount == 8 ){
		return true;
	}
	int undoBoard[64];
	copyBoard(src,undoBoard);
	int x, y;
	while( getPutPoint( src, &x, &y ) ){
		int undoBoard[64];
		copyBoard(src,undoBoard);
		if( putBoard( src, x, y ) ){
			if( checkBoard(src,putCount+1) ){
				return true;
			}
		}
		// 元に戻す
		copyBoard(undoBoard,src);
		src[getIndex(x,y)] = 3;
	}
	
	// 元に戻す
	copyBoard(undoBoard,src);
	
	return false;
}

int main()
{
	char ban[8][9];
	for( int i = 0; i < 8; ++i ){
		scanf("%s", ban[i]);
	}
	int board[64];
	for( int i = 0; i < 8*8; ++i ){
		board[i] = 0;
	}
	int putCount = 0;
	bool initResult = true;
	for( int y = 0; y < 8; ++y ){
		for( int x = 0; x < 8; ++x ){
			if( ban[y][x] == 'Q' ){
				++putCount;
				if( !putBoard(board,x,y) ){
					initResult = false;
					break;
				}
			}
		}
		if( !initResult ){
			break;
		}
	}
	if( !initResult ){
		printf("No Answer\n");
	}
	else{
		if( checkBoard(board,putCount) ){
			printBoard(board);
		}
		else{
			printf("No Answer\n");
		}
	}
	return 0;
}

Submission Info

Submission Time
Task C - パズルのお手伝い
User Neige
Language C++ (G++ 4.6.4)
Score 100
Code Size 3594 Byte
Status AC
Exec Time 25 ms
Memory 808 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:168:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 42
Set Name Test Cases
All 00_sample1.txt, 00_sample2.txt, 01_rnd_00.txt, 01_rnd_01.txt, 01_rnd_02.txt, 01_rnd_03.txt, 01_rnd_04.txt, 01_rnd_05.txt, 01_rnd_06.txt, 01_rnd_07.txt, 01_rnd_08.txt, 01_rnd_09.txt, 01_rnd_10.txt, 01_rnd_11.txt, 01_rnd_12.txt, 01_rnd_13.txt, 01_rnd_14.txt, 01_rnd_15.txt, 01_rnd_16.txt, 01_rnd_17.txt, 01_rnd_18.txt, 01_rnd_19.txt, 01_rnd_20.txt, 01_rnd_21.txt, 01_rnd_22.txt, 01_rnd_23.txt, 01_rnd_24.txt, 01_rnd_25.txt, 01_rnd_26.txt, 01_rnd_27.txt, 01_rnd_28.txt, 01_rnd_29.txt, 01_rnd_30.txt, 01_rnd_31.txt, 01_rnd_32.txt, 01_rnd_33.txt, 01_rnd_34.txt, 01_rnd_35.txt, 01_rnd_36.txt, 01_rnd_37.txt, 01_rnd_38.txt, 01_rnd_39.txt
Case Name Status Exec Time Memory
00_sample1.txt AC 22 ms 800 KiB
00_sample2.txt AC 21 ms 804 KiB
01_rnd_00.txt AC 22 ms 808 KiB
01_rnd_01.txt AC 21 ms 700 KiB
01_rnd_02.txt AC 23 ms 704 KiB
01_rnd_03.txt AC 22 ms 716 KiB
01_rnd_04.txt AC 22 ms 796 KiB
01_rnd_05.txt AC 23 ms 796 KiB
01_rnd_06.txt AC 21 ms 724 KiB
01_rnd_07.txt AC 21 ms 708 KiB
01_rnd_08.txt AC 21 ms 800 KiB
01_rnd_09.txt AC 23 ms 708 KiB
01_rnd_10.txt AC 22 ms 800 KiB
01_rnd_11.txt AC 23 ms 708 KiB
01_rnd_12.txt AC 21 ms 800 KiB
01_rnd_13.txt AC 23 ms 708 KiB
01_rnd_14.txt AC 23 ms 800 KiB
01_rnd_15.txt AC 22 ms 712 KiB
01_rnd_16.txt AC 23 ms 796 KiB
01_rnd_17.txt AC 23 ms 704 KiB
01_rnd_18.txt AC 23 ms 652 KiB
01_rnd_19.txt AC 23 ms 708 KiB
01_rnd_20.txt AC 21 ms 708 KiB
01_rnd_21.txt AC 24 ms 704 KiB
01_rnd_22.txt AC 22 ms 796 KiB
01_rnd_23.txt AC 23 ms 796 KiB
01_rnd_24.txt AC 23 ms 704 KiB
01_rnd_25.txt AC 23 ms 700 KiB
01_rnd_26.txt AC 21 ms 708 KiB
01_rnd_27.txt AC 24 ms 716 KiB
01_rnd_28.txt AC 23 ms 700 KiB
01_rnd_29.txt AC 24 ms 704 KiB
01_rnd_30.txt AC 24 ms 804 KiB
01_rnd_31.txt AC 24 ms 704 KiB
01_rnd_32.txt AC 24 ms 708 KiB
01_rnd_33.txt AC 22 ms 652 KiB
01_rnd_34.txt AC 23 ms 800 KiB
01_rnd_35.txt AC 25 ms 772 KiB
01_rnd_36.txt AC 21 ms 704 KiB
01_rnd_37.txt AC 21 ms 708 KiB
01_rnd_38.txt AC 21 ms 796 KiB
01_rnd_39.txt AC 24 ms 712 KiB