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×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 <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 ";
      else
    cout<<"valid ";
      return 0; }
  • 相关阅读:
    一道简单的递推题(快速幂+矩阵乘法优化+滚动数组)
    玲珑OJ 1129
    (转)Python函数式编程——map()、reduce()
    在windows中安装两个不同版本的Python
    Python 安装 pytesser 处理验证码出现的问题
    Python爬虫之HDU提交数据
    Python SGMLParser 的1个BUG??
    CF622F:The Sum of the k-th Powers
    LuoGuP3321:[SDOI2015]序列统计
    卡马克开方膜拜笔记
  • 原文地址:https://www.cnblogs.com/Asimple/p/6744635.html
Copyright © 2011-2022 走看看