USACO 6.4.1 The Primes 搜索剪枝

http://train.usaco.org/usacoprob2?a=gY0nBCREMEt&S=prime3

题目大意:往一个5*5的方阵填数字,最左上角数字给定,要求每一行每一列两个对角线(都是从左到右看)是一个五位素数,且的五位上的数字之和确定(给定)


一道令人绝望的搜索题,尝试了四个版本终于过了


虽然搜索题给人的感觉往往是麻烦又没什么意义,但这道题(也确实如此)让我第一次接触到了搜索树这个概念,以前只知道剪枝剪枝,但没有深究过其中的逻辑原理,通过这道题我认识到了选择正确的搜索顺序的重要性,通过构造一棵上疏下密的搜索树可以让剪枝操作发挥更巨大的作用,这也是思考搜索算法时的基本


第一版:枚举每一个点加上一些简单的剪枝,有脊髓想都知道肯定过不了

第二版:开始参考了网上的方法:http://blog.youkuaiyun.com/jasison/article/details/24056575

主要是:①事先获得所有素数 ②整行整列枚举 ③按一定的顺序枚举 ④最后几个格子通过计算得到

主要的学问就在这个顺序这里,我先是按第一行(首位确定),第一列(首位确定且不含零),最后一列(不含偶数),左下对角线的顺序枚举,结果发现剩下了9个格子!而且要枚举其中两个才能填充完,所以我又枚举了最后一行碰碰运气,果不其然T了

    for(int i = 0; i < super_prime[table[0]].size(); ++i){
        fill_row(1, super_prime[table[0]][i]);
        for(int j = 0; j < super_prime[table[0]].size(); ++j){
            if(contain_zero(super_prime[table[0]][j])) continue;
            fill_col(1, super_prime[table[0]][j]);
            for(int k = 0; k < super_prime[table[4]].size(); ++k){
                if(!all_odd(super_prime[table[4]][k])) continue;
                fill_col(5, super_prime[table[4]][k]);
                for(int m = 0; m < super_prime[table[20]].size(); ++m){
                    if(super_prime[table[20]][m]%10 != table[4]) continue;
                    fill_down_up(super_prime[table[20]][m]);
                    for(int n = 0; n < super_prime[table[20]].size(); ++n){
                        if(super_prime[table[20]][n]%10 != table[24] || contain_zero(super_prime[table[20]][n])) continue;
                        fill_row(5, super_prime[table[20]][n]);
                        for(int p = 0; p <= 9; ++p){
                            table[6] = p;
                            table[7] = demand - table[5] - table[6] - table[8] - table[9];
                            table[11] = demand - table[1] - table[6] - table[16] - table[21];
                            table[13] = demand - table[10] - table[11] - table[12] - table[14];
                            table[17] = demand - table[2] - table[7] - table[12] - table[22];
                            table[18] = demand - table[15] - table[16] - table[17] - table[19];
                            if(table[7] < 0 || table[11] < 0 || table[13] < 0 || table[17] < 0 || table[18] < 0){
                                continue;
                            }
                            judge();
                        }
                    }
                }
            }
        }
    }


第三版:完全照着题解来,枚举了第一行,第五列,第四列,第三列,但还是T了。我觉得是因为第三第四列缺少特殊性,没法剪枝

    for(int i = 0; i < super_prime[table[0]].size(); ++i){
        if(contain_zero(super_prime[table[0]][i])) continue;
        fill_row(1, super_prime[table[0]][i]);
        for(int j = 0; j < super_prime[table[4]].size(); ++j){
            if(!all_odd(super_prime[table[4]][j])) continue;
            fill_col(5, super_prime[table[4]][j]);
            for(int k = 0; k < super_prime[table[3]].size(); ++k){
                fill_col(4, super_prime[table[3]][k]);
                for(int m = 0; m < super_prime[table[2]].size(); ++m){
                    fill_col(3, super_prime[table[2]][m]);
                    for(int n = 1; n <= 9; n += 2){
                        table[20] = n;
                        table[21] = demand-table[20]-table[22]-table[23]-table[24];
                        table[16] = demand-table[20]-table[12]-table[8]-table[4];
                        table[15] = demand-table[16]-table[17]-table[18]-table[19];
                        table[6] = demand-table[0]-table[12]-table[18]-table[24];
                        table[5] = demand-table[6]-table[7]-table[8]-table[9];
                        table[11] = demand-table[1]-table[6]-table[16]-table[21];
                        table[10] = demand-table[11]-table[12]-table[13]-table[14];
                        if(table[15] <= 0 || table[10] <= 0 || table[5] <= 0 || table[6] < 0 || table[11] < 0 || table[16] < 0 || table[21] < 0) continue;
                        judge2();
                    }
                }
            }
        }
    }

第四版:这次换成了第一行,第一列,第五列,第四列,但是在第九个点上怎么都会T。于是我去掉了原来的vector用数组代替,在判断素数时换成了i*i<=k,但无济于事。最后发现原来问题在第五列时除了不能是偶数外还不能存在5!加了这条判断之后就能过了。

for(int i = 0; i < super_prime[table[0]][siz]; ++i){
        if(contain_zero(super_prime[table[0]][i])) continue;
        fill_row(1, super_prime[table[0]][i]);
        for(int j = 0; j < super_prime[table[0]][siz]; ++j){
            if(contain_zero(super_prime[table[0]][j])) continue;
            fill_col(1, super_prime[table[0]][j]);
            for(int k = 0; k < super_prime[table[4]][siz]; ++k){
                if(!all_odd(super_prime[table[4]][k])) continue;
                fill_col(5, super_prime[table[4]][k]);
                for(int m = 0; m < super_prime[table[3]][siz]; ++m){
                    fill_col(4, super_prime[table[3]][m]);
                    for(int n = 0; n <= 9; ++n){
                        table[6] = n;
                        table[7] = demand-table[5]-table[6]-table[8]-table[9];
                        table[12] = demand-table[0]-table[6]-table[18]-table[24];
                        table[11] = demand-table[10]-table[12]-table[13]-table[14];
                        table[16] = demand-table[20]-table[12]-table[8]-table[4];
                        table[17] = demand-table[16]-table[18]-table[19]-table[15];
                        table[21] = demand-table[1]-table[6]-table[11]-table[16];
                        table[22] = demand-table[2]-table[7]-table[12]-table[17];
                        if(table[21] <= 0 || table[22] <= 0 || table[16] < 0 || table[17] < 0 || table[11] < 0 || table[12] < 0 || table[7] < 0) continue;
                        judge3();
                    }
                }
            }
        }
    }


完整代码:

/*
ID: frontie1
TASK: prime3
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int siz = 10000;

struct ans
{
    int data[25];

    void getanswer(int arr[])
    {
        for(int i = 0; i < 25; ++i){
            data[i] = arr[i];
        }
        return;
    }

    void print()
    {
        for(int i = 0; i < 25; ++i){
            printf("%d", data[i]);
            if(i%5 == 4) puts("");
        }
    }

    bool operator<(const ans &obj)
    {
        for(int i = 0; i < 25; ++i){
            if(data[i] == obj.data[i]) continue;
            else return data[i] < obj.data[i];
        }
    }
};

int demand, st;
int super_prime[10][10010];
int prime[100000];
int e = 0;
bool flag;
int table[30];
bool isprime[100000];

ans output[200];
int cnt = 0;

void fill_row(int r, int num)
{
    for(int i = 1; i <= 5; ++i){
        table[5*r-i] = num % 10;
        num /= 10;
    }
}

void fill_col(int c, int num)
{
    for(int i = 4; i >= 0; --i){
        table[i*5+c-1] = num % 10;
        num /= 10;
    }
}

void fill_down_up(int num)
{
    for(int i = 0; i < 5; ++i){
        table[4+4*i] = num % 10;
        num /= 10;
    }
}

int sum(int num)
{
    int output = 0;
    while(num > 0){
        output += (num % 10);
        num /= 10;
    }
    return output;
}

bool all_odd(int num) //and have no 5
{
    while(num > 0){
        if((num%2) == 0 || (num%5) == 0) return false;
        num /= 10;
    }
    return true;
}

bool contain_zero(int num)
{
    while(num > 0){
        if(num%10 == 0) return true;
        num /= 10;
    }
    return false;
}

int calculate_num(int st, int step)
{
    int output = 0;
    for(int i = 0; i < 5; ++i){
        output = output * 10 + table[st+step*i];
    }
    return output;
}

int calculate_sum(int st, int step)
{
    int output = 0;
    for(int i = 0; i < 5; ++i){
        output += table[st+step*i];
    }
    return output;
}

void judge3()
{
    int tem;
    tem = calculate_num(0, 6);
    if(!isprime[tem]) return;
    tem = calculate_num(20, -4);
    if(!isprime[tem]) return;

    tem = calculate_num(5, 1);
    if(!isprime[tem]) return;
    tem = calculate_num(10, 1);
    if(!isprime[tem]) return;
    tem = calculate_num(15, 1);
    if(!isprime[tem]) return;
    tem = calculate_num(20, 1);
    if(!isprime[tem]) return;

    tem = calculate_num(1, 5);
    if(!isprime[tem]) return;
    tem = calculate_num(2, 5);
    if(!isprime[tem]) return;

    output[cnt].getanswer(table);
    ++cnt;
}

int main()
{
    freopen("prime3.in", "r", stdin);
    freopen("prime3.out", "w", stdout);

    cin >> demand >> table[0];

    for(int i = 2; i < 100000; ++i){
        flag = true;
        for(int k = 0; k < e && prime[k]*prime[k] <= i; ++k){
            if(i % prime[k] == 0){
                flag = false;
                break;
            }
        }
        if(flag){
            prime[e++] = i;
            if(i > 9999 && sum(i) == demand){
                super_prime[i/10000][super_prime[i/10000][siz]++] = i;
                isprime[i] = true;
            }
        }
    }

    for(int i = 0; i < super_prime[table[0]][siz]; ++i){
        if(contain_zero(super_prime[table[0]][i])) continue;
        fill_row(1, super_prime[table[0]][i]);
        for(int j = 0; j < super_prime[table[0]][siz]; ++j){
            if(contain_zero(super_prime[table[0]][j])) continue;
            fill_col(1, super_prime[table[0]][j]);
            for(int k = 0; k < super_prime[table[4]][siz]; ++k){
                if(!all_odd(super_prime[table[4]][k])) continue;
                fill_col(5, super_prime[table[4]][k]);
                for(int m = 0; m < super_prime[table[3]][siz]; ++m){
                    fill_col(4, super_prime[table[3]][m]);
                    for(int n = 0; n <= 9; ++n){
                        table[6] = n;
                        table[7] = demand-table[5]-table[6]-table[8]-table[9];
                        table[12] = demand-table[0]-table[6]-table[18]-table[24];
                        table[11] = demand-table[10]-table[12]-table[13]-table[14];
                        table[16] = demand-table[20]-table[12]-table[8]-table[4];
                        table[17] = demand-table[16]-table[18]-table[19]-table[15];
                        table[21] = demand-table[1]-table[6]-table[11]-table[16];
                        table[22] = demand-table[2]-table[7]-table[12]-table[17];
                        if(table[21] <= 0 || table[22] <= 0 || table[16] < 0 || table[17] < 0 || table[11] < 0 || table[12] < 0 || table[7] < 0) continue;
                        //cout << "down" << endl;
                        judge3();
                    }
                }
            }
        }
    }

    sort(output, output+cnt);


    for(int i = 0; i < cnt; ++i){
        output[i].print();
        if(i < cnt-1) printf("\n");
    }

    return 0;
}

备注:填充的函数其实可以写成一个,就像计算的函数那样,但我懒得改了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值