Pizza Anyone?
Pizza Anyone? |
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 includes anchovies or jalapenos.
Input
The input consists of a series of pizza constraints.A pizza constraint is a list of 1 to 12 topping constraint lists each on a line by itself followed by a period on a line by itself.
A topping constraint list is a series of topping requests terminated by a single semicolon.
An topping request is a sign character (+/-) and then an uppercase letter from A to P.
Output
For each pizza constraint, provide a description of a pizza that satisfies it. A description is the string `` Toppings: " in columns 1 through 10 and then a series of letters, in alphabetical order, listing the toppings on the pizza. So, a pizza with onion, anchovies, fresh broccoli and Canadian bacon would be described by:Toppings: ACFO
If no combination toppings can be found which satisfies at least one request of every person, your program should print the string
No pizza can satisfy these requests.
on a line by itself starting in column 1.
Sample Input
+A+B+C+D-E-F-G-H; -A-B+C+D-E-F+G+H; -A+B-C+D-E+F-G+H; . +A+B+C+D; +E+F+F+H; +A+B-G; +O+J-F; +H+I+C; +P; +O+M+L; +M-L+P; . +A+B+C+D; +E+F+F+H; +A+B-G; +P-O; +O+J-F; +H+I+C; +P; +O; +O+M+L; -O-P; +M-L+P; .
Sample Output
Toppings: Toppings: CELP No pizza can satisfy these requests.
一个披萨可以选择16种材料,每个有不同要求给出一个方案每个人至少有一个要求得到满足,如果某人不想要哪个,那么不添加那个也算是满足了他的一个要求。简单回溯
后来看到别人别人直接用二进制2^16,枚举那样编码结构思路更清晰,
#include<string.h>
#include<stdio.h>
#define begin freopen("a.in","r",stdin); freopen("a.out","w",stdout);
#define end fclose(stdin); fclose(stdout);
#define p1 for (i=0;i<16;i++) if (Food[i]==1) printf("%c",i+'A'); printf("\n");
struct node
{int like[16];
}a[100];
int ok,n=1,visit[100],Food[16],ii;
void dfs(int people,int food)
{
int i,j,yes=0,no=0,temp[100],k,num,f=0;
if (ok==0) return ;
if (people==n) {ok=0;printf("Toppings: "); p1;}
if (food>15) return ;
for (k=-1;k<=1;k=k+2)//k=-1表示不要,1表示要
{
num=0;
for (i=1;i<=n;i++)
{
temp[i]=visit[i];
if ((visit[i]==0)&&(a[i].like[food]==k)) {++num; visit[i]=1;}
}
if (num)
{
++f;
Food[food]=k;
dfs(people+num,food+1);
Food[food]=0;
for (i=1;i<=n;i++)
visit[i]=temp[i];
}
}
if (f==0) dfs(people,food+1); //当前材料选择与非都不会满足任何一个人的要求
}
int main()
{char ch1,ch2;
int i,j;
memset(a,0,sizeof(a));
while (scanf("%c",&ch1)!=EOF)
{
if (ch1==';') {++n; getchar();}
else
if (ch1=='.')
{
getchar(); ok=1; --n;
memset(visit,0,sizeof(visit));
memset(Food,0,sizeof(Food));
dfs(0,0);
n=1;
memset(a,0,sizeof(a));
if (ok) printf("No pizza can satisfy these requests.\n");
}
else
{
scanf("%c",&ch2);
if (ch1=='+') a[n].like[ch2-'A']=1;
else a[n].like[ch2-'A']=-1;
}
}
return 0;
}