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 ...
随机推荐
- 主从同步工作过程?(binlog日志)
在从数据库服务器的/var/lib/mysql/master.info 记录连接主数据库服务器信息文件mail-relay-bin.XXXXXX 中继日志文件(记录SQL)mail-relay ...
- NP是什么意思?
举例叙述(转自百度百科,纯为学习笔记) 编辑 在一个周六的晚上,你参加了一个盛大的晚会.由于感到局促不安,你想知道这一大厅中是否有你已经认识的人.你的主人向你提议说,你一定认识那位正在甜点盘附近角落的 ...
- Spring 静态工厂实例
直接上代码,看注释. 创建实体类: package com.spring.classs; public class Test { private String name; private ...
- MNIST机器学习数据集
介绍 在学习机器学习的时候,首当其冲的就是准备一份通用的数据集,方便与其他的算法进行比较.在这里,我写了一个用于加载MNIST数据集的方法,并将其进行封装,主要用于将MNIST数据集转换成numpy. ...
- MAVEN项目的搭建
MAVEN能为我们做什么? 1.Jar的声明式依赖性管理 2.项目的自动构建 搭建流程 环境配置 http://maven.apache.org/download.html 下载最新版本Maven 3 ...
- mybatis入门截图四(订单商品数据模型 一对一,一对多,多对多)
--------------------------------- 一对一查询 查询订单信息,关联查询创建订单的用户信息 1.高级映射-一对一查询-使用resultType 2.高级映射-一对一查询- ...
- mysql修改配置文件
在Apache, PHP, MySQL的体系架构中,MySQL对于性能的影响最大,也是关键的核心部分.对于Discuz!论坛程序也是如此,MySQL的设置是否合理优化,直接影响到论坛的速度和承载量!同 ...
- SQL优化-标量子查询(数据仓库设计的隐患-标量子查询)
项目数据库集群出现了大规模节点宕机问题.经查询,问题在于几张表被锁.主要问题在于近期得几个项目在数据库SQL编写时大量使用了标量子查询. 为确定为题确实是由于数据表访问量超过单节点限制,做了一些测试. ...
- 使用动态代理实现dao接口
使用动态代理实现dao接口的实现类 MyBatis允许只声明一个dao接口,而无需写dao实现类的方式实现数据库操作.前提是必须保证Mapper文件中的<mapper>标签的namespa ...
- Routh-Hurwitz Criterion 劳斯稳定判据
Routh-Hurwitz Criterion 为什么仅仅要有一个极点在右半平面,那么系统就不会稳定? 比如H(s) =( 1/(s+1) ) * ( 1/(s+3) ) * ( 1/(s-2) ) ...