UVa 565:pizza Anyone?

Source: Internet
Author: User
Tags bool printf

Topic Link:

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

Type: Brute Force enumeration, search

Topic:

You are are responsible for ordering a large pizza for you and your friends. Each of the them has told you what him wants on a pizza and what his does not; Of course they all understand this since there is only going to being one pizza, no one is likely to have all their NTS satisfied. Can you order a pizza that would satisfy at least one request to all your friends?

The pizza parlor you are calling offers the following pizza; You can include or omit any of the them in a pizza:

Your friends provide with a line of text of this describes their pizza preferences. For example, the line

+o-h+p;

Reveals that someone would accept a pizza with onion, or without ham, or with pepperoni, and the line

-e-i-d+a+j;

Indicates that someone else would accept a pizza that omits extra cheese, or Italian sausage, or diced garlic, or that incl Udes anchovies or jalapenos.

The main effect of the topic:

You are responsible for ordering a large pizza, but your friends have different requirements for what to add or not to add. But your friends know that it's impossible to meet all the requirements. So you have to choose a subscription scheme that allows everyone to meet at least one of these requirements. Note that if someone doesn't want one, then it's a requirement to not add that.

Analysis and Summary:

There are only 16 things to choose from, and for each thing is to choose or not to select Ze two states. Then the violent enumeration method can be completely done.

With a number, its various bits are expressed in a definite or indefinite order. 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; } 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; }

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.