1.枚举、暴力
2.二分查找
3.广度优先搜索
特点:经常被用来解决最优值问题;常用队列;注意要剪枝【对搜过的状态进行标记】
例2:
小明有体积为s(<101)的可乐和容量为n,m的两个空杯子【无刻度且s == n + m】。
问:能否平分可乐?能则打印倒可乐的最少次数,否则打印“NO”
输入:s,n,m
#include<stdio.h>
#include<queue>
using namespace std;
int arr[100][100][100];
int mark[100][100][100];
struct cup{
int a, b, c;
int t;
};
queue<cup> Q;
//倾倒函数,由容量为volumeA的杯子倒向容量为volumeB的杯子。
//引用的方式调用ab,初始时为原始杯子中可乐的体积,函数调用完毕后,为各自杯子中可乐的新体积。
void AToB (int &a, int volumeA, int &b, int volumeB) {
if (volumeB -b >= a) {//若a可以全部倒到b中
b += a;
a = 0;
}
else {//否则
a -= volumeB - b;
b = volumeB;
}
}
int BFS (int s, int m, int n) {
while (!Q.empty()) {
cup now = Q.front();
Q.pop();
int a, b, c;
//a倒入b
a = now.a;
b = now.b;
c = now.c;
AToB(a, s, b, n);
if (mark[a][b][c] == false) {
mark[a][b][c] == true;
cup tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = now.t + 1;//生成新的状态
if (a == s / 2 && b == s / 2 || b == s / 2 && c == s / 2 || a == s / 2 || c == s / 2)
return tmp.t;
Q.push(tmp);//没找到,则放入队列
}
//b倒入a,注意abc的值要置为now的值
a = now.a;
b = now.b;
c = now.c;
AToB (b, n, a, s);
if (mark[a][b][c] == false) {
mark[a][b][c] == true;
cup tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = now.t + 1;//生成新的状态
if (a == s / 2 && b == s / 2 || b == s / 2 && c == s / 2 || a == s / 2 || c == s / 2)
return tmp.t;
Q.push(tmp);//没找到,则放入队列
}
//a倒入c,注意abc的值要置为now的值
a = now.a;
b = now.b;
c = now.c;
AToB(a, s, c, m);
if (mark[a][b][c] == false) {
mark[a][b][c] == true;
cup tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = now.t + 1;//生成新的状态
if (a == s / 2 && b == s / 2 || b == s / 2 && c == s / 2 || a == s / 2 || c == s / 2)
return tmp.t;
Q.push(tmp);//没找到,则放入队列
}
//c倒入a,注意abc的值要置为now的值
a = now.a;
b = now.b;
c = now.c;
AToB(c, m, a, s);
if (mark[a][b][c] == false) {
mark[a][b][c] == true;
cup tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = now.t + 1;//生成新的状态
if (a == s / 2 && b == s / 2 || b == s / 2 && c == s / 2 || a == s / 2 || c == s / 2)
return tmp.t;
Q.push(tmp);//没找到,则放入队列
}
//b倒入c,注意abc的值要置为now的值
a = now.a;
b = now.b;
c = now.c;
AToB(b, n, c, m);
if (mark[a][b][c] == false) {
mark[a][b][c] == true;
cup tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = now.t + 1;//生成新的状态
if (a == s / 2 && b == s / 2 || b == s / 2 && c == s / 2 || a == s / 2 || c == s / 2)
return tmp.t;
Q.push(tmp);//没找到,则放入队列
}
//c倒入b,注意abc的值要置为now的值
a = now.a;
b = now.b;
c = now.c;
AToB(c, m, b, n);
if (mark[a][b][c] == false) {
mark[a][b][c] == true;
cup tmp;
tmp.a = a;
tmp.b = b;
tmp.c = c;
tmp.t = now.t + 1;//生成新的状态
if (a == s / 2 && b == s / 2 || b == s / 2 && c == s / 2 || a == s / 2 || c == s / 2)
return tmp.t;
Q.push(tmp);//没找到,则放入队列
}
}//while结束
return -1;//都没找到,返回-1.
}
int main () {
int s, n, m;
while (scanf("%d%d%d",&s, &n, &m) != EOF) {
if (s == 0)
break;
if (s & 0x1) {//为奇数不可能平分
puts("NO");
continue;
}
//初始化开始:1.mark置false 2.abc置初始值 3.队列清空!
for (int i = 0; i <= s; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= m; k++) {
mark[i][j][k] = false;
}
}
}
cup tmp;
tmp.a = s;
tmp.b = tmp.c = tmp.t = 0;
while (!Q.empty()) {
Q.pop();
}
//初始化结束
Q.push(tmp);
mark[s][0][0] = true;//标记初始状态!!!!!
int ans = BFS(s, n, m);
if (ans == -1)
puts("NO");
else
printf("%d\n", ans);
}//while结束
return 0;
}
4.递归
5.深度优先搜索