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

<center> </center>
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 <bits/stdc++.h>
using namespace std;

struct point{
  int x,y;
};
int absolutey(int z) { if(z<0){return z*-1;} else{return z;} }
bool ceksinggung(point a, point b) { if(a.x==b.x||a.y==b.y||(a.x+a.y)==(b.x+b.y)||(a.x-a.y)==(b.x-b.y)||(a.y-a.x)==(b.y-b.x)) return true; else return false; } int main() { vector<point> vp; for(int i=0;i<8;i++) { string s; cin>>s; for(int j=0;j<s.length();j++) { if(s.substr(j,1)=="*") { point temp; temp.x=j+1; temp.y=i+1; vp.push_back(temp); } } } bool singgung=false; for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { if(i!=j) { singgung=singgung||ceksinggung(vp[i],vp[j]); } } }
  
if(singgung)cout<<"invalid\n";
  else
cout<<"valid\n";
  return 0; }

 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# AI面会问哪些问题? #
24847次浏览 491人参与
# 中国电信笔试 #
31080次浏览 283人参与
# 厦门银行科技岗值不值得投 #
7491次浏览 186人参与
# 你的实习产出是真实的还是包装的? #
18817次浏览 330人参与
# 如果秋招能重来,我会____ #
96691次浏览 500人参与
# 春招至今,你的战绩如何? #
59982次浏览 543人参与
# 开放七大实习专项,百度暑期实习值得冲吗 #
14147次浏览 209人参与
# i人适合做什么工作 #
36914次浏览 124人参与
# 我是面试官,请用一句话让我破防 #
79511次浏览 219人参与
# 哪些公司真双非友好? #
69200次浏览 287人参与
# 金三银四,你的春招进行到哪个阶段了? #
21567次浏览 277人参与
# 找AI工作可以去哪些公司? #
7673次浏览 186人参与
# 从事AI岗需要掌握哪些技术栈? #
7676次浏览 251人参与
# 投递几十家公司,到现在0offer,大家都一样吗 #
339915次浏览 2165人参与
# 面试尴尬现场 #
220755次浏览 861人参与
# 五一之后,实习真的很难找吗? #
102797次浏览 584人参与
# 你做过最难的笔试是哪家公司 #
30108次浏览 193人参与
# 你小时候最想从事什么职业 #
159840次浏览 2072人参与
# 应届生第一份工资要多少合适 #
20483次浏览 84人参与
# 阿里笔试 #
176460次浏览 1302人参与
# 一张图晒出你司的标语 #
3821次浏览 72人参与
# 面试被问期望薪资时该如何回答 #
382459次浏览 2163人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务