关闭

uva 565 Pizza Anyone?

标签: dfsDFS剪枝
767人阅读 评论(0) 收藏 举报
分类:

题目地址:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=506

题目描述:

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.
题意:

让所有人满意,其中每个人满足其中至少一个需求即可。

题解:

刚开始读这道题的时候感觉很简单,直接爆搜 看能不能过,但做得过程中,发现每个样例如果有满足所有人的情况,其字符串的答案不止一种,然后又受了样例输出的误导,以为要取满足情况中字符串长度最小的,然后看到字母序又以为要取所有串中,排序最靠前的那个答案串。并且读题没看到它说这题是special judge,在那纠结老半天,最后莫名其妙地1y,感觉这道题真真莫名其妙 也没考虑什么剪枝,直接搜就是,得到一个答案立即退出整个DFS。还有这题还真就是special judge。。。。。

我是以人为深度进行深搜,然后再存储形式上用了一维整数数组来表示A~P的喜好问题,然后以人为深度深搜下去,只要至少满足一个人的其中一个喜好就行了,直接跳到下个人的搜索中,到最后一个人,如果发现满足情况直接跳出整个DFS,输出答案就行,注意答案要字母顺序。其他没有什么限制条件。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iostream>
#include <algorithm>
#define LL 16//ABCDEFGHIJKLMNOP
using namespace std;
char instr[50+5]={'\0'};
int prefre[26+5]={0};//-1 is without  1  is  with  0 is not refer to this
int prefres[20+5][26+5]={0};//input the prefres
int prefrescnt=0;//maze 's count
char minstr[26+5]={'\0'};//output this or cmp for this
char prestr[26+5]={'\0'};//preference string but not must be minstr
int flag=0;//whether find the case satisfy all people at least one constraint
/*switch the string to the prefres int array*/
int switchpre(char str[],int ind)
{
	int i=0;
	while(str[i]!=';')
	{
		if(isupper(str[i]))
		{
			if(str[i-1]=='+')
				prefres[ind][str[i]-'A']=1;
			else
				prefres[ind][str[i]-'A']=-1;
		}
		i++;
	}
	return(0);
}
/*switch the array to string*/
int switchstr(char str[],int pre[])
{
	int i=0,k=0;
	str[0]='\0';
	for(i=0;i<=LL-1;i++)
	{
		if(pre[i]==1)
		{
			str[k]=i+'A';
			k++;
		}
	}
	str[k]='\0';
	return(0);
}
/*DFS the people constraint*/
int DFS(int cur)
{
	if(flag)	return(0);
	if(cur==prefrescnt)
	{
		switchstr(prestr,prefre);
		memcpy(minstr,prestr,sizeof(prestr));
		flag=1;
		return(0);
	}
	else
	{
		int i=0;
		for(i=0;i<=LL-1;i++)//pick a 1 or -1 add to the prefre array
		{
			if(prefres[cur][i]!=0)
			{
				if(prefre[i]==0)
				{
					prefre[i]=prefres[cur][i];
					DFS(cur+1);
					if(flag)	return(0);
					prefre[i]=0;
				}
				else if(prefre[i]==prefres[cur][i])//this privilege is higher than prefre[i]==0
				{
					DFS(cur+1);
					if(flag)	return(0);
				}
			}
		}
	}
	return(0);
}
/*for test*/
int test()
{
	return(0);
}
/*main process*/
int MainProc()
{
	while(scanf("%s",instr)!=EOF)
	{
		//init
		memset(prefre,0,sizeof(prefre));
		memset(prefres,0,sizeof(prefres));
		memset(minstr,'Z',sizeof(minstr));
		memset(prestr,'\0',sizeof(prestr));
		prefrescnt=0;
		switchpre(instr,prefrescnt);
		prefrescnt++;
		while(scanf("%s",instr)!=EOF&&instr[0]!='.')
		{
			switchpre(instr,prefrescnt);
			prefrescnt++;
		}
		flag=0;
		DFS(0);//the depth is people constraint
		if(flag)
		{
			printf("Toppings: %s\n",minstr );
		}
		else
		{
			printf("No pizza can satisfy these requests.\n");
		}
	}
	return(0);
}
int main(int argc, char const *argv[])
{
	/* code */
	MainProc();
	return 0;
}




0
0
查看评论

UVA - 565 Pizza Anyone?

题目大意:有16种材料,N个人,每个有都有喜欢和不喜欢的才
  • L123012013048
  • L123012013048
  • 2014-10-26 21:25
  • 601

UVa 565 - Pizza Anyone?

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=109&page=show_problem&problem=506 类型: 暴力枚举...
  • shuangde800
  • shuangde800
  • 2012-07-20 01:50
  • 1518

UVa 565 - Pizza Anyone?

題目:製作一個披薩,有幾個小夥伴他們都有喜歡喝不喜歡的材料列表,             製作一個披薩讓所有的人條件都至少滿足一個。 分析:搜索,狀態壓縮。一共65536種狀態直接利用二進制枚舉所有狀態,判斷是否滿足即可。 說明:還差...
  • mobius_strip
  • mobius_strip
  • 2016-09-05 15:50
  • 392

UVA 565 Pizza Anyone?

题意:一些人提出自己喜欢的pizza的要求,问是否存在对所有人至少满足一个条件的要求。这里要注意的是不存在某人不喜欢的东西也算一条满足这个人的要求。 分析:一共有16中东西,深搜最多2^16。用一个数的二进制表示当前的选择。 Code: #include #include #includ...
  • HRHACMER
  • HRHACMER
  • 2013-07-13 15:37
  • 597

uva 565 - Pizza Anyone?

点击打开链接 题目意思:   有16种甜品,现在要做一个批萨,有很多人,每个人喜欢的甜品各不相同,要求我们找到一个方案使得至少让每个人都能够满足一个要求,最后输出这个方案。 解题思路:   我么知道对于每一个toppings只有两种情况取和...
  • cgl1079743846
  • cgl1079743846
  • 2012-08-02 22:47
  • 1026

UVA 565 - Pizza Anyone?

#include #include int main() { char str[50]; while (scanf("%s", str) != EOF) { int n, status, num, person[100][2] = {0}, Max = (1 <...
  • kl28978113
  • kl28978113
  • 2014-08-30 17:32
  • 1531

uva uva 565 - Pizza Anyone?

此题大意:某个人要买一个pizza和朋友一起吃,每个人都有一些要求,要求是需要满足每个人至少一个要求。 注意点:对于某个人来说,若存在他不想要的但是存在他想要的是可以的,也可以存在没有他想要的,但是他不想要也没得。 这道题我用了4个多小时: 第一次:使用了集合+二进制枚举子集 ...
  • lovejava123qqq
  • lovejava123qqq
  • 2015-08-07 23:06
  • 214

UVA 565 565 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...
  • u011217342
  • u011217342
  • 2013-08-06 23:06
  • 1126

uva 565 - Pizza Anyone?(暴力枚举 + 二进制)

题目:uva 565 - Pizza Anyone?(暴力枚举 + 二进制) 题目大意:题目是说有一个人要帮他的朋友门定批萨,然后每个朋友都有自己的口味要求,问能不能定一个批萨然后满足每个朋友的至少一个要求,输出要求按字典序。 能就输出所定批萨里面加的东西; 不能就输出:No pizza can...
  • u012997373
  • u012997373
  • 2014-05-10 19:34
  • 673

UVA - 565 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...
  • HelloWorld10086
  • HelloWorld10086
  • 2014-08-23 20:39
  • 605
    个人资料
    • 访问:560104次
    • 积分:6027
    • 等级:
    • 排名:第4997名
    • 原创:145篇
    • 转载:54篇
    • 译文:0篇
    • 评论:98条
    最新评论