AcWing 1116. 马走日(遍历棋盘的路径总数)

题目链接:点击这里

在这里插入图片描述

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

using namespace std;
const int N = 15;

int dx[] = {-1, -2, -2, -1, 1, 2, 2, 1};
int dy[] = {-2, -1, 1, 2, 2, 1, -1, -2};

bool st[N][N];
int n, m, sx, sy;
int ans = 0;

void dfs(int x, int y, int now)
{
    if(now == 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, now + 1);
    }
    
    st[x][y] = false;       // 恢复现场
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        memset(st, false, sizeof st);
        
        cin >> n >> m >>sx >> sy;
        
        ans = 0;
        dfs(sx, sy, 1);
        
        cout << ans << endl;
    }
    
    return 0;
}
发布了844 篇原创文章 · 获赞 135 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/105107258