洛谷P1219 [USACO1.5]八皇后 Checker Challenge(dfs,减枝

在这里插入图片描述
洛谷P1219 [USACO1.5]八皇后 Checker Challenge

开O(2)优化后擦边边AC。。
真是惊险刺激呢
搜索过程中,如果在两条斜边上有棋子,返回。
如果搜到底了,输出每一个棋子的位置,方案数加一并返回。
否则,对下一层的每一个格子,如果在该列没有被搜过,标记,搜,还原。

  for(a=1;a<=n;a++){
        if(k[a]==0) {
            k[a]=1;
            vis[x + 1][a] = 1;
            dfs(x + 1, a);
            vis[x + 1][a] = 0;
            k[a]=0;
        }
    }

如果在dfs中搜该列是否已经出现过,会TLE
代码是这样:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <math.h>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <cstdio>
#include <list>
#include <queue>
#include <set>
#include <unordered_map>
#define ll long long
using namespace std;

int n;
int vis[15][15];
int ans=0;

void dfs(int x,int y){
    int i;
    for(i=1;i<x;i++){//列
        if(vis[i][y]==1) return;
    }
    int a=x,b=y;
    int s=0;
    while(a>=1&&a<=n&&b<=n&&b>=1){//对角线
        s+=vis[a][b];
        a--;
        b--;
    }
    a=x+1,b=y+1;
    while(a>=1&&a<=n&&b<=n&&b>=1){
        s+=vis[a][b];
        a++;
        b++;
    }
    if(s!=1) return;
    s=0;
    a=x,b=y;
    while(a>=1&&a<=n&&b<=n&&b>=1){
        s+=vis[a][b];
        a++;
        b--;
    }
    a=x-1,b=y+1;
    while(a>=1&&a<=n&&b<=n&&b>=1){
        s+=vis[a][b];
        a--;
        b++;
    }
    if(s!=1) return;
    if(x==n) {//搜到底了
        ans++;
        if(ans<=3) {
            for (i = 1; i <= n; i++) {
                for (a = 1; a <= n; a++) {
                    if (vis[i][a] == 1) cout << a << " ";
                }
            }
            cout << endl;
        }
    }
    for(a=1;a<=n;a++){
        vis[x+1][a]=1;
        dfs(x+1,a);
        vis[x+1][a]=0;
    }
}

int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        vis[1][i]=1;
        dfs(1,i);
        vis[1][i]=0;
    }
    cout<<ans;
}

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <math.h>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <cstdio>
#include <list>
#include <queue>
#include <set>
#include <unordered_map>
#define ll long long
using namespace std;

int n;
int vis[15][15];
int ans=0;
int k[15];

void dfs(int x,int y){
    int i;
    int a=x,b=y;
    int s=0;
    while(a>=1&&a<=n&&b<=n&&b>=1){
        s+=vis[a][b];
        a--;
        b--;
    }
    a=x+1,b=y+1;
    while(a>=1&&a<=n&&b<=n&&b>=1){
        s+=vis[a][b];
        a++;
        b++;
    }
    if(s!=1) return;
    s=0;
    a=x,b=y;
    while(a>=1&&a<=n&&b<=n&&b>=1){
        s+=vis[a][b];
        a++;
        b--;
    }
    a=x-1,b=y+1;
    while(a>=1&&a<=n&&b<=n&&b>=1){
        s+=vis[a][b];
        a--;
        b++;
    }
    if(s!=1) return;
    if(x==n) {
        ans++;
        if(ans<=3) {
            for (i = 1; i <= n; i++) {
                for (a = 1; a <= n; a++) {
                    if (vis[i][a] == 1) cout << a << " ";
                }
            }
            cout << endl;
        }
    }
    for(a=1;a<=n;a++){
        if(k[a]==0) {
            k[a]=1;
            vis[x + 1][a] = 1;
            dfs(x + 1, a);
            vis[x + 1][a] = 0;
            k[a]=0;
        }
    }
}

int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        k[i]=1;
        vis[1][i]=1;
        dfs(1,i);
        vis[1][i]=0;
        k[i]=0;
    }
    cout<<ans;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值