# 5.捡球

5.捡球

``5 4 0 0``

``32``

``````#include<stdio.h>
#include<stdlib.h>
const int maxn = 20;
const int mv[8][2] = {{1,2},{1,-2},{2,1},{2,-1},{-1,2},{-1,-2},{-2,-1},{-2,1}};
int m,n;
int startx,starty;
int vis[maxn][maxn];
int has_visited_num = 1;
int ways;
void dfs(int startx2,int starty2)
{
if(has_visited_num == m * n)
{
ways ++;
return;
}
for(int i=0; i<8; i++)
{
int x = startx2 + mv[i][0];
int y = starty2 + mv[i][1];
if(x<0||y<0||x>=m||y>=n)
continue;
if(!vis[x][y])
{
vis[x][y] = 1;
has_visited_num ++;
dfs(x,y);
has_visited_num --;
vis[x][y] = 0;
}
}
}
int main()
{
scanf("%d%d%d%d",&m,&n,&startx,&starty);
vis[startx][starty] = 1;
dfs(startx,starty);
printf("%d\n",ways);
return 0;
}
``````

``````#include <bits/stdc++.h>

using namespace std;
int Book[25][25];
int Next[8][2]=        //定义八个方向
{
{1,-2},
{2,-1},
{2,1},
{1,2},
{-1,2},
{-2,1},
{-2,-1},
{-1,-2}
};
int step;
int sum;
void dfs(int x,int y,int n,int m,int step)
{
int i,j,tx,ty;
if(step==(n*m)){
sum++;
}
for(i=0;i<8;i++){
tx=x+Next[i][0];
ty=y+Next[i][1];
if(tx<0||ty<0||tx>=n||ty>=m||Book[tx][ty]==1) continue;
if(Book[tx][ty]==0){
Book[tx][ty]=1;
dfs(tx,ty,n,m,step+1);
Book[tx][ty]=0;
}
}
}
int main()
{
int T;
int n,m,x,y;
sum=0;
cin>>n>>m>>x>>y;
Book[x][y]=1;
dfs(x,y,n,m,1);
cout<<sum;
return 0;
}``````