暴力之回溯法入門(持續更新)

來源:互聯網
上載者:User

刷刷水題,入個門先:

百練oj:2754 八皇后

西電oj:1010 素數環

Poj:1011 Sticks

百練oj:2754 八皇后

  不過這道題還用存下每個可能解。

  下面是代碼:

  

#include<iostream>#include<cmath>using namespace std;const int maxn = 100;const int N = 8;int A[maxn];int Ans[maxn][10];int n,b,tot;void eight_queen(int cur){    if(cur == N+1)    {        tot++;        for(int i = 1; i <= N; i++) Ans[tot][i] = A[i];    }    for(int i = 1; i <= N; i++)    {        A[cur] = i;        int ok = 1;        for(int j = 1; j < cur; j++)        {            if(A[j]== A[cur] || A[cur]-cur == A[j]-j || A[cur] + cur == A[j] + j)            {                ok = 0;                break;            }        }        if(ok) eight_queen(cur+1);    }}int main(){    cin>>n;    tot = 0;    eight_queen(1);    while(n--)    {        cin>>b;        //cout<<tot<<endl;        for(int i = 1; i <= N; i++)        {            cout<<Ans[b][i];        }        cout<<endl;    }    return 0;}

西電oj:1010 素數環

   這道題和八皇后差不多,直接代碼:

   

#include<iostream>using namespace std;const int maxn = 21;int A[maxn];int Prime[maxn];int n,T;int is_prime(int j){    for(int i = 2; i * i <= j; i++)    {        if(j%i == 0) return 0;    }    return 1;}void solve(int cur){    if(cur == n+1)    {        for(int i = 1; i < cur; i++)        {           if(i == 1) cout<<A[i];           else cout<<" "<<A[i];        }        cout<<endl;        return;    }    for(int i = 2; i <= n; i++)    {        A[cur] = i;        int ok = 1;        for(int j = 1; j < cur; j++)            if(A[j] == i) ok = 0;        if(ok)        {            if(cur == 1) solve(cur+1);            else            {                int t = A[cur-1] + A[cur];                if(is_prime(t))                {                    if(cur < n) solve(cur+1);                    else if(cur == n && is_prime(A[cur]+A[1])) solve(cur+1);                }            }        }    }}int main(){    A[1] = 1;    T = 0;    while(cin>>n)    {        T++;        cout<<"Case "<<T<<":\n";        solve(2);        cout<<endl;    }    return 0;}

Poj:1011 Sticks

   這道題很經典,剪枝的方法要自己仔細體會。

   一、stick的長度要是總長的約數

   二、將part按從大到小排序

   三、每次要判斷還差多少長度可以拼湊出一個完整的stick

   四、一些特判,orz。。。

下面是代碼:

#include<iostream>#include<algorithm>using namespace std;const int maxn = 65;int A[maxn];bool used[maxn];int n;bool cmp(int a,int b){    return a > b;}bool ok(int re,int len,int length){    if(re == 0 && len == 0) return true;    if(len == 0) len = length;    for(int i = 0; i < n; i++)    {        if(used[i]) continue;        if(A[i] > len) continue;        if(i && !used[i-1] && A[i-1] == A[i]) continue;        used[i] = true;        if(ok(re-1,len-A[i],length)) return true;        used[i] = false;        if(A[i] == len || len == length) break;// something wrong here    }    return false;}int main(){    while(cin>>n)    {        if(n == 0) break;        int tot = 0;        for(int i = 0; i < n; i++)        {            used[i] = false;            cin>>A[i];            tot += A[i];        }        sort(A,A+n,cmp);        for(int i = A[0]; i <= tot; i++)        {            if(tot%i == 0)            {                if(ok(n,0,i))                {                    cout<<i<<endl;                    break;                }            }        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.