dfs为什么不行啊
查看原帖
dfs为什么不行啊
181715
gjh303987897楼主2021/10/24 11:25
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int b_x,b_y,hou_x,hou_y;
int map[25][25];
int ans;
void pre_deal(){
    map[hou_x][hou_y]=1;
	map[hou_x+2][hou_y+1]=map[hou_x+1][hou_y+2]=1;
	if(hou_x>=2){ 
		map[hou_x-2][hou_y+1]=1;
	}
	if(hou_x>=1){
		map[hou_x-1][hou_y+2]=1;
	}
	if(hou_y>=1){
		map[hou_x+2][hou_y-1]=1;
	}
	if(hou_y>=2){
		map[hou_x+1][hou_y-2]=1;
	}
	if(hou_y>=1&&hou_x>=2){
		map[hou_x-2][hou_y-1]=1;
	}
	if(hou_x>=1&&hou_y>=2){
		map[hou_x-1][hou_y-2]=1;
	}
}
void dfs(int x,int y){
	if(x==hou_x&&y==hou_y){
		ans++; return;
	}
	if(map[x][y]||x>b_x||y>b_y) return;
	dfs(x+1,y); dfs(x,y+1);
}
int main()
{
	cin>>b_x>>b_y>>hou_x>>hou_y;
	pre_deal(); dfs(0,0);
	cout<<ans;
	return 0;
	
} 
2021/10/24 11:25
加载中...