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 ...
随机推荐
- 关闭浏览器 清除session
捕获关闭浏览器的事件 关于关闭IE清空session的总结 Session过期会清楚session 还可以手动清除session实现关闭浏览器时清除session的方法
- 如何安全使用公共Wifi,防止信息泄露?
购物中心,机场,餐厅,咖啡馆,图书馆,公共交通,酒店客房均提供免费无线网络连接,这些网络每天被数百万人使用.然而,大多数人没有意识到的是,免费的公共Wi-Fi并不安全.即使需要密码才能登录,这并不一定 ...
- HDU-3015 Disharmony Trees [数状数组]
Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...
- 计蒜客 时间复杂度 (模拟) & 洛谷 P3952 时间复杂度
链接 : Here! 思路 : 这是一道大模拟, 区分好情况就没问题了 循环构成部分 : $F , x , i , j$ 和 $E$ , 需要注意的是 $i , j$, - 分析 $i, j$ 的情况 ...
- Git学习笔记(1)
1.安装Git: 在linux下安装:yum install git 其他系统安装在这里略去~~~ 安装完成后,需要设置一下,在命令行输入以下命令: [root@xwq ~]# git config ...
- 深入了解Spring中的容器
1.创建Bean的3种方式 1.1使用构造器创建bean实例 这是最常见的方式,如果不采用构造注入,bean类需要有默认构造函数.如果采用构造注入,则需要配置xml文件的<constructor ...
- 实战:一、使用mongo做一个注册的小demo
思路:1.使用mongoose 进行 数据库的链接 2.使用Schema来进行传输字段的定义 3.安装koa-router进行数据处理4.安装koa-bodyparser 进行post数据交互5.解决 ...
- Civil3D二次开发常见问题总结
Civil3D二次开发常见问题总结 AutoCAD命令提示"未知命令**--"的原因:在Initialize方法内报出异常就会导致这种情况.O__O"-(或是少加了dll ...
- LaTeX soul包
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50774955 详细的soul包的官方P ...
- 洛谷——P2657 低头一族
https://www.luogu.org/problem/show?pid=2657 题目描述 一群青年人排成一队,用手机互相聊天. 每个人的手机有一个信号接收指标,第i个人的接收指标设为v[i]. ...