2015 AlBaath Collegiate Programming Contest B

Description

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.

Examples
input
2
A1 B5 C8 D6 E3 F7 G2 H4
C3 E4 C4 E1 C4 F4 A8 G6
output
Valid
Invalid
题意:告诉我们8个棋子的摆放位置,问我们可以这样摆放么
解法:A-Z先变换为1-8,然后根据八皇后的规定判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
struct P
{
    int x,y;
}He[100];
char s[10];
set<int>q1,q2;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=1;i<=8;i++)
        {
            cin>>s;
            He[i].x=s[0]-'A'+1;
            He[i].y=s[1]-'0';
            q1.insert(He[i].x);
            q2.insert(He[i].y);
            //cout<<He[i].x<<" "<<He[i].y<<endl;
        }
        if(q1.size()==q2.size()&&q1.size()==8)
        {
            int flag=0;
            for(int i=1;i<=8;i++)
            {
                for(int j=1;j<=8;j++)
                {
                    if(i!=j)
                    {
                        if(abs(He[i].x-He[j].x)==abs(He[i].y-He[j].y))
                           {
                               flag=1;
                               break;
                           }
                    }
                }
            }
            if(flag)
            {
                cout<<"Invalid"<<endl;
            }
            else
            {
                cout<<"Valid"<<endl;
            }
        }
        else
        {
            cout<<"Invalid"<<endl;
        }
       // cout<<q1.size()<<" "<<q2.size()<<endl;
        q1.clear(),q2.clear();
    }
    return 0;
}

  

posted @   樱花落舞  阅读(525)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示