592-Island of Logic

Source: Internet
Author: User
I haven't figured out how to do it for a long time, and I don't understand other people's code ...... The following is a reprinted code of another person: a maximum of five people, plus time, can be enumerated in six cycles. During input, the content of the statement can be determined based on a specific character, for example, 'divine' with 'D '. To determine whether the statement is valid, You must judge it based on the speaker's identity. If all statements are valid, it is possible to enumerate them. Save them and judge them after enumeration. # Include <iostream> # include <cstdio> # include <string> # include <cstring> using namespace std; struct Statement {char speaker, object, nature; // The nature of the speaker, declarative object, and object bool whether; // whether}; Statement s [60]; int res [5] [4]; // Save the enumerated result void input (int num) {string str1, str2; getline (cin, str1); for (int I = 0; I <str1.length (); I ++) // remove all spaces in the statement if (str1 [I]! = '') Str2 + = str1 [I]; s [num]. speaker = str2 [0]; if (str2 [3] = 'T') {s [num]. object = 'T'; // The statement object is time if (str2 [7] = 'O') {s [num]. whether = false; s [num]. nature = str2 [9];} else {s [num]. whether = true; s [num]. nature = str2 [6] ;}} else {if (str2 [2] = 'I') s [num]. object = s [num]. speaker; else s [num]. object = str2 [2]; if (str2 [5] = 'n') {s [num]. whether = false; s [num]. nature = str2 [8];} else {s [num]. whether = true; s [num]. natur E = str2 [5] ;}} bool judge (int _ fact, int t, bool whether, char statement) // checks whether the nature of the statement matches the fact {char fact; if (_ fact = 1) fact = 'D'; else if (_ fact = 2) fact = 'E'; else fact = 'H '; if (statement = 'l' & whether) {if (fact = 'E' | fact = 'H' & t = 2) // if you tell the other party to lie, the other party must be a demon or a human in the evening. return 1; else return 0;} if (statement = 'l '&&! Whether) // if the other party does not lie, then the other party must be a priest or a human being during the day to make a correct statement {if (fact = 'D' | fact = 'H' & t = 1) return 1; else return 0;} if (statement = fact) = whether) // The statement is true or false. return 1; else return 0 ;} bool islegal (int num, int A, int B, int C, int D, int E, int t) {bool stateistrue; // record statement if (s [num]. object = 'T') // if the statement is time {char time; if (t = 1) time = 'D'; else time = 'n '; if (s [num]. nature = time) = (s [num]. whether) // statement is true or false Teistrue = 1; else stateistrue = 0;} else {switch (s [num]. object) {case 'A': stateistrue = judge (A, t, s [num]. whether, s [num]. nature); break; case 'B': stateistrue = judge (B, t, s [num]. whether, s [num]. nature); break; case 'C': stateistrue = judge (C, t, s [num]. whether, s [num]. nature); break; case 'D': stateistrue = judge (D, t, s [num]. whether, s [num]. nature); break; case 'E': stateistrue = judge (E, t, s [num]. whether, s [num]. nature ); Break ;}}if (stateistrue) // determine whether the statement is correct. You must end the statement with the speaker's identity to confirm whether the statement is valid {switch (s [num]. speaker) {case 'A': if (A = 1 | A = 3 & t = 1) return 1; else return 0; case 'B ': if (B = 1 | B = 3 & t = 1) return 1; else return 0; case 'C ': if (C = 1 | C = 3 & t = 1) return 1; else return 0; case 'D ': if (D = 1 | D = 3 & t = 1) return 1; else return 0; case 'E ': if (E = 1 | E = 3 & t = 1) return 1; else return 0 ;}} else {switch (s [num]. speaker) {c Ase 'A': if (A = 2 | A = 3 & t = 2) return 1; else return 0; case 'B ': if (B = 2 | B = 3 & t = 2) return 1; else return 0; case 'C ': if (C = 2 | C = 3 & t = 2) return 1; else return 0; case 'D ': if (D = 2 | D = 3 & t = 2) return 1; else return 0; case 'E ': if (E = 2 | E = 3 & t = 2) return 1; else return 0 ;}} int main () {int I, j, n, cases = 0; while (cin> n & n) {getchar (); cases ++; cout <"Conversation #" <cases <endl; for (I = 0; I <n; I ++) {Input (I); // input statement I} memset (res, 0, sizeof (res); int day = 0, night = 0; for (int A = 1; A <= 3; A ++) // 1, 2, and 3 represent divine, edevil, human for (int B = 1; B <= 3; B ++) for (int C = 1; C <= 3; C ++) for (int D = 1; D <= 3; D ++) for (int E = 1; E <= 3; E ++) for (int t = 1; t <= 2; t ++) // 1 indicates daytime, 2 indicates night {int OK = 1; for (I = 0; I <n; I ++) if (islegal (I, A, B, c, D, E, t) {}// judge whether the statement is legal else {OK = 0; break;} if (OK) {res [0] [A] = 1; res [1] [B] = 1; res [2] [C] = 1; res [3] [D] = 1; res [4] [E] = 1; if (t = 1) day = 1; else night = 1 ;}} int impossible = 0, nodeducible = 1; for (I = 0; I <5; I ++) {int cnt = 0, index; for (j = 1; j <= 3; j ++) if (res [I] [j]) {cnt ++; index = j;} if (cnt = 0) {impossible = 1; break;} else if (cnt = 1) {char person = I + 'a'; if (index = 1) cout <person <"is divine. "<endl; else if (index = 2) cout <person <" is edevil. "<endl; else cout <person <" is human. "<endl; no Deducible = 0 ;}} if (! Day &&! Night) impossible = 1; else if (day &&! Night) {cout <"It is day." <endl; nodeducible = 0;} else if (night &&! Day) {cout <"It is night. "<endl; nodeducible = 0;} if (impossible) cout <" This is impossible. "<endl; else if (nodeducible) cout <" No facts are deducible. "<endl; putchar ('\ 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.