#include <iostream> #include <cstdio> using namespace std; int n = 9; int a[10000],book[10000]; void dfs(int step) { if(step == 10&&a[1]*100+a[2]*10+a[3] + a[4]*100+a[5]*10+a[6] == a[7]*100+a[8]*10+a[9]){ for(int i = 1;i <= 9;i++){ cout << a[i] << " "; } cout << endl; return; } if(step == 10){ return; } for(int i = 1;i <= 9;i++){ if(book[i] == 0){ a[step] = i; book[i] = 1; dfs(step+1); book[i] = 0; } } return; } int main(){ dfs(1); }
算法初步P3.深度优先搜索
最新推荐文章于 2024-06-16 22:31:26 发布