图~算法题

 老子的全排列呢

#include<bits/stdc++.h>

using namespace std;
vector<vector<int>> ret;
vector<int> res;

void dfs (vector<bool> &used) {
    if (res.size() == 8) {ret.push_back(res);return;}
    for (int i = 1; i <= 8; i++) {
        if (used[i]) continue;
        used[i] = true;
        res.push_back(i);
        dfs(used);
        res.pop_back();
        used[i] = false;
    } 
}
    
int main() {
    vector<bool> used(8, false);
    dfs(used);
    for (int i = 0; i < ret.size(); i++) {
        for (int j = 0; j < ret[i].size(); j++) cout << ret[i][j] << " ";
        cout << "\n";
    }
    return 0;
}

 排列数字

 

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int n;
int path[100];
bool s[100];

void dfs(int u) {
    if(u > n) {
        for (int i = 1; i <= n; i++){
            cout << path[i] << " ";
        }
        cout << endl;
    }
    
    for (int i = 1; i <= n; i++) {
        if(!s[i]) {
            s[i] = true;
            path[u] = i;
            dfs(u + 1);
            s[i] = false;
        }
    }
}

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

841. 钥匙和房间 - 力扣(LeetCode)

dfs 

class Solution {
public:
    bool used[1100];
    void dfs(vector<vector<int>>&rooms, int x) {
        for (auto &y : rooms[x]) {
            if (!used[y]){
            used[y] = true;
            dfs(rooms, y);}
        }
    }
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        dfs(rooms, 0);
        used[0] = true;
        for (int i = 0; i < rooms.size(); i++) {
            if (!used[i]) return false;
        }
        return true;
    }
};

bfs 

class Solution {
public:
    bool used[1100];
    void bfs(vector<vector<int>> &rooms, int x) {
        queue<int> que;
        que.push(0);
        while (!que.empty()) {
            auto it = que.front();que.pop();
            for (auto y : rooms[it]) {
                if (used[y] == false) {
                    used[y] = true;
                    que.push(y);
                }
            }
        }
    }
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        used[0] = true;
        bfs(rooms, 0);
        for (int i = 0; i < rooms.size(); i++) {
            if(!used[i]) return false;
        }
        return true;
    }
};

走出迷宫

#include<bits/stdc++.h>

using namespace std;

const int N = 510;
char a[N][N];
int st[N][N];
int n, m, flag = 0;
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};

void dfs(int xx, int yy) {
    if (a[xx][yy] == 'E') flag = 1;
    for (int i = 0; i < 4; i++) {
        int x = dx[i] + xx, y = dy[i] + yy;
        if (x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '#' && !st[x][y]) {
            st[x][y] = 1;
            dfs(x, y);
        }
    }
}

int main() {
    
    while (cin >> n >> m) {
        flag = 0;
        int x1, y1;
        memset(a, 0, sizeof a);
        memset(st, 0, sizeof st);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> a[i][j];
                if (a[i][j] == 'S') {x1 = i; y1 = j;}
            }
        }
        dfs(x1, y1);
        if (flag) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    
    return 0;
}

n-皇后

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int n;
const int N = 20;
char a[N][N];

bool judge(int start, int end) {
    for (int i = 0; i <= start; i++) if(a[i][end] == 'Q') return false;
    for (int i = 0; i <= end; i++) if(a[start][i] == 'Q') return false;
    
    for (int i = start, j = end; i >= 0 && j >= 0; i--, j--)  if(a[i][j] == 'Q') return false;
    for (int i = start, j = end; i >= 0 && j < n; i--, j++)  if(a[i][j] == 'Q') return false;
    
    return true;
}

void dfs(int u) {
    if(u == n) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << a[i][j];
            }
            cout << endl;
        }
        cout << endl;
    }
    
    for (int j = 0; j < n; j++) {
        if (judge(u, j)) {
            a[u][j] = 'Q';
            dfs(u + 1);
            a[u][j] = '.';
        }
    }
}

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

 走迷宫

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> PII;
#define x first
#define y second

queue<PII>q;
int n, m;
const int N = 110;
int a[N][N],s[N][N];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int bfs() {
    q.push({0, 0});
    
    while(q.size()) {
        auto it = q.front();q.pop();
        for (int i = 0; i < 4; i++) {
            int x = it.x + dx[i], y = it.y + dy[i];
            if(x >= 0 && x < n && y >= 0 && y < m && a[x][y] == 0 && s[x][y] == 0) {
                s[x][y] = s[it.x][it.y] + 1;
                q.push({x,y});
            }
        }
    }
    return s[n - 1][m - 1];
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }
    
    cout << bfs();
    return 0;
}

受伤的皇后 - 蓝桥云课 (lanqiao.cn)

全球变暖 - 蓝桥云课 (lanqiao.cn)

模拟战役

jelly

[NOIP2014]寻找道路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未央吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值