1128 N Queens Puzzle

题意:给定一串序列,判断其是否是合法的N皇后方案。

思路:本题是阅读理解题,不是真的N皇后问题。N皇后问题的合法序列要求任意两个皇后不在同一行、同一列,以及不在对角线。本题已经明确不会在同一列,故只需要判断是否在同一行和是否在对角线即可。

代码:

#include <cstdio>
#include <cstdlib>
int table[1010];//table[i]=j表示第i列,第j行

int main()
{
    int k,n;
    scanf("%d",&k);
    while(k--){
        scanf("%d",&n);
        bool flag=true;
        for(int c=1;c<=n;c++){
            scanf("%d",&table[c]);
            if(flag==false) continue;
            for(int i=1;i<c;i++){
                if(table[c]==table[i] || abs(table[c]-table[i])==c-i){//注意加绝对值
                    flag=false;
                    break;
                }
            }
        }
        printf("%s\n",flag?"YES":"NO");
    }
    return 0;
}

【补充:求解8皇后问题】

假设棋牌的方向是这样的,按列放入皇后,问总共有多少中合理的方案?

代码:

#include <cstdio>
#include <cstdlib>
const int n=8;
int table[n]={0};//table[i]=j表示第i列,第j行
int totalCnt=0;//总的方案数

//求8皇后问题,逐列放入皇后
void dfs(int x)
{
    if(x==n){
        totalCnt++;
        for(int i=0;i<n;i++)//输出方案
            printf("%d ",table[i]);
        printf("\n");
        return;
    }
    for(int y=0;y<n;y++){
        table[x]=y;//把皇后放在位置(x,y)处
        bool flag=true;
        for(int i=0;i<x;i++){//检查该位置是否合法
            if(table[i]==table[x] || abs(table[x]-table[i])==x-i) {
                flag=false;
                break;
            }
        }
        if(flag)//如果皇后放在第x列是合理的,就进入下一列
            dfs(x+1);
    }
}

int main()
{
    dfs(0);
    printf("total solutions:%d\n",totalCnt);
    return 0;
}

原文地址:https://www.cnblogs.com/kkmjy/p/9574009.html

时间: 2024-11-11 00:39:03

1128 N Queens Puzzle的相关文章

54. 八皇后问题[eight queens puzzle]

[本文链接] http://www.cnblogs.com/hellogiser/p/eight-queens-puzzle.html [题目] 在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即任意两个皇后不得处在同一行.同一列或者同一对角斜线上.下图中的每个黑色格子表示一个皇后,这就是一种符合条件的摆放方法.请求出总共有多少种摆法. [分析] 之前博文28.字符串的排列[StringPermutation]介绍过字符串的全排列,八皇后问题也可以转换为全排列问题. 由于八个皇后的任意两个不

poj3239 Solution to the n Queens Puzzle (n皇后问题)

Solution to the n Queens Puzzle Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3494   Accepted: 1285   Special Judge Description The eight queens puzzle is the problem of putting eight chess queens on an 8 × 8 chessboard such that none

Poj 3239 Solution to the n Queens Puzzle

1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3459   Accepted: 1273   Special Judge Description The eight queens puzzle is the problem of putting eight

PAT A1128 N Queens Puzzle (20 分)

The "eight queens puzzle" is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

递归和回溯(recursion and backtracking)

1. Concept Backtracking is a refinement of the brute force approach, which systematically searches for a solution to a problem among all available options. It does so by assuming that the solutions are represented by vectors (v1, ..., vm) of values a

[Leetcode] n queens n皇后问题

The n-queens puzzle is the problem of placing n queens on an n×nchessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of t

1128: 零起点学算法35——再求多项式(含浮点)

1128: 零起点学算法35--再求多项式(含浮点) Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 2141  Accepted: 1002[Submit][Status][Web Board] Description 输入一个整数n,计算 1+1/(1-3)+1/(1-3+5)+...+1/(1-3+5-...+2n-1)的值 Input 输入一个整数n(多组数据) Output 出1+1/(1

UVa - 227 - Puzzle

给空格子上下左右的互换操作,问最后是怎样的 注意一行的最后一个若是空格,需要自己加注意读取时 操作可能分好多行,一定要读取到 0 为止 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 char map[50][50],op[1000],c,tmp; 5 int k,t,x,y,cnt; 6 bool flag; 7 void fuc() 8 { 9 flag=1; 10 for(int i=0;