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.

Figure 1: Queen movement (left), invalid solution (center), valid solution (right).

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的更多相关文章

  1. Kattis之旅——Eight Queens

    In the game of chess, the queen is a powerful piece. It can attack by moving any number of spaces in ...

  2. Jeff Somers's N Queens Solutions 最快的n皇后算法

    /* Jeff Somers * * Copyright (c) 2002 * * jsomers@alumni.williams.edu * or * allagash98@yahoo.com * ...

  3. [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 ...

  4. lintcode 中等题:N Queens II N皇后问题 II

    题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...

  5. lintcode 中等题:N Queens N皇后问题

    题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...

  6. Codeforces Gym 100650D Queens, Knights and Pawns 暴力

    Problem D: Queens, Knights and PawnsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu ...

  7. 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 ...

  8. Pat1128:N Queens Puzzle

    1128. N Queens Puzzle (20) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The & ...

  9. PAT 1128 N Queens Puzzle

    1128 N Queens Puzzle (20 分)   The "eight queens puzzle" is the problem of placing eight ch ...

随机推荐

  1. 面试官:精通 Mybatis?请回答下这几个问题

    点关注,不迷路:持续更新Java架构相关技术及资讯热文!!! Mybatis是现在非常主流的持久层框架,虽然平时用的多,但是其中几个细节的问题,能说出个所以然来不? 一.最常见,参数中 #{} 和 $ ...

  2. 《Python数据分析与挖掘实战》-第四章-数据预处理

    点我看原版

  3. C++基础 (8) 第八天 数组指针 模板指针 C语言中的多态 模板函数

    1昨日回顾 2 多态的练习-圆的图形 3多态的练习-程序员薪资 4员工管理案例-抽象类和技术员工的实现 employee.h: employee.cpp: technician.h: technici ...

  4. GDI 像素(5)

    RGB 颜色 使用 RGB 宏可以创建一个由三个整数值(R.G.B)的 COLORREF 值. COLORREF RGB( BYTE byRed, // 红色值(R) BYTE byGreen, // ...

  5. ClipboardJS实现点击复制功能

    <script src="//lib.baomitu.com/clipboard.js/1.7.1/clipboard.min.js"></script> ...

  6. CF140E New Year Garland (计数问题)

    用$m$种颜色的彩球装点$n$层的圣诞树.圣诞树的第$i$层恰由$a_{i}$个彩球串成一行,且同一层内的相邻彩球颜色不同,同时相邻两层所使用彩球的颜色集合不 同.求有多少种装点方案,答案对$p$取模 ...

  7. python第六周:面向对象编程

    面向对象编程: 世界万物,皆可分类 世界万物,对象 只要是对象,就肯定属于某种品类 只要是对象,就肯定有属性 oop编程利用"类"和"对象"来创建各种模型来实现 ...

  8. VS2015使用C++编写DLL,并在C#环境中调用【转】

    说明:本文是本人在学习封装DLL时看到的,觉得说得很清楚,为了防止忘记,特意复制下来学习用,原文链接:https://blog.csdn.net/songyi160/article/details/5 ...

  9. 0111mysql如何选择Join的顺序

    本文通过一个案例来看看MySQL优化器如何选择索引和JOIN顺序.表结构和数据准备参考本文最后部分"测试环境".这里主要介绍MySQL优化器的主要执行流程,而不是介绍一个优化器的各 ...

  10. 使用jekyll配置一个自己的blog

    使用coding.net上提供的pages服务来配置一个自己的站点 提示:下载这些软件,最好能FQ,有些链接是国外的,淘宝的ruby镜像已经不提供服务了 1. 安装Ruby 2. 安装Rubygems ...