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.
![\includegraphics[width=0.7\textwidth ]{chess}](https://open.kattis.com/problems/8queens/file/statement/en/img-0001.png)
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 IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理