求问各位大佬,深搜能过的题为什么广搜过不了?
  • 板块P1644 跳马问题
  • 楼主yfhshtk
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/10/27 22:45
  • 上次更新2023/10/28 09:27:27
查看原帖
求问各位大佬,深搜能过的题为什么广搜过不了?
1173402
yfhshtk楼主2023/10/27 22:45

广搜代码:


#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-'){
			w=(~w)+1;
		}
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		s=(s<<3)+(s<<1)+(ch&15);
		ch=getchar();
	}
	return s*w;
}
inline void write(int x){
	if(x<0){
		putchar('-');
		x=(~x)+1;
	}
	if(x>9){
		write(x/10);
	}
	putchar(x%10|48);
}
int n,m,cnt;
int dx[4]={1,2,-2,-1};
int dy[4]={2,1,1,2};
struct node{
	int x,y;
};
void bfs(){
	queue<node>q;
	q.push(node{0,0});
	while(!q.empty()){
		node tmp=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int h=tmp.x+dx[i];
			int l=tmp.y+dy[i];
			if(h<0||h>n||l>m){
				continue ;
			}
			if(h==n&&l==m){
				cnt++;
				continue;
			}
			q.push(node{h,l});
		}
	}
	return ;
}
int main(){
	std::ios::sync_with_stdio(false);
	n=read(),m=read();
	bfs();
	write(cnt);
	return 0;
}

深搜代码:


#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-'){
			w=(~w)+1;
		}
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		s=(s<<3)+(s<<1)+(ch&15);
		ch=getchar();
	}
	return s*w;
}
inline void write(int x){
	if(x<0){
		putchar('-');
		x=(~x)+1;
	}
	if(x>9){
		write(x/10);
	}
	putchar(x%10|48);
}
int n,m,cnt;
int dx[4]={1,2,-2,-1};
int dy[4]={2,1,1,2};
void dfs(int x,int y){
	for(int i=0;i<4;i++){
		int h=dx[i]+x;
		int l=dy[i]+y;
		if(h<0||h>n||l>m){
			continue;
		}
		if(h==n&&l==m){
			cnt++;
			continue;
		}
		dfs(h,l);
	}
}
int main(){
	n=read(),m=read();
	dfs(0,0);
	write(cnt);
	return 0;
}
2023/10/27 22:45
加载中...