其他分享
首页 > 其他分享> > AcWing 1116 . 马走日

AcWing 1116 . 马走日

作者:互联网

题目传送门

#include <bits/stdc++.h>

using namespace std;
const int N = 10;

int n, m;
bool st[N][N]; //是否走过
int ans;

//八个方向
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};

// cnt:已经走了多少个格子
void dfs(int x, int y, int cnt) {
    //收集答案
    if (cnt == n * m) {
        ans++;
        return;
    }

    //标识当前格子已使用
    st[x][y] = true;

    for (int i = 0; i < 8; i++) {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || a >= n || b < 0 || b >= m) continue;
        if (st[a][b]) continue;
        dfs(a, b, cnt + 1);
    }

    //标识当前格子未使用
    st[x][y] = false;
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int x, y;
        cin >> n >> m >> x >> y;
        memset(st, 0, sizeof st);
        ans = 0;
        dfs(x, y, 1);
        printf("%d\n", ans);
    }
    return 0;
}

标签:cnt,格子,int,1116,dfs,st,马走,ans,AcWing
来源: https://www.cnblogs.com/littlehb/p/15975748.html