UVA 750 八皇后·(2)+ 预处理+递归+棋盘问题

1 题意,题目描述里似乎没说清楚输入输出格式。

2 分析。

学习:用一个行数组、左对角线数组、右对角线数组,分别表示某一行、某一条左对角线个、某一条右对角线是否被占用。用于高效的处理,辨别是否在同一行、同一列、同一对角线的情况!

3

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int cnt=0;
int p[110][9];// 第一维是第几种方法,第二维是第几列,值是第几行
bool row[9]={0},left_[15]={0},right_[15]={0};
int column[9];
void Fun(int c){
    if(c==9){
        for(int i=1;i<=8;i++)
            p[cnt][i]=column[i];
        cnt++;
        return ;
    }
    for(int r=1;r<=8;r++){      // 遍历每一行
        int ld=(c-r)+7; //控制在0~14
        int rd=(c+r)-2; //同上
        if(!row[r]&&!left_[ld]&&!right_[rd]){
            row[r]=left_[ld]=right_[rd]=1;
            column[c]=r; //该函数是0~7,题目棋盘是1~8
            Fun(c+1);
            row[r]=left_[ld]=right_[rd]=0;
        }
    }
}
int main()
{
    int s_x,s_y;
    Fun(1);
    int t;
    while(~scanf("%d",&t))
    while(t--){
        scanf("%d%d",&s_x,&s_y);
        printf("SOLN       COLUMN\n");
        printf(" #      1 2 3 4 5 6 7 8\n\n");
        int ans=1;
        for(int i=0;i<cnt;i++){
            if(p[i][s_y]==s_x){
                 printf("%2d     ",ans++);
                for(int j=1;j<=8;j++){
                    cout<<" "<<p[i][j];
                }
                cout<<endl;
            }
        }
        if(t) cout<<endl;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值