BFS法题解:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct point { int x; int y; };
struct point horse, horsemid;
struct point end;
struct point pawn;
char count=1;
int main() {
scanf("%d%d%d%d", &end.x, &end.y, &horse.x, &horse.y);
long board[end.x+3][end.y+3];
for (char a=0;a<end.x+3;a++){
for (char b=0;b<end.y+3;b++){
board[a][b]=1;
}
}
horse.x++;
horse.y++;
board[horse.x][horse.y]=0;
board[horse.x+2][horse.y-1]=0;
board[horse.x+2][horse.y+1]=0;
board[horse.x+1][horse.y+2]=0;
board[horse.x+1][horse.y-2]=0;
board[horse.x-1][horse.y-2]=0;
board[horse.x-1][horse.y+2]=0;
board[horse.x-2][horse.y-1]=0;
board[horse.x-2][horse.y+1]=0;
for (char a=0;a<end.x+3;a++){
board[a][0]=0;
board[a][end.y+2]=0;
}
for (char a=0;a<end.y+3;a++){
board[0][a]=0;
board[end.x+2][a]=0;
}
for (char a=1;a<=end.x+1;a++){
for (char b=1;b<=end.y+1;b++){
if(board[a][b]!=0){
board[a][b]=board[a][b-1]+board[a-1][b];
}
if(a==1&&b==1){
board[a][b]=1;
}
}
}
printf("%ld\n",board[end.x+1][end.y+1]);
return 0;
}
DFS法题解:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct point { char x; char y; };
struct point horse, horsemid;
struct point end;
struct point pawn;
long dfs(char (*ptr)[end.y+3],char x,char y ){
static long count =0;
if(ptr[x][y]==10){
count ++;
}
if (ptr[x+1][y]==0||ptr[x+1][y]==10){
dfs(ptr,x+1,y);
}
if(ptr[x][y+1]==0||ptr[x][y+1]==10){
dfs(ptr,x,y+1);
}
return count;
}
int main() {
scanf("%d%d%d%d", &end.x, &end.y, &horse.x, &horse.y);
char board[end.x+3][end.y+3];
for (char a=0;a<end.x+3;a++){
for (char b=0;b<end.y+3;b++){
board[a][b]=0;
}
}
for (char a=0;a<end.x+3;a++){
board[a][0]=1;
board[a][end.y+2]=1;
}
for (char a=0;a<end.y+3;a++){
board[0][a]=1;
board[end.x+2][a]=1;
}
board[end.x+1][end.y+1]=10;
horse.x++;
horse.y++;
board[horse.x][horse.y]=1;
board[horse.x+2][horse.y-1]=1;
board[horse.x+2][horse.y+1]=1;
board[horse.x+1][horse.y+2]=1;
board[horse.x+1][horse.y-2]=1;
board[horse.x-1][horse.y-2]=1;
board[horse.x-1][horse.y+2]=1;
board[horse.x-2][horse.y-1]=1;
board[horse.x-2][horse.y+1]=1;
printf("%ld\n",dfs(board,1,1));
return 0;
}