马走日DFS解法 From CSDN
2024-07-13 21:13:30
发布于:广东
111阅读
0回复
0点赞
题……
不想解
算了,废话不多说,直接上题解
#include<iostream>
#include<string.h>
using namespace std;
int book[15][15];
int dir[8][2]={{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
int n,m,x,y;
int sum=0;
void dfs(int a,int b,int s){
int p,q,k;
if(s==n*m){
sum++;
}
if(book[a][b]==0){
book[a][b]=1;
for(k=0;k<8;k++){
p=a+dir[k][0];
q=b+dir[k][1];
if(p>=n||p<0||q>=m||q<0)
continue;
if(book[p][q]==1)
continue;
dfs(p,q,s+1);
}
book[a][b]=0;
}
return ;
}
int main(){
int t;
cin>>t;
while(t--){
cin>>n>>m>>x>>y;
sum=0;
memset(book,0,sizeof(book));
dfs(x,y,1);
cout<<sum;
}
return 0;
}
求求各位老爷,copy了我的代码或觉得我的代码好的,请给我一个免费的赞,这是对我最大的鼓励!
这里空空如也
有帮助,赞一个