Gym 100947B- 8 Queens, Again!!

Description
standard input/output
Announcement
Statements
Yaaaay, Haven’t you heard the news? Bakaloria results are out! And Reem had very good grades. Finally she can go to IT college and pursue her childhood dream of becoming a great programmer.

Reem is very excited about her college, she already started learning programming. Today she was learning about a very well known problem, the 8-Queens problem (you never saw that coming, didn’t you?) she has wrote a program to generate all the possible situations of the queens, but as a novice programmer she found it hard to check whether if these situations are valid. As usual you come to her rescue and offer to help with this hard task!

Now you are stuck with this task: Given the positions of 8 queens on a regular chess board, determine if this situation is valid.

A valid situation is a configuration of the 8 queens where no queen can strike any other queen.

On a standard 8 × 8 chess board, a queen can strike any other queen on it’s row, column, or two diagonals.

Input
The first line of input has one integer T the number of test cases your program must process.

Each of the next T lines contains the positions of 8 queens. A position of a queen is described as one character [A - H] (the column of the queen), followed by one number [1 - 8] (the row of the queen)

For example: “A4” represents a queen at the first column and the fourth row.

(see sample input for more details)

Output
For each test case print one line containing the word Valid if the configuration is valid, or Invalid otherwise.

Sample Input
Input
2
A1 B5 C8 D6 E3 F7 G2 H4
C3 E4 C4 E1 C4 F4 A8 G6
Output
Valid
Invalid

八皇后问题。。同行同列同对角线不行。
同对角线是两个点横纵坐标之差,之和一样。

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
struct Point
{
    int x,y;
}p[10];
int check(int a,int b)
{
    if(p[a].x==p[b].x)
        return 1;
    if(p[a].y==p[b].y)
        return 1;
    if(p[a].x-p[a].y==p[b].x-p[b].y)
        return 1;
    if(p[a].x+p[a].y==p[b].x+p[b].y)
        return 1;
    return 0;
}
int main (void)
{   
    int t;
    cin>>t;
    while(t--)
    {
        char str[10];
        for(int i=0;i<8;i++)
        {
            scanf("%s",str);
            p[i].x=str[0]-'A';
            p[i].y=str[1]-'0';
        }
        int mark=1;
        for(int i=0;mark&&i<8;++i)
        {
            for(int j=i+1;mark&&j<8;++j)
            {
                if(check(i,j))
                {
                    mark=0;
                    //printf("%d %d\n",i,j);
                }
            }
        }
        if(mark)
        {
            printf("Valid\n");
        }
        else
            printf("Invalid\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值