因为你的函数是`int`类型的,而`int`类型的函数应该返回值,一直搜索似乎会开一大堆`int`。
by lzh009 @ 2024-04-21 18:46:11
把`int dfs(...)`改成`void dfs(...)`,然后把`return 0`去掉试试。
by lzh009 @ 2024-04-21 18:48:15
@[lzh009](/user/952814) 所以我要改成void?
by ycx20120224 @ 2024-04-21 18:48:54
是的,要不然搜索用int一般会超空间
by lzh009 @ 2024-04-21 18:50:07
@[ycx20120224](/user/1057768)
把 `dfs` 的类型该成 `void` 或者加上返回值(建议用第一种方法)
1.
```cpp
#include<iostream>
using namespace std;
int n,m,ans;
void dfs(int a,int b)
{
if(a<0 || a>n || b>m)
{
return;
}
if(a==n && b==m)
{
ans+=1;
}
else
{
dfs(a+2,b+1);
dfs(a+1,b+2);
dfs(a-2,b+1);
dfs(a-1,b+2);
}
}
int main()
{
cin>>n>>m;
dfs(0,0);
cout<<ans;
return 0;
}
```
2.
```cpp
#include<iostream>
using namespace std;
int n,m,ans;
int dfs(int a,int b)
{
if(a<0 || a>n || b>m)
{
return 0;
}
if(a==n && b==m)
{
ans+=1;
}
else
{
dfs(a+2,b+1);
dfs(a+1,b+2);
dfs(a-2,b+1);
dfs(a-1,b+2);
}
return 0;
}
int main()
{
cin>>n>>m;
dfs(0,0);
cout<<ans;
return 0;
}
```
by _s_z_y_ @ 2024-04-21 18:51:47
是的
by gongtui66966 @ 2024-04-21 18:52:15
@[lzh009](/user/952814) @[lzh009](/user/952814) 好的谢谢,AC了
by ycx20120224 @ 2024-04-21 18:53:46