8 Queens Chess Problem UVA - 750 (很久之前WA,,然后 现在改了一点之后 PE,,又改了一会A了)

22 篇文章 0 订阅

PEIn chess it is possible to place eight queens on the board so that no one queen can be taken by any other. Write a program that will determine all such possible arrangements for eight queens given the initial position of one of the queens. Do not attempt to write a program which evaluates every possible 8 configuration of 8 queens placed on the board. This would require 88 evaluations and would bring the system to its knees. There will be a reasonable run time constraint placed on your program. Input The first line of the input contains the number of datasets, and it’s followed by a blank line. Each dataset contains a pair of positive integers separated by a single space. The numbers represent the square on which one of the eight queens must be positioned. A valid square will be represented; it will not be necessary to validate the input. To standardize our notation, assume that the upper leftmost corner of the board is position (1,1). Rows run horizontally and the top row is row 1. Columns are vertical and column 1 is the left-most column. Any reference to a square is by row then column; thus square (4,6) means row 4, column 6. Each dataset is separated by a blank line. Output Output for each dataset will consist of a one-line-per-solution representation. Each solution will be sequentially numbered 1 . . . N. Each solution will consist of 8 numbers. Each of the 8 numbers will be the ROW coordinate for that solution. The column coordinate will be indicated by the order in which the 8 numbers are printed. That is, the first number represents the ROW in which the queen is positioned in column 1; the second number represents the ROW in which the queen is positioned in column 2, and so on. Notes: The sample input below produces 4 solutions. The full 8×8 representation of each solution is shown below. DO NOT SUBMIT THE BOARD MATRICES AS PART OF YOUR SOLUTION! SOLUTION 1 SOLUTION 2 SOLUTION 3 SOLUTION 4 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 Submit only the one-line, 8 digit representation of each solution as described earlier. Solution #1 below indicates that there is a queen at Row 1, Column 1; Row 5, Column 2; Row 8, Column 3; Row 6, Column 4; Row 3,Column 5; ... Row 4, Column 8. Include the two lines of column headings as shown below in the sample output and print the solutions in lexicographical order. Sample Input 1 1 1 Sample Output SOLN COLUMN # 1 2 3 4 5 6 7 8 1 1 5 8 6 3 7 2 4 2 1 6 8 3 7 4 2 5 3 1 7 4 6 8 2 5 3 4 1 7 5 8 2 4 6 3

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <iomanip>
using namespace std;
int n=8;
int a[10][10] = { 0 }, b[99][9];
int t = 1;
void print()
{
    //   printf("No. %d\n", t++);
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if(a[i][j])
                b[t][i] = j+1;
            // printf("%d ", a[j][i]);
        }
        //      printf("\n");
    }
    t++;
}
bool judge(int x, int y)
{
    for(int i=0; i<x; i++)
    {//竖直方向||左上方 || 右上方
        if(a[i][y] || (y-i-1>=0 && a[x-i-1][y-i-1]) || (y+i+1<8 && a[x-i-1][y+i+1]))
            return false;
    }
    return true;
}
/*
void dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
       if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            dfs(num + 1);
            a[num][i] = 0;
        }
    }
}
*/

int dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
        if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            if(dfs(num + 1))
                return 1;
            a[num][i] = 0;
        }

    }
    return 0;
}
int main()
{


    dfs(0);
    /*
    for(int i=1; i<93; i++)
        for(int j=0; j<8; j++) {
            cout << b[i][j] << " ";
            if (j == 7)

        cout << endl;}*/

    int n, x, y;

    cin >> n;



    while(n--) {
        cin >> x >> y;
        int t = 1;
        printf("SOLN       COLUMN\n");
        printf(" #      1 2 3 4 5 6 7 8\n\n");
        int i, j = 0;
        for (i = 1; i < 93; i++) {
/*            if (j == 8) {
                j = 0;
                cout << endl;
            }
*/
            //  if(b[i][0] >= x && b[i][0] <= y)
            if (b[i][y - 1] == x) {
                cout << setw(2) << t++ << "     ";
                for (j = 0; j < 8; j++) {

                    cout << " " << b[i][j];


                }
                cout << endl;
                    //else t = 1;
            }

        }
        if(n){
            cout << endl;


        }

    }
    return 0;
}

//WA版本 ,,,WA的原因是因为理解错题意,,,以为第一个数字在x 和y之间就可以了,,重看发现x,y确定了一个坐标.......

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <iomanip>
using namespace std;
int n=8;
int a[10][10] = { 0 }, b[99][9];
int t = 1;
void print()
{
 //   printf("No. %d\n", t++);
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if(a[i][j])
                b[t][i] = j+1;
           // printf("%d ", a[j][i]);
        }
  //      printf("\n");
    }
    t++;
}
bool judge(int x, int y)
{
    for(int i=0; i<x; i++)
    {//竖直方向||左上方 || 右上方
        if(a[i][y] || (y-i-1>=0 && a[x-i-1][y-i-1]) || (y+i+1<8 && a[x-i-1][y+i+1]))
            return false;
     }
    return true;
}
/*
void dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
       if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            dfs(num + 1);
            a[num][i] = 0;
        }
    }
}
*/

int dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
        if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            if(dfs(num + 1))
                return 1;
            a[num][i] = 0;
        }

    }
return 0;
}
int main()
{


    dfs(0);
    /*
    for(int i=1; i<93; i++)
        for(int j=0; j<8; j++) {
            cout << b[i][j] << " ";
            if (j == 7)

        cout << endl;}*/

    int n, x, y;

    cin >> n;

    printf("SOLN       COLUMN\n");
    printf(" #      1 2 3 4 5 6 7 8\n\n");

    while(n--)
    {
        cin >> x >> y;
        int t = 1;
        for(int i=1; i<93; i++) {

            if(b[i][0] >= x && b[i][0] <= y)
            {
                cout << setw(2) << t++ << "     ";
                for (int j = 0; j < 8; j++) {

                cout << " " << b[i][j];
                if (j == 7)

                    cout << endl;
            }

            }else t = 1;
        }


    }
    return 0;
}

 

//PE版本


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <iomanip>
using namespace std;
int n=8;
int a[10][10] = { 0 }, b[99][9];
int t = 1;
void print()
{
    //   printf("No. %d\n", t++);
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if(a[i][j])
                b[t][i] = j+1;
            // printf("%d ", a[j][i]);
        }
        //      printf("\n");
    }
    t++;
}
bool judge(int x, int y)
{
    for(int i=0; i<x; i++)
    {//竖直方向||左上方 || 右上方
        if(a[i][y] || (y-i-1>=0 && a[x-i-1][y-i-1]) || (y+i+1<8 && a[x-i-1][y+i+1]))
            return false;
    }
    return true;
}
/*
void dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
       if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            dfs(num + 1);
            a[num][i] = 0;
        }
    }
}
*/

int dfs(int num)
{
    if (num >= 8){
        print();
    }
    for (int i = 0; i < 8; i++)
    {
        if (a[num][i]!=1&&judge(num, i))
        {
            a[num][i] = 1;
            if(dfs(num + 1))
                return 1;
            a[num][i] = 0;
        }

    }
    return 0;
}
int main()
{


    dfs(0);
    /*
    for(int i=1; i<93; i++)
        for(int j=0; j<8; j++) {
            cout << b[i][j] << " ";
            if (j == 7)

        cout << endl;}*/

    int n, x, y;

    cin >> n;



    while(n--) {
        cin >> x >> y;
        int t = 1;
        printf("SOLN       COLUMN\n");
        printf(" #      1 2 3 4 5 6 7 8\n\n");
        int i, j = 0;
        for (i = 1; i < 93; i++) {
/*            if (j == 8) {
                j = 0;
                cout << endl;
            }
*/
            //  if(b[i][0] >= x && b[i][0] <= y)
            if (b[i][y - 1] == x) {
                cout << setw(2) << t++ << "     ";
                for (j = 0; j < 8; j++) {

                    cout << " " << b[i][j];


                }
                //else t = 1;
            }
            cout << endl;
        }
        if(n){
            cout << endl;
        }

    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值