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. js如何让高度和宽度保持一致_如何实现一个下载进度条/播放进度条
  2. Codevs 1005 生日礼物
  3. R-FCN每秒30帧实时检测3000类物体,马里兰大学Larry Davis组最新目标检测工作
  4. 如何给Wordpress安装插件
  5. 使用Vue2.x高效还原美团外卖项目
  6. Mysql改写子查询SQL优化案例
  7. Ros_Topic通信方式
  8. 【智力题】拿硬币(数数字)、游戏
  9. SpringBoot整合tkMybatis基础教程
  10. 线性代数在计算机中的应用论文,论线性代数在现实生活中的应用(结课论文)
  11. 算法题_寻找最大连通区域
  12. RabbitMQ 使用的AMQP模型解析
  13. nacos注册服务的时候报错server is DOWN now, please try again later!
  14. flutter Text文本,中英文混合,或则英文单词自动换行处理
  15. Cornfields
  16. 晨曦记账本记账,计算每个账户的某时间段账目
  17. lucksheet表格操作插件的使用
  18. PyQt5基础知识大全
  19. 一般将来时语法课教案_英语一般将来时被动语态公开课教案
  20. 你不知道的CRM销售漏斗管理的真正价值

热门文章

  1. 电脑推荐-暂时就写这么多
  2. 整理总结:机器学习常用九大算法
  3. 微信小程序包体积过大解决方法
  4. 马斯克一夜之间裁掉50%员工!推特CEO上任即成“灭霸”,幸存员工:快吐了
  5. 社招进京东真的很难吗?小伙毕业2年有幸获得京东内推,3面技术官直接拿下offer!
  6. 关于机器学习系统设计的一些思路
  7. 关于我发表了TalentOrg的面试文章而被官方的人找上门
  8. 小学计算机课标教学大纲的依据,《中小学课程标准与教学大纲有什么区别和联系》...
  9. 【论文阅读】D19-1435——GEC问题解决的一种方法:PIE架构
  10. 数据库之mysql事务原理分析与锁机制 详解