Type: brute-force enumeration, search
Question:
You are responsible for ordering a large pizza for you and your friends. each of them has told you what he wants on a pizza and what he does not; of course they all understand that since there is only going to be one pizza, no one is likely to have all their requirements satisfied. can you order a pizza that will satisfy at least one request from all your friends?
The pizza parlor you are calling offers the following pizza toppings; you can include or omit any of them in a pizza:
Input Code Topping
A Anchovies
B Black Olives
C Canadian Bacon
D Diced Garlic
E Extra Cheese
F Fresh Broccoli
G Green Peppers
H Ham
I Italian Sausage
J Jalapeno Peppers
K Kielbasa
L Lean Ground Beef
M Mushrooms
N Nonfat Feta Cheese
O Onions
P Pepperoni
Your friends provide you with a line of text that describes their pizza preferences. For example, the line
+ O-H + P;
Reveals that someone will accept a pizza with onion, or without ham, or with pepperoni, and the line
-E-I-D + A + J;
Indicates that someone else will accept a pizza that omits extra cheese, or Italian sausage, or diced garlic, or that between des anchovies or jalapnos.
Question:
You are responsible for ordering a big pizza, but your friends have different requirements for what to add or not to add. But your friends also know that it is impossible to satisfy all requirements. Therefore, you need to select an ordering scheme so that everyone can meet at least one of the requirements. Note: If a person does not want to add one, it is a requirement.
Analysis and Summary:
There are only 16 things to choose from. For each kind of things, there are two statuses: select or not select Ze. The brute force enumeration method can be used.
Each binary digit of a number is fixed or not fixed. Enumeration 0 ~ (1 <16)-1.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Char str [1, 100] [2, 100];
Int nIndex;
Int main (){
# Ifdef LOCAL
Freopen ("input.txt", "r", stdin );
# Endif
While (gets (str [0]) {
NIndex = 1;
While (gets (str [nIndex]), str [nIndex ++] [0]! = '.');;
Int status = 0;
Bool flag = true;
Int maxNum = (1 <16)-1;
While (status <= maxNum ){
Flag = true;
For (int I = 0; I <nIndex-1; ++ I ){
Bool OK = false;
Int pos = 0;
While (pos <strlen (str [I]) {
If (str [I] [pos] = '+ '){
If (status >>( str [I] [pos + 1]-'A') & 1 ){
OK = true; break;
}
}
Else if (str [I] [pos] = '-'){
If (! (Status >>( str [I] [pos + 1]-'A') & 1 )){
OK = true;
Break;
}
}
Pos + = 2;
}
If (! OK) {flag = false; break ;}
}
If (flag) break;
++ Status;
}
If (! Flag) printf ("No pizza can satisfy these requests. \ n ");
Else {
Int pos = 0;
Printf ("Toppings :");
While (pos <16 ){
If (status & 1) printf ("% c", pos + 'A ');
++ Pos; status >>= 1;
}
Printf ("\ n ");
}
}
Return 0;
}
Author: shuangde800