马走日
题目类型:DFS,外部搜索题目描述:
给定 $ n*m$ 大小的棋盘,以及马的起始位置,要求不能重复经过棋盘上的同一个点,问马有多少种途径全覆盖棋盘上的点
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 15;
int T=1;
int n,m,x,y;
int ans;
bool st[N][N];
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
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()
{
cin>>T;
while(T--){
cin>>n>>m>>x>>y;
memset(st,0,sizeof st);
ans=0;
dfs(x,y,1);
cout<<ans<<endl;
}
return 0;
}