搜索學習

dfs

題目: 排列數字

題意:就是讓你輸出全排列。
思路:可以用dfs去搜索。
代碼:數組標記

#include<bits/stdc++.h>
using namespace std;

const int maxn = 10;
int pri[maxn], n, vis[maxn];
void dfs(int step){
    if(step == n){
        for(int i = 0; i < n; i++){
            if(i != 0)cout<<" ";
            cout<<pri[i];
        }
        cout<<endl;
        return;
    }
    for(int i = 1;  i <= n; i++){
        if(!vis[i]){
            vis[i] = 1;//標記
            pri[step] = i;//記錄
            dfs(step+1);
            vis[i] = 0;//恢復現場
            pri[step] = 0;
        }
    }
    
}

int main(){
    cin>>n;
    dfs(0);
    
return 0;    
}

通過位運算標記:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 10;
int pri[maxn], n, vis = 0;
void dfs(int step){
    if(step == n){
        for(int i = 0; i < n; i++){
            if(i != 0)cout<<" ";
            cout<<pri[i];
        }
        cout<<endl;
        return;
    }
   // cout<<step<<endl;
    for(int i = 1;  i <= n; i++){
        if((vis & (1<<i)) == 0){
            vis += 1<<i;
            //cout<<vis<<endl;
            pri[step] = i;
            dfs(step+1);
            vis -= 1<<i;
            pri[step] = 0;
            
        }
    }
    
}

int main(){
    cin>>n;
    vis = 0;
    dfs(0);
    
return 0;    
}

題目:n-皇后問題

題意:在同一行或者同一列,不能放置Q,把所有的結果輸出來。
思路:以行爲參數,去搜索列。
代碼:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 10;
int c[maxn*2], r[maxn*2], vis[maxn];
int n;
char mp[maxn][maxn];
void dfs(int u){
    if(u == n){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++)cout<<mp[i][j];
            cout<<endl;
        }
        cout<<endl;
        return;
    }
    for(int i = 0; i < n; i++){
        if(!vis[i] && !c[n-u+i] && !r[u+i]){  //行c: y = x + b -> b = y-x,由於可能是負的所以加一個n,列r:y = x - b -> b = y+x,
            vis[i] = 1;
            c[n-u+i] = 1;
            r[u+i] = 1;
            mp[u][i] = 'Q';
            dfs(u+1);
            vis[i] = 0;//恢復現場
            c[n-u+i] = 0;
            r[u+i] = 0;
            mp[u][i] = '.';
        }
    }
    
}

int main(){
    cin>>n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++)mp[i][j] = '.';
    }
    dfs(0);
    
    return 0;
}

bfs

題目:走迷宮

題意:給你一個圖0代表可以走, 1代表不可以走,問你從(1, 1)到(n, m)的最短距離。
代碼:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100;
int n, m;
int dx[4] = {0,0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int mp[maxn][maxn], vis[maxn][maxn];
typedef pair<int, int> PII;
PII tmp;
void bfs(){
    queue<PII>q;
    vis[1][1] = 0;
    q.push({1, 1});
    while(q.size()){
        PII tmp = q.front();
        q.pop();
        //cout<<tmp.first<<" "<<tmp.second<<endl;
        for(int i = 0; i < 4; i++){
            int xx, yy;
            xx = tmp.first + dx[i];
            yy = tmp.second + dy[i];
            if(xx >= 1 && xx  <= n && yy >= 1 & yy <= m && !mp[xx][yy] && vis[xx][yy] == -1){
                q.push({xx, yy});
                vis[xx][yy] = vis[tmp.first][tmp.second] + 1;
                //if(xx == n && yy == m)break;
            }
        }

    }


}

int main(){
    cin>>n>>m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cin>>mp[i][j];
        }
    }
    memset(vis, -1, sizeof vis);
    bfs();
    cout<<vis[n][m]<<endl;


  return 0;
}

題目:圖中點的層次

代碼:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e5+10;
int h[maxn], to[maxn], nex[maxn], tot = 0;

void add(int u, int v){
    to[tot] = v;
    nex[tot] = h[u];
    h[u] = tot++;
}

int n, m, vis[maxn];

int bfs(){
    memset(vis, -1, sizeof vis);
    vis[1] = 0;
    
    queue<int>q;
    q.push(1);
    
    while(q.size()){
        int tmp = q.front();
        q.pop();
        
        for(int i = h[tmp]; ~i; i = nex[i]){
            int j = to[i];
            
            if(vis[j] == -1){
                vis[j] = vis[tmp] + 1;
                q.push(j);
                if(j == n)break;
            }
            
        }
    }
    return vis[n];
}
int main(){
    cin>>n>>m;
    int a, b;
    memset(h, -1, sizeof h);
    
    for(int i = 1; i <= m; i++){
       cin>>a>>b;
       add(a, b);
    }
    
    cout<<bfs()<<endl;
    return 0;
}

拓撲排序

題目:有向圖的拓撲序列

代碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;

int h[maxn], to[maxn], nex[maxn], degree[maxn], tot = 0;
int n, m;
vector<int>v;
void add(int u, int v){
    to[tot] = v;
    nex[tot] = h[u];
    h[u] = tot++;
}
int TopSort(){
    queue<int>q;
    for(int i = 1; i <= n; i++){
        if(!degree[i])q.push(i);//入度爲0的點
    }
    int cnt = 0;
    while(q.size()){
        int tmp = q.front();
        //cout<<tmp<<endl;
        cnt++;
        q.pop();
        v.push_back(tmp);
        for(int i = h[tmp]; ~i; i = nex[i]){
            int j = to[i];
            degree[j]--;
            if(!degree[j])q.push(j);
        }
        
    }
    if(cnt < n)return -1;//代表產生環
    return 1;
    
}

int main(){
    cin>>n>>m;
    int a, b;
    memset(h, -1, sizeof h);
    for(int i = 1; i <= m; i++){
        cin>>a>>b;
        add(a, b);
        degree[b]++;//統計入度
    }
    if(TopSort() == -1)cout<<-1<<endl;
    else{ for(int i = 0; i < n; i++){
        cout<<v[i]<<" ";
    }
        puts("");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章