64,266
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<memory.h>
using namespace std;
int arr[11][11]={0};
int t,n,m,x,y,tx,ty,cnt=0; //***********************
//x在前
int dir[8][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
//点x,点y,计数
void Dfs(int x,int y,int ans){ //重命名了 ,但是局部变量优先级高于全局,代码逻辑没错误
if(ans==m*n){
cnt++;
}else{
for(int i=0;i<8;i++){
tx=x+dir[i][0]; //**************
ty=y+dir[i][1]; //**************
if(tx>=0 && tx<n && ty>=0 && ty<m && arr[ty][tx]!=1){ //m, 打成n了
arr[ty][tx]=1;
Dfs(tx,ty,ans+1);
arr[ty][tx]=0;
}
}
}
}
int main(){
cin>>n>>m>>x>>y;
memset(arr, 0, sizeof(arr));
cnt=0;
arr[y][x]=1; //1=走过
Dfs(x,y,1);
cout<<cnt<<endl;
return 0;
}
#include<iostream>
#include<memory.h>
using namespace std;
int arr[11][11]={0};
int t,n,m,x,y,cnt=0;
//x在前
int dir[8][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}};
//点x,点y,计数
void Dfs(int x,int y,int ans){ //重命名了 ,但是局部变量优先级高于全局,代码逻辑没错误
if(ans==m*n){
cnt++;
}else{
for(int i=0;i<8;i++){
int tx=x+dir[i][0]; //未知问题,变量定义在这里使用栈空间就没问题了.
int ty=y+dir[i][1]; //占用全局变量空间就会发生错误
if(tx>=0 && tx<n && ty>=0 && ty<m && arr[ty][tx]!=1){ //m, 打成n了
arr[ty][tx]=1;
Dfs(tx,ty,ans+1);
arr[ty][tx]=0;
}
}
}
}
int main(){
cin>>n>>m>>x>>y;
memset(arr, 0, sizeof(arr));
cnt=0;
arr[y][x]=1; //1=走过
Dfs(x,y,1);
cout<<cnt<<endl;
return 0;
}