Ultraviolet A 565-pizza anyone?

Source: Internet
Author: User

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 109 & page = show_problem & problem = 506

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:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;char str[100][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;}

-- The meaning of life is to give it meaning.

 

Original Http://blog.csdn.net/shuangde800 , By d_double(For details, refer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.