oj:深搜+回溯(2)
来源:互联网 发布:数据库购买 编辑:程序博客网 时间:2024/05/18 14:43
参考《算法设计与分析》P276
#include <iostream>#include <string>using namespace std;int map[25][4];int n;int q;int icount[25];int itable[25];int place(int pos){ if (pos == n*n)return 1; //递归出口 for (int i = 0; i < q; i++) //在pos位置上,从候选类型中选择一个放在此位置 { if (icount[i] == 0)continue; //如果类型为i的没有,则继续for循环 if (pos%n != 0) if (map[itable[pos - 1]][1] != map[i][3])continue; if (pos >= n) if (map[itable[pos - n]][2] != map[i][0])continue; itable[pos] = i; icount[i]--; if (place(pos + 1) == 1)return 1; icount[i]++; } return 0;}int main(){ while (cin >> n && n) { int top, right, down, left; /* 初始化全局变量 */ q = 0; memset(map, 0, sizeof(map)); memset(icount, 0, sizeof(icount)); memset(itable, 0, sizeof(itable)); /* 输入值,重复类型不存入map中 */ for (int i = 0; i < n*n; i++) { cin >> top >> right >> down >> left; int j; for ( j = 0; j < q; j++) { if (map[j][0] == top && map[j][1] == right && map[j][2] == down && map[j][3] == left) { icount[j]++; break; } } if (j == q) { map[j][0] = top; map[j][1] = right; map[j][2] = down; map[j][3] = left; icount[j]++; q++; } } /* 开始深搜+回溯 */ if (place(0)) cout << "possible" << endl; else cout << "impossible" << endl; } system("pause"); return 0;}
此题也可以理解为:一个全排列+剪枝(条件处理)
0 0
- oj:深搜+回溯(2)
- OJ:深搜+回溯(1)
- oj:深搜+回溯(3)
- oj:深搜+回溯(4)
- OJ【3099】递归回溯?
- Hdu oj 2553 N皇后问题(回溯加预处理)
- 回溯法(2)
- 回溯法(2)
- hdu1010(深搜 剪枝 回溯)
- 迷宫问题(深搜+回溯)
- 杭电OJ(HDOJ)2553题:N皇后问题(回溯法)
- 九度OJ 1254:N皇后问题 (N皇后问题、递归、回溯)
- LeetCode OJ - N-Queens 回溯法
- LeetCode OJ-22.Generate Parentheses(回溯法)
- Tempter of the Bone(深搜,回溯)
- 回溯法------ 解数独游戏(2)
- 习题 7-2 uva225(回溯)
- 九度OJ 教程91 回溯算法之《全排列》
- 深入浅出SQL Server 2008 分区函数和分区表
- Activiti 接收任务(receivetask)
- 基础算法题(1) - 数字、模拟
- 补间动画(Tween)
- C语言union关键字
- oj:深搜+回溯(2)
- jQuery插件实现“点击获取验证码后60秒内禁止重新获取(防刷新)”
- Makefile 使用总结
- matlab中用到的randperm函数
- 8.10号的Java作业
- NOSQL数据库
- MVC Action Filter
- |Tyvj|NOIP2004|堆|P3301 最小函数值
- 设计模式总结之Strategy Pattern(策略模式)