Nine Knights

 Nine Knights

时间限制: 1 Sec   内存限制: 128 MB
提交: 155   解决: 46
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

In the game of chess, knights are unique due to their “L-shaped” movement. A knight can move, as shown in figure A.1, by either moving two squares sideways and one square up or down, or moving one square sideways and two squares either up or down.
figure A.1: The highlight e d squares show all possible moves for a knight. 
In the Nine Knights puzzle, exactly nine knights must be positioned on a 5-by-5 board so that no knight can attack another knight with a single move. The configuration shown in figure A.2 is an invalid solution because two of the knights can attack each other, where the configuration shown in figure A.3 is a valid solution.
figure A.2: Invalid game configuration                                figure A.3: Valid game configuration
Given the description of a game configuration, your job is to determine whether or not it represents a valid solution to the Nine Knights puzzle.

输入

The input will consist of 5 lines, each having 5 characters. All characters will be either ’k’, indicating the  placement of a knight, or ’.’, indicating an empty space on the board.

输出

Display the word valid if the given chess board is a valid solution to the Nine Knights puzzle. Otherwise,display the word invalid.

样例输入

...k.
...k.
k.k..
.k.k.
k.k.k

样例输出

invalid
题目大意:一个5*5的棋盘,上面有棋子(k代表),每个棋子的运动一次,规定为:只能左走或右走两部再上走或下走一步,或者上走或者下走两步再左走或者右走一步
所以共8种走路的路线,问棋盘上的是否存在棋子只走一次,就能到达其他棋子,如果存在输出“invalid”,否则输出“valid”。
解题思路:建立一个棋子运动的路径的方向数组,只要在这个方向路径中没有其他棋子阻挡,并在最后的终点有棋子就终止循环,否则扫描完所有的棋子,及其所走的8个路线,
注意:必须保证有9个棋子。(WA超多次的原因)
给出c++代码(不是正宗的c++):
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
char maze[6][6]={'\0'};
int ter[8][6]={
 0,1,0,2,1,2 
,0,1,0,2,-1,2  
,1,0,2,0,2,1
,1,0,2,0,2,-1  
,0,-1,0,-2,1,-2 
,0,-1,0,-2,-1,-2  
,-1,0,-2,0,-2,1
,-1,0,-2,0,-2,-1 };  //8个方向路径
typedef struct nodee{
	int x,y;
}node;             
int main()
{	int i,j,countt,xx,yy,flag;
	queue<node> q;
	node now;
	for(i=0;i<5;i++){
		scanf("%s",maze[i]);
	}
	
	countt=0;
	for(i=0;i<5;i++){
		for(j=0;j<5;j++){
			if(maze[i][j]=='k'){
				now.x=i;
				now.y=j;
				q.push(now);   
				countt++;
			}
		}
	}             //结构体存棋子的位置
	if(countt!=9){
		printf("invalid\n");
		return 0;
	}
	
	while(!q.empty()){
		now=q.front();
		q.pop();
	
		for(i=0;i<8;i++){
			flag=0;
			for(j=0;j<4;j+=2){
				xx=now.x+ter[i][j];
				yy=now.y+ter[i][j+1];
				if(xx>=0&&xx<5&&yy>=0&&yy<5&&maze[xx][yy]=='k'){
					flag=1;
					break;
				}
				xx=now.x+ter[i][4];        //对8个方向依次判断
				yy=now.y+ter[i][5];
				if(xx>=0&&xx<5&&yy>=0&&yy<5&&flag==0&&maze[xx][yy]=='k'){
					printf("invalid\n");
					return  0;
				}
			}
		}
	}
	
	printf("valid\n");

	return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值