Kattis - Eight Queens
Eight Queens
In the game of chess, the queen is a powerful piece. It can attack by moving any number of spaces in its current row, in its column or diagonally.
In the eight queens puzzle, eight queens must be placed on a standard $8 \times 8$ chess board so that no queen can attack another. The center figure below shows an invalid solution; two queens can attack each other diagonally. The figure on the right shows a valid solution. Given a description of a chess board, your job is to determine whether or not it represents a valid solution to the eight queens puzzle.
Input
Input will contain a description of a single chess board, given as eight lines of eight characters each. Input lines will consist of only the characters ‘.’ and ‘*’. The ‘.’ character represents an empty space on the board, and the ‘*’ character represents a queen.
Output
Print a single line of output. Print the word “valid” if the given chess board is a valid solution to the eight queens problem. Otherwise, print “invalid”.
| Sample Input 1 | Sample Output 1 |
|---|---|
*....... |
invalid |
| Sample Input 2 | Sample Output 2 |
|---|---|
*....... |
valid |
题意
检查一下给出的棋盘是否是八个皇后都无法攻击到任何人的,唯一一个坑点大概就是皇后一定要8个。。。
代码
#include<stdio.h>
#include<string.h>
#include<math.h>
int main() {
char ch[][],i,j;
int xx[];
int yy[];
while (~scanf("%s",ch[]))
{
for (i = ; i < ; i++)
{
scanf("%s", ch[i]);
}
int c = ;
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
if (ch[i][j] == '*')
{
xx[c] = i;
yy[c] = j;
c++;
}
}
}
int flag = ;
if (c != )
{
flag = ;
}
if (c == )
{
for (i = ; i < ; i++)
{
for (j = i + ; j < ; j++)
{
if (xx[i] == xx[j] || yy[i] == yy[j] || fabs(xx[i] - xx[j]) == fabs(yy[i] - yy[j]))
{
flag = ;
}
}
if (flag)break;
}
}
puts(flag ? "invalid" : "valid");
}
return ;
}
Kattis - Eight Queens的更多相关文章
- Kattis之旅——Eight Queens
In the game of chess, the queen is a powerful piece. It can attack by moving any number of spaces in ...
- Jeff Somers's N Queens Solutions 最快的n皇后算法
/* Jeff Somers * * Copyright (c) 2002 * * jsomers@alumni.williams.edu * or * allagash98@yahoo.com * ...
- [CareerCup] 9.9 Eight Queens 八皇后问题
9.9 Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board so that non ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- lintcode 中等题:N Queens N皇后问题
题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...
- Codeforces Gym 100650D Queens, Knights and Pawns 暴力
Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...
- Poj 3239 Solution to the n Queens Puzzle
1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS ...
- Pat1128:N Queens Puzzle
1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...
- PAT 1128 N Queens Puzzle
1128 N Queens Puzzle (20 分) The "eight queens puzzle" is the problem of placing eight ch ...
随机推荐
- NP是什么意思?
举例叙述(转自百度百科,纯为学习笔记) 编辑 在一个周六的晚上,你参加了一个盛大的晚会.由于感到局促不安,你想知道这一大厅中是否有你已经认识的人.你的主人向你提议说,你一定认识那位正在甜点盘附近角落的 ...
- 【数据分析学习】016-numpy数据结构
通常对数据的矩阵进行操作,就用numpy操作,打开txt文件 使用help()去查询文档,可以看到官方的注释 import numpy path = r'F:\数据分析专用\数据分析与机器学习\wor ...
- java中String和int的互相转化
1. String 转 int 方式1:Integer.parseInt(); 方式2: Integer.valueOf(myStr).intValue(); 2. int 转String 方式1: ...
- yii2.0 表单小部件常用的默认选中
下面自己总结了一下表单小部件中经常使用到的一些默认选中 直接在视图代码里面说明啦 <?php //这个表头就不多说了 use yii\helpers\Html; use yii\widgets\ ...
- BA-siemens-insight_ppcl_adapts函数用法
adapts函数是比pid调节性更好的自适应调节算法,比pid有更好的稳定性,具有震荡小.调节过程快.平稳等特点,函数的用法如下:
- Spring 注解学习笔记
声明Bean的注解: @Component : 组件,没有明确的角色 @Service : 在业务逻辑层(service层)使用 @Repository : 在数据访问层(dao层)使用. @Cont ...
- java中继承关系学习小结
继承:把多个类中同样的内容提取出来.定义到一个类中,其它类仅仅须要继承该类.就能够使用该类公开的属性和公开的方法. 继承的优点:提高代码的复用性.提高代码的可维护性.让类与类之间产生关系,是多态存 ...
- UVALive 6663 Count the Regions 离散+bfs染色_(:зゝ∠)_
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4675">点击打开链接 gg.. ...
- 2015.05.15,外语,学习笔记-《Word Power Made Easy》 01 “如何讨论人格特点”
2015.03.17,外语,读书笔记-<Word Power Made Easy> 01 “如何讨论人格特点”学习笔记 SESSIONS 1 本来这些章节都是在一两年前学习的,现在趁给友人 ...
- Common webpart properties in kentico
https://devnet.kentico.com/docs/7_0/devguide/index.html?common_web_part_properties.htm HTML Envelope ...