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 ...
随机推荐
- vc++如何创建程序-析构函数01
#include<iostream.h>class Point{public: int x; int y; Point() { x=0; y=0; }//构造函数是用来创建函数本身,那么, ...
- C++基础 (8) 第八天 数组指针 模板指针 C语言中的多态 模板函数
1昨日回顾 2 多态的练习-圆的图形 3多态的练习-程序员薪资 4员工管理案例-抽象类和技术员工的实现 employee.h: employee.cpp: technician.h: technici ...
- PART 5: INTEGRATING SPRING SECURITY WITH SPRING BOOT WEB
转自:http://justinrodenbostel.com/2014/05/30/part-5-integrating-spring-security-with-spring-boot-web/ ...
- Webpack 学习记录之概念
1 什么是webpack webpack是一个模块打包器,可以递归的构建一个依赖关系图,其中包含每个程序需要的每个模块,然后将所有模块打包成一个或多个bundle.他和其他的工具最大的不同在于他支持c ...
- POJ Pseudoprime numbers( Miller-Rabin素数测试 )
链接:传送门 题意:题目给出费马小定理:Fermat's theorem states that for any prime number p and for any integer a > 1 ...
- Ansible常见问题处理
1.ansible all -m ping报错,信息如下: [WARNING]: log file at /var/log/ansible.log is not writeable and we ca ...
- Charles抓包工具抓取HTTS请求
1. 移动端(手机)配置证书 1.1 进入Charles,点击Help,选择SSL Proxying --> 选择安装手机证书 1.2 在Charles弹框提示中,按照提示内容,进入手机进入下图 ...
- 关于安卓sdk开发环境的的更新-Android SDK下载和更新失败
由于特别原因,我们更新安卓sdk时不会成功.解决办法: 修改C:\Windows\System32\drivers\etc\hosts文件.添加一行:74.125.237.1 dl-ssl ...
- JAVAEE网上商城项目总结
发送邮件实现(使用QQ邮箱发送到指定邮箱) 需要的jar 邮件发送类代码: package util; import java.util.Properties; import javax.mail.A ...
- 公司组织oracle培训的理解
oracle执行机制 1.客户端发送一条sql给oracle服务器,oracle会看这条sql的执行计划是否存在缓存 如果存在则直接运行,如果不存在执行第二步. 2.如果不存在缓存 则会 进行语法检 ...