online_judge_1140
来源:互联网 发布:石家庄鸿搜网络怎么样 编辑:程序博客网 时间:2024/04/26 05:22
#include <iostream>#include <cstdio>#include <cstring>using namespace std;bool mark[9][9];int ans[100];int cnt = 1;bool isSafe(int deep, int m){ for(int i=1; i<deep; ++i) { if(mark[m][i]) return false; if(m>i && mark[m-i][deep-i]) return false; if(m+i<9 && mark[m+i][deep-i]) return false; } return true;}void DFS(int deep){ int i,j; if(deep == 9) { for(j=1; j<9; ++j) { for(i=1; i<9; ++i) { if(mark[i][j]) { ans[cnt] = ans[cnt]*10 + i; break; } } } cnt++; return ; } for(i=1; i<9; ++i) { if(isSafe(deep, i)) { mark[i][deep] = true; DFS(deep+1); mark[i][deep] = false; } }}int main(){ int n,m; memset(mark, 0, sizeof(mark)); memset(ans, 0, sizeof(ans)); DFS(1); scanf("%d",&n); while(n--) { scanf("%d",&m); printf("%d\n",ans[m]); } return 0;}
简单的一道回溯法题目。。但是由于对细节上的处理不当,,浪费了好多时间……
0 0
- online_judge_1140
- 算法面试题整理
- zoj 3537 Cake 【凸包 + 区间dp】 【最优三角剖分】
- Google 推出的 31 套在线课程
- -bash: netstat: command not found
- Introduction to Chain Codes 链码导论
- online_judge_1140
- Diffy使用过程问题总结
- 机房收费系统安装过程
- 排序算法—选择排序算法分析与实现(Python)
- Android Studio你不知道的调试技巧
- ehcache缓存的使用
- GlassFish WELD IllegalStateException
- sqllite3命令行下的常用命令及接口
- Chain Codes and Your Brain 链码和大脑