POJ 1270 Following Orders (DFS,拓扑排序,剪枝)
摘要:1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 char contents[30]; 7 char ans[30]; 8 bool constraints[60][60]; 9 bool visited[30]; //记录了contents[i]是否已被访问过 10 int before[100]; 11 int n=0; 12 /************没加任何限制的,输出
阅读全文
posted @
2012-11-26 17:01
MicZ
阅读(304)
推荐(0)
HOJ 1956 Square(DFS+剪枝)
摘要:看到题目很容易想到要用DFS,但写出来之后发现TLE了。以为要用其它的算法,百度了下之后发现这题的考点就在于剪枝。刚开始我只用了两个剪枝:各木棍之和是否能被4整除;是否有木棍的长度大于正方形的边长。但事实证明还不够。关键的两个剪枝在DFS里面。具体看代码及注释吧。注意:每一次递归进去,开始搜索的位置的选择,经常是可以进行重要优化的!//DFS+剪枝优化
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm>
using namespace std
阅读全文
posted @
2012-09-02 23:56
MicZ
阅读(157)
推荐(0)
HOJ 2576 Simple Calculation 容斥原理(DFS)
摘要:输入两个数n,m,再输入一个有n元素的集合x[n]。求从[1,m]中有多少个数,能被x[n]中至少一个数整除。这题利用了容斥原理。容斥原理在编程中实现起来一般有dfs+剪枝和按位运算两种。按位运算易于调试,但是不适于大数据量。第一次写还是感觉挺困难的,参考了别人的代码。是用dfs写的。参照他的思路我自己又写了一遍,却总是WA,找了好久也没发现错在哪。。。先贴上来吧。自己写的WA代码:#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
int len,num[20];
阅读全文
posted @
2012-08-23 19:59
MicZ
阅读(208)
推荐(0)