Nine Knights
Nine Knights
时间限制: 1 Sec 内存限制: 128 MB
提交: 155 解决: 46
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
![](http://exam.upc.edu.cn/upload/image/20171204/20171204153115_51036.jpg)
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.
![](http://exam.upc.edu.cn/upload/image/20171204/20171204153127_64901.jpg)
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.
输入
输出
样例输入
...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相关推荐
- Codeforces Gym100812 L. Knights without Fear and Reproach-扩展欧几里得(exgcd)
补一篇以前的扩展欧几里得的题,发现以前写错了竟然也过了,可能数据水??? 这个题还是很有意思的,和队友吵了两天,一边吵一边发现问题??? L. Knights without Fear and Rep ...
- UVA1364 Knights of the Round Table(双连通分量、二分图染色,超详细解释)
整理的算法模板合集: ACM模板 UVA1364 Knights of the Round Table 题目中要求互相有憎恨关系的人不能坐在相邻的位置,一个圆桌可以很形象地看作是一个环,也就是说我们两 ...
- 被英特尔“冷落”的Knights Mill 悄然发布了
近日,英特尔"Knights Mill"的低调发布了.至于为什么会低调,原因很简单,英特尔Xeon Phi x205芯片是一个生态性质更强的产品. 英特尔至强Phi x205 Kn ...
- Game of Hyper Knights LightOJ - 1315
Game of Hyper Knights LightOJ - 1315 六个方向搜索 不能打表,打表很麻烦,记录不了旧状态 #include <bits/stdc++.h> #defin ...
- 【POJ - 2942】Knights of the Round Table(点双连通分量,二分图判断奇环奇圈)
题干: Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in dist ...
- LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...
- POJ-2488 A Knights Journey-深度优先搜索DFS
POJ-2488 A Knights Journey-深度优先搜索 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37974 A ...
- 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 ...
- 如果圆桌骑士有特殊情况(Knights of the Round Table)
题目描述 Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in dis ...
- POJ2942 UVA1364 Knights of the Round Table 圆桌骑士
POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...
最新文章
- Ios中checkBox
- python培训班排行榜-深圳python培训机构排行榜
- 外伤导致色觉异常型复视(球后视神经炎+视神经萎缩)
- 2020.8.26广联达笔试第二题——魔法师四种元素平衡(Python)
- Linux-存储服务之NFS
- 其实在直播平台买东西的客户最愚蠢
- php检测数组值的长度,php检测数组长度的函数sizeof count_PHP教程
- 边缘计算ARMNN(一):ARM-ARMNN-ComputeLibrary rk3399 编译配置
- 工作中JS语法的小整理以及vue小知识的收纳
- Linux——常用文件管理命令(必会)
- HDU_1874_畅通工程续_最短路问题
- 拓扑排序算法原理及Java代码实现
- 图像的二阶导数为何对噪声尤为敏感-----图像二阶导数的本质
- 计算机毕业设计之java+ssm校园外卖配送系统
- 求一个乘法算式采用几进制
- 配置 Tapestry
- shiro-基本原理和逻辑配置
- 英特尔发布新的 “Xe” 内核图形驱动程序
- opencv的基本数据类型CvPoint,CvSize,CvRect和CvScalar
- Java编程经典书籍