zoukankan      html  css  js  c++  java
  • 回溯法

    N皇后问题

    递归:

    #include<iostream>
    #include<vector>
    #include<stack>
    #include<cmath>
    #include<cstring>
    using namespace std;
    typedef pair<int, int> P;
    int n,sum;
    vector<P> v;
    bool judge(int x, int y){
        for (int i = 0; i < v.size(); i++){
            float bioas = (v[i].second*1.0 - y*1.0) / (v[i].first*1.0 - x * 1.0);
            if (x == v[i].first || y == v[i].second || abs(bioas) == 1.0)
                return false;
        }
        return true;
    }
    void trace(int index){
        if (index >= n){
            sum++;
            for (int i = 0; i < v.size(); i++)
                printf("(%d, %d) ", v[i].first, v[i].second);
            cout << endl;
            return;
        }
        for (int i = 0; i < n; i++){
            if (judge(index, i)){
                v.push_back(P(index, i));
                trace(index+1);
                v.erase(v.begin()+index, v.end());    
            }
        }
    }
    
    
    int main(){
        while(cin >> n){
            v.clear();
            sum = 0;
            trace(0);
            cout << sum << endl;
        }    
        return 0;
    } 
    View Code

    迭代:

    #include<iostream>
    #include<vector>
    #include<stack>
    #include<cmath>
    #include<cstring>
    using namespace std;
    typedef pair<int, int> P;
    int n,sum;
    vector<P> v;
    bool judge(int x, int y){
        for (int i = 0; i < v.size(); i++){
            float bioas = (v[i].second*1.0 - y*1.0) / (v[i].first*1.0 - x * 1.0);
            if (x == v[i].first || y == v[i].second || abs(bioas) == 1.0)
                return false;
        }
        return true;
    }
    void trace(int index){
        if (index >= n){
            sum++;
            for (int i = 0; i < v.size(); i++)
                printf("(%d, %d) ", v[i].first, v[i].second);
            cout << endl;
            return;
        }
        for (int i = 0; i < n; i++){
            if (judge(index, i)){
                v.push_back(P(index, i));
                trace(index+1);
                v.erase(v.begin()+index, v.end());    
            }
        }
    }
    
    
    int main(){
        while(cin >> n){
            v.clear();
            sum = 0;
            trace(0);
            cout << sum << endl;
        }    
        return 0;
    } 
    View Code

    0-1背包问题

    #include<iostream>
    #include<vector>
    #include<sstream>
    #include<algorithm>
    using namespace std;
    typedef pair<int, int> P;
    int n, maxV, W;
    string maxS;
    vector<P> v;
    bool cmp(P a, P b){
        return a.first > b.first;
    }
    void solve(int index, int sumV, int sumW, int fV, string s){
        if (maxV < sumV){
            maxV = sumV;
            maxS = s;
        }
        
        if (index >= n || maxV >= fV + sumV) return;
        string left = s;
        solve(index+1, sumV, sumW, fV - v[index].first, left);
        if(sumW + v[index].second <= W && sumV + fV > maxV){
            stringstream ss;
            ss << index+1;
            string right = s +' '+ ss.str();
            solve(index+1, sumV + v[index].first, sumW + v[index].second, fV - v[index].first, right); 
        }
    }
    int main(){
        while(cin >> n >> W) {
            v.clear();
            maxS.clear();
            maxV = 0;
            int fV = 0;
            for (int i = 0; i < n; i++) {
                int x, w;
                cin >> x >> w;
                fV += x;
                v.push_back(P(x,w));
            }
            //sort(v.begin(), v.end(),cmp);
            string s="最佳解为:";
            solve(0, 0, 0, fV ,s);
            cout << maxV << endl;
            cout << maxS << endl;
        }
        return 0;
    } 
    
    /*
    4 10
    42 7
    25 5
    12 3
    40 4
    
    
    65
    */
    View Code
  • 相关阅读:
    【ST开发板评测】Nucleo-F411RE开箱报告
    手把手教你制作Jlink-OB调试器(含原理图、PCB、外壳、固件)
    国产处理器的逆袭机会——RISC-V
    基于uFUN开发板和扩展板的联网校准时钟
    基于uFUN开发板的RGB调色板
    理解ffmpeg中的pts,dts,time_base
    如何终止线程的运行(C/C++)
    关于阻塞和非阻塞,同步和异步的总结
    QT移植无法启动 This application failed to start because it could not find or load the QT platform
    Qt5学习记录:QString与int值互相转换
  • 原文地址:https://www.cnblogs.com/astonc/p/12013692.html
Copyright © 2011-2022 走看看