[数据结构]八皇后(暴力,解答树,DFS回溯)
2015-06-21 10:05
393 查看
/* Name:八皇后(DFS回溯) Actor:HT Time:2015年6月21日 Error Reporte: } */ #include"stdio.h" #include"iostream" #include"string.h" using namespace std; #define N 8 int sum; int _amap[2 * N + 1] = { 0 }; int _bmap[2 * N + 1] = { 0 }; //int _xmap[N + 1] = { 0 }; int _ymap[N + 1] = { 0 }; int bigmap ; //写着玩 void set(int x, int y, int i) { _ymap[y] = i; _amap[y - x + N] = i; _bmap[y + x - 1] = i; bigmap[x] = y; //写着玩 } void print() //写着玩 { int i, j; for (i = 1; i <= N; i++) { for (j = 1; j <= N; j++) { if (bigmap[i] == j) cout << " * "; else cout << " . "; } cout << endl; } } void dfs(int x) { if (x == N+1) { sum++; print(); //写着玩 cout << endl; //写着玩 } int i; for (i = 1; i <= N; i++) { if (_amap[i - x + N] == 1 || _bmap[i + x - 1] == 1 || _ymap[i] == 1) continue; set(x, i, 1); dfs(x + 1); set(x, i, 0); } } int main() { sum = 0; dfs(1); cout << sum << endl; system("pause"); }
相关文章推荐
- 二叉树的三种遍历方式:递归、栈、循环 分类: C/C++ 数据结构与算法 2015-06-21 09:47 269人阅读 评论(0) 收藏
- JAVA 常用数据结构操作
- 数据结构和算法-010 数组排序 希儿排序
- [数据结构]埃及分数(暴力,解答树,迭代加深搜索)
- [数据结构]二叉树自学
- 线性表子系统
- 数据结构第三部分:树与树的表示、二叉树及其遍历、二叉搜索树、平衡二叉树、堆、哈夫曼树、集合及其运算
- 数据结构——链式队列(c++)
- 斐波那契查找(超详解)
- 数据结构——循环队列(c++)
- 数据结构——链式栈(c++)
- 第2章 算法入门
- To_review_100_1---KMP算法的整理
- 数据结构——顺序栈(c++)
- Esper入门简介:三、 对Esper底层数据结构特点分析,数据的入、出
- Redis内部数据结构详解之压缩链表(ziplist)
- 算法竞赛入门经典之stl重的常用数据结构----->set
- [数据结构]栈之顺序栈的类模板实现
- Codeforces Round #285 (Div. 1)B. Misha and Permutations Summation(数学+数据结构打脸)
- leetcode--Binary Search Tree Iterator