深搜思想

这道题的题意大概是OOO+OOO=OOO,九个数都是1-9之间并且不重复

这道题的代码是我在算法书上看到的,先写在这里吧,之后有时间在完善

这道题的思想是利用深度优先搜索的思想

  1. /*深搜的基本套路代码*/  
  2.   
  3. void dfs(int step)  
  4. {  
  5. //判断边界  
  6. //尝试每一种可能  
  7. for(i=1;i<=n;i++)  
  8. {  
  9. //继续下一步  
  10. dfs(step+1);  
  11. //返回  
  12. }  
  13.   
#include<iostream>
using namespace std;
/*
@quthor:浅滩 
problem:算术式子 
data:2019.05.21 
*/
int book[10],a[10],cnt=0; 
void dfs(int step)
{
    int i;
    if(step==10)
    {    
        if(a[1]*100+a[2]*10+a[3]+a[4]*100+a[5]*10+a[6]==a[7]*100+a[8]*10+a[9])
        {cout<<a[1]<<a[2]<<a[3]<<'+'<<a[4]<<a[5]<<a[6]<<'='<<a[7]<<a[8]<<a[9]<<endl;
        }
        return ;
    }
  for(i=1;i<=9;i++)
{
  if(book[i]==0)
  {
  a[step]=i; 
  book[i]=1; 
  dfs(step+1);
  book[i]=0;
  }
}    
        
}
int main()
{
dfs(1);    
return 0;    
} 
不一样的烟火
原文地址:https://www.cnblogs.com/cstdio1/p/10902669.html