zoukankan      html  css  js  c++  java
  • Kattis

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

    includegraphics[width=0.7	extwidth ]{chess}
    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 1Sample Output 1
    *.......
    ..*.....
    ....*...
    ......*.
    .*......
    .......*
    .....*..
    ...*....
    
    invalid
    
    Sample Input 2Sample Output 2
    *.......
    ......*.
    ....*...
    .......*
    .*......
    ...*....
    .....*..
    ..*.....
    
    valid
    

    题意

    检查一下给出的棋盘是否是八个皇后都无法攻击到任何人的,唯一一个坑点大概就是皇后一定要8个。。。

    代码

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    int main() {
        char ch[9][9],i,j;
        int xx[100];
        int yy[100];
        while (~scanf("%s",ch[0]))
        {
            for (i = 1; i < 8; i++)
            {
                scanf("%s", ch[i]);
            }
            int c = 0;
            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {
                    if (ch[i][j] == '*')
                    {
                        xx[c] = i;
                        yy[c] = j;
                        c++;
                    }
                }
            }
            int flag = 0;
            if (c != 8)
            {
                flag = 1;
            }
            if (c == 8)
            {
                for (i = 0; i < 8; i++)
                {
                    for (j = i + 1; j < 8; j++)
                    {
                        if (xx[i] == xx[j] || yy[i] == yy[j] || fabs(xx[i] - xx[j]) == fabs(yy[i] - yy[j]))
                        {
                            flag = 1;
                        }
                    }
                    if (flag)break;
                }
            }
            puts(flag ? "invalid" : "valid");
        }
        return 0;
    }
  • 相关阅读:
    第3章 Java数组(上): 一维数组和二维数组
    第二章 JavaScript总结(下)
    第二章 JavaScript案例(中)
    第二章 JavaScript文档(上)
    第一章 Html+Css使用总结(下)
    第一章 HTML+CSS(中)
    div布局
    阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:6. 设备事件上报
    阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:5. 设置设备属性
    阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:4.1 上报位置信息
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6284123.html
Copyright © 2011-2022 走看看