以一种枚举所有完整路径以遍历所有情况的搜索方法
递归
由栈实现
eg1:有n件物品,每件物品的重量为w[i],价值为c[i],选出若干件物品放入一个容量为V的背包中,使得在选入背包的物品重量和不超过V的前提下,让背包中的物品价值最大
记物品编号为index 记已选的物品重量sumW,总价值sumC
定义void DFS(int index,int sumW,int sumC);
遇到每一个物品都有放入和不放入两种情况,放入该物品,不放入该物品
即DFS(index+1,sumW,sumC)和DFS(index+1,sumW+w[index],sumC+c[index]);
优化:只有当sumW<=V时才进入选该物品的岔道
用sumC更新maxValue
判断递归结束条件,index增加到n则说明n件物品已经处理结束
#include
const int maxn = 30;
int n, v, maxValue = 0;//物品件数,背包容量,最大价值
int w[maxn], c[maxn];//w为每件物品重量,c为每件物品价值
void dfs(int index, int sumW, int sumC) {
if (index == n) {
if (sumW <= v && sumC > maxValue) maxValue = sumC;
return;
}
dfs(index + 1, sumW, sumC);//选第index件物品
dfs(index + 1, sumW + w[index], sumC + c[index]);//不选第index件物品
}
int main() {
(void)scanf("%d %d", &n, &v);
for (int i = 0; i < n; i++) {
(void)scanf("%d", &w[i]);
}
for (int i = 0; i < n; i++) {
(void)scanf("%d", &c[i]);
}
dfs(0, 0, 0);
printf("%d\n",maxValue);
return 0;
}
//input
5 8
3 5 1 2 2
4 5 2 1 3
//output
剪枝:上述代码中总是把n见物品的选择全部确定之才去更新最大价值,忽视了背包容量不超过v这个特点,可以把对sumW的判断加入岔道口,只有sumW<=v才会进入岔道。可以将dfs函数修改如下:
void DFS(int index,int sumW,int sumC){
if(index==n){
return ;
}
DFS(index+1,sumW,sumC);
if(sumW+w[index]<=V){//只有当加入第index件物品后不超过背包容量,才继续
if(sumC+c[index]>ans){
ans+=sumC+c[index];//更新最大价值maxValue
}
DFS(index+1,sumW+w[index],sumC+c[index]);
}
}
eg2:给定N个整数,选择K个数,使得这K个数之和恰好等于一个给定的整数X,如果有多种方案,选择它们中元素平方和最大的一个,数据保证这样的方案唯一
思路:记录index,nowK来记录当前已经选择的个数,sum和sumSqu分别记录当前已选整数和平方和
定义void DFS(int index,int nowK,int sum,int sumSqu)
保存最优方案:需要一个数组temp存放当前已经选择的整数,当试图进入选index号数的分支时,把A[index]加入temp中,当这条分支结束时,把它从temp中去除,不影响不选index这条分支
在某个时刻发现当前已经选择了K个数,且这K个数之和恰好为x时,判断平方和是否大于sumSqu,如果大,说明找见了更优解,把temp赋给ans
所有方案枚举结束后,ans存放最优方案,maxSumSqu存放最优值
int n,k,x,maxSqu=-1,A[maxn];
vector<int> temp,ans;
void DFS(int index,int nowK,int sum,int sumSqu){
if(nowk==k&&sum==x){//找见k个数的和为x
if(sumSqu>maxSqu){//找见比当前的更优(平方和更大)
maxSqu=sumSqu;
ans=temp;//更新最优方案
}
return;
}
if(index==n||nowK>k||sum>x)return;//处理完或者已经超过条件
temp.push_back(A[index]);//选第index号数
DFS(index+1,nowK+1,sum+A[index],sumSqu+A[index]*A[index]);
temp.pop_back();//不选index号数
DFS(index+1,nowK,sum,sumSqu);
}
如果每个数可以选择多次,则选index这条分支应该一直进行index的处理,直到某个时刻不选择index号数即改为
DFS(index,nowK+1,sum+A[index],sumSqu+A[index]*A[index]);
**eg3:[PAT-A 1103]Integer Factorization
DFS:碰到岔道口先选择其中的一条岔路前进
BFS:碰到岔道口选择其中的一条前进,不管其他岔路
由队列实现
基本模板
void BFS(int s){
queue q;
q.push(s);
while(!q.empty()){
取队首元素top;
访问队首元素top;
队首元素出队;
将top下一层节点中未曾入队的节点全部入队,并设置为已入队;
}
}
eg1:给定一个m*n的矩阵,矩阵中的元素为0或1,称位置(x,y)与其上下左右四个位置(x,y+1),(x,y-1)(x+1,y)(x-1,y)是相邻的,(不必两两相邻,那么这些1就构成了一个块,求给定矩阵中的块的个数)
如该矩阵块为4
0 1 1 1 0 0 1
0 0 1 0 0 0 0
0 0 0 0 1 0 0
0 0 0 1 1 1 0
1 1 1 0 1 0 0
1 1 1 1 0 0 0
思路:枚举一个位置的元素,如果为0则跳过,如果为1,则使用BFS查询与该位置相邻的4个位置(不出界)判断是否为1,如果相邻位置为1,则同样去查询与该位置相邻的4个位置,直到整个1的块访问完毕,设置一个bool类型的数组inq,记录每个位置是否在BFS已经入过队
可以设置增量数组
int X[4] = { 0,0,1,-1 };
int Y[4] = { 1,-1,0,0 };
for (int i = 0; i < 4; i++) {
int newX = top.x + X[i];
int newY = top.y + Y[i];
}
来枚举四个方向,确定相邻的四个位置
#include
#include
using namespace std;
const int maxn = 100;
struct node {
int x, y;
}Node;
int n, m;
int matrix[maxn][maxn];
bool inq[maxn][maxn] = { false };//标记是否入过队
int X[4] = { 0,0,1,-1 };
int Y[4] = { 1,-1,0,0 };
bool judge(int x, int y) {
if (x >= n || x < 0 || y >= m || y < 0)return false;//越界
//当前位置为0或已经入过队
if (matrix[x][y] == 0 || inq[x][y] == true)return false;
return true;
}
void BFS(int x, int y) {
queue<node>Q;
Node.x = x, Node.y = y;
Q.push(Node);
inq[x][y] = true;
while (!Q.empty()) {
node top = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int newX = top.x + X[i];
int newY = top.y + Y[i];
if (judge(newX, newY)) {
Node.x = newX;
Node.y = newY;
Q.push(Node);
inq[newX][newY] = true;
}
}
}
}
int main() {
(void)scanf("%d %d", &n, &m);
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
(void)scanf("%d", &matrix[x][y]);
}
}
int ans = 0;
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
if (matrix[x][y] == 1 && inq[x][y] == false) {
ans++;
BFS(x, y);
}
}
}
printf("%d\n", ans);
return 0;
}
输出
4
相关例题:[PAT-A 1091]Acute Stroke