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;
}
												

Nine Knights相关推荐

  1. Codeforces Gym100812 L. Knights without Fear and Reproach-扩展欧几里得(exgcd)

    补一篇以前的扩展欧几里得的题,发现以前写错了竟然也过了,可能数据水??? 这个题还是很有意思的,和队友吵了两天,一边吵一边发现问题??? L. Knights without Fear and Rep ...

  2. UVA1364 Knights of the Round Table(双连通分量、二分图染色,超详细解释)

    整理的算法模板合集: ACM模板 UVA1364 Knights of the Round Table 题目中要求互相有憎恨关系的人不能坐在相邻的位置,一个圆桌可以很形象地看作是一个环,也就是说我们两 ...

  3. 被英特尔“冷落”的Knights Mill 悄然发布了

    近日,英特尔"Knights Mill"的低调发布了.至于为什么会低调,原因很简单,英特尔Xeon Phi x205芯片是一个生态性质更强的产品. 英特尔至强Phi x205 Kn ...

  4. Game of Hyper Knights LightOJ - 1315

    Game of Hyper Knights LightOJ - 1315 六个方向搜索 不能打表,打表很麻烦,记录不了旧状态 #include <bits/stdc++.h> #defin ...

  5. 【POJ - 2942】Knights of the Round Table(点双连通分量,二分图判断奇环奇圈)

    题干: Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in dist ...

  6. LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...

  7. POJ-2488 A Knights Journey-深度优先搜索DFS

    POJ-2488 A Knights Journey-深度优先搜索 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37974 A ...

  8. bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士(BFS)

    1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 416  Solved: 26 ...

  9. 如果圆桌骑士有特殊情况(Knights of the Round Table)

    题目描述 Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in dis ...

  10. POJ2942 UVA1364 Knights of the Round Table 圆桌骑士

    POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...

最新文章

  1. Ios中checkBox
  2. python培训班排行榜-深圳python培训机构排行榜
  3. 外伤导致色觉异常型复视(球后视神经炎+视神经萎缩)
  4. 2020.8.26广联达笔试第二题——魔法师四种元素平衡(Python)
  5. Linux-存储服务之NFS
  6. 其实在直播平台买东西的客户最愚蠢
  7. php检测数组值的长度,php检测数组长度的函数sizeof count_PHP教程
  8. 边缘计算ARMNN(一):ARM-ARMNN-ComputeLibrary rk3399 编译配置
  9. 工作中JS语法的小整理以及vue小知识的收纳
  10. Linux——常用文件管理命令(必会)
  11. HDU_1874_畅通工程续_最短路问题
  12. 拓扑排序算法原理及Java代码实现
  13. 图像的二阶导数为何对噪声尤为敏感-----图像二阶导数的本质
  14. 计算机毕业设计之java+ssm校园外卖配送系统
  15. 求一个乘法算式采用几进制
  16. 配置 Tapestry
  17. shiro-基本原理和逻辑配置
  18. 英特尔发布新的 “Xe” 内核图形驱动程序
  19. opencv的基本数据类型CvPoint,CvSize,CvRect和CvScalar
  20. Java编程经典书籍

热门文章

  1. 4W+字的Java并发面渣逆袭宝典?扶我起来,我还可以肝
  2. C++报错无效的预处理命令include_Chapter2:从C/C++的编译原理说起
  3. 强大的Excel 的 VBA
  4. 通信网络价值的魔鬼 梅特卡夫定律错在何处
  5. 软考网络工程师上午考试知识点总结整理
  6. 直链下载Windows和office安装包,这个网站值得收藏
  7. 【开源免费】微软官方推荐的桌面小工具
  8. 海思篇之开机logo的加载(Hi3531a命令版)
  9. Linux中kil命令和pkill命令的区别
  10. 最大公共子串----快手校招真题