无输出,求条,悬关
查看原帖
无输出,求条,悬关
780514
N1tr0us_Acid楼主2024/8/27 21:53
#include <bits/stdc++.h>

using namespace std;

const int N = 25;

int n;
int flag = 0;
int vis[N][N];
vector <int> path;
int r[N], c[N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int to(int x, int y) {
    return (x - 1) * n + y - 1;
}


bool dfs(int x, int y) {
    if(x == n && y == n && accumulate(c + 1, c + n + 1, 0) == 0 && accumulate(r + 1, r + n + 1, 0) == 0) {
        return true;
    }

    if(x > n || y > n || x < 1 || y < 1 || vis[x][y] || !c[y] || !r[x]) return false;

    c[y] --;
    r[x] --;
    
    int id = to(x, y);
            
    path.push_back(id);

    vis[x][y] = 1;

    for (int i = 0; i < 4; i ++) {
        int nx = x + dx[i];
        int ny = y + dy[i];

        if(dfs(nx, ny)) return true;
    }

    vis[x][y] = 0;
    
    path.pop_back();
    r[x] ++;
    c[y] ++;

    return false;
}

int main() {
    cin >> n;

    for (int i = 1; i <= n; i++) {
        cin >> c[i];
    }

    for (int i = 1; i <= n; i++) {
        cin >> r[i];
    }

    bool k = dfs(1, 1);

    for (int i = 0; i < path.size(); i++) {
        cout << path[i] << " ";
    }

    return 0;
}   
2024/8/27 21:53
加载中...