PAT Basic Level 1089 werewolf killing - simple version problem solving ideas and AC code v0.96

Keywords: Algorithm

1. Topic description and online test location

1.1 N people play werewolf killing, and each person explains the identity of others according to the number sequence from 1 to N. it is known that there are two werewolves in n people, two people lied when describing the identity of others, and only one werewolf lied. Judge who is a werewolf according to the above information
1.2 online test location: 1089 werewolf kill - simple version

2. Basic ideas

2.0 want to break the head series: at the beginning, code according to the idea of solving mathematical problems: the first person may have four states (people telling the truth, people telling the lie, wolves telling the truth and wolves telling the lie), deduce step by step according to the set state, and find that it can't be compiled at all... Finally, it is found that the correct posture should be: similar to solving equations (finding laws / equations), Try with all known conditions (computers are good at it) and find the conditions that meet the law / equation
2.1 the final result needs to meet the identity description (two of them lied, and one of them is a werewolf), and store the identity description information in the integer array. For example, PeopleWord[1]=6, the first player says that the sixth player is human; PeopleWord[3]=-4 the third player said that the fourth player played werewolf

	for (int i = 1; i <= N; i++)
	{
		cin >> Data;
		PeopleWord[i] = Data; 
	}

2.2 the computer can traverse all possible werewolf combinations: - 1 - 2, - 1 - 3, - 1 - 4, etc. The Werewolf identity is determined and everyone's identity status is determined

	for (i = 1; i < N; i++)
	{
		for (j = i + 1; j <= N; j++)
		{
			for (int i = 1; i <= N; i++)
				Status[i] = 1;
			Status[i] = Status[j] = -1;
		}
	}		

2.3 compare the identity status of each person with the identity description information: when the identity description information is inconsistent with the identity status, it indicates that the narrator corresponding to the identity status is lying

CountLiar = 0;
for (int k = 1; k <= N; k++)
{
	int tmp = PeopleWord[k];//the k said

	if ( Status[Abs(tmp)] * tmp < 0 ) //standard for tell lies
		Liar[CountLiar++] = k; //k tell lies
}

2.4 when the number of liars is 2 and only one of them plays werewolf, it means that everyone's identity status is correct, that is, the answer is found

if (CountLiar == 2 )
{
		if ((Liar[0] == i || Liar[0] == j) && (Liar[1] != i && Liar[1] != j))
		{
			cout << i << " " << j << endl;
			return 0;
		}					
		else if ((Liar[1] == i || Liar[1] == j) && (Liar[0] != i && Liar[0] != j))
		{
			cout << i << " " << j << endl;
			return 0;
		}
	}		

2.5 because it is traversed from small to large, the first result satisfying the condition is the minimum sequence required by the topic

3. Complete AC code

#include <iostream>
using namespace std;

#define MAX 101

int Abs(int n);

int main()
{
	int PeopleWord[MAX]; //Store everyone's description
	int N,Data;

	cin >> N;
	for (int i = 1; i <= N; i++)
	{
		cin >> Data;
		PeopleWord[i] = Data;
	}
	
	short Status[MAX]; //An array that stores identity status
	short Liar[MAX],CountLiar ; //Count Liar liar array counters
		
	int i, j;
	for (i = 1; i < N; i++)
	{
		for (j = i + 1; j <= N; j++) //Through a two-layer loop, traverse all possible werewolf combinations
		{
			//initialization
			for (int i = 1; i <= N; i++)
				Status[i] = 1;
			CountLiar = 0;
			Status[i] = Status[j] = -1; //werewolf

			for (int k = 1; k <= N; k++)
			{
				int tmp = PeopleWord[k];//the k said
	
				if ( Status[Abs(tmp)] * tmp < 0 ) //standard for tell lies
					Liar[CountLiar++] = k; //k tell lies
			}

			if (CountLiar == 2 )
			{
				if ((Liar[0] == i || Liar[0] == j) && (Liar[1] != i && Liar[1] != j))
				{
					cout << i << " " << j << endl;
					return 0;
				}					
				else if ((Liar[1] == i || Liar[1] == j) && (Liar[0] != i && Liar[0] != j))
				{
					cout << i << " " << j << endl;
					return 0;
				}
			}			
		}
	}

	cout << "No Solution";

	return 0;
}

int Abs(int n)
{
	if (n < 0)
		return n * -1;
	else
		return n;
}

Posted by champrock on Sat, 09 Oct 2021 00:36:22 -0700