PAT(A) 1148 Werewolf - Simple Version(Java)逻辑推理

题目链接:1148 Werewolf - Simple Version (20 point(s))

Description

Werewolf(狼人杀) is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game,

player #1 said: “Player #2 is a werewolf.”;
player #2 said: “Player #3 is a human.”;
player #3 said: “Player #4 is a werewolf.”;
player #4 said: “Player #5 is a human.”; and
player #5 said: “Player #4 is a human.”.

Given that there were 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liars. Can you point out the werewolves?

Now you are asked to solve a harder version of this problem: given that there were N players, with 2 werewolves among them, at least one but not all the werewolves were lying, and there were exactly 2 liars. You are supposed to point out the werewolves.

Input Specification

Each input file contains one test case. For each case, the first line gives a positive integer N (5≤N≤100). Then N lines follow and the i-th line gives the statement of the i-th player (1≤i≤N), which is represented by the index of the player with a positive sign for a human and a negative sign for a werewolf.

Output Specification

If a solution exists, print in a line in ascending order the indices of the two werewolves. The numbers must be separated by exactly one space with no extra spaces at the beginning or the end of the line. If there are more than one solution, you must output the smallest solution sequence – that is, for two sequences A=a[1],…,a[M] and B=b[1],…,b[M], if there exists 0≤k

Sample Case

Sample Input 1
5
-2
+3
-4
+5
+4
Sample Output 1
1 4
Sample Input 2
6
+6
+3
+1
-5
-2
+4
Sample Output 2 (the solution is not unique)
1 5
Sample Input 3
5
-2
-3
-4
-5
-1
Sample Output 3
No Solution

分析?

假设某两位是狼,判断他们是否是一真一假,再判断人是否只有一假,满足以上条件说明成立。否则不成立。

代码?

/**********************************************************************************
Submit Time			Status		Score	Problem	Compiler		Run Time	User
7/28/2019, 12:35:51	Accepted	20		1089	Java (openjdk)	82 ms		wowpH
**********************************************************************************/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	// w1,w2是狼,word是玩家说的话
	private static boolean judge(int w1, int w2, int word) {// 判断是否是真话
		if (word < 0 && (0 == word + w1 || 0 == word + w2)) {
			return true;						// 语义:某位玩家是狼,并且他真的是狼
		} else if (word > 0 && word != w1 && word != w2) {
			return true;						// 语义:某位玩家是人,并且他真的是人
		}
		return false;									// 否则是谎话
	}

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());		// 玩家人数
		int[] word = new int[n + 1];					// 玩家说的话
		for (int i = 1; i <= n; ++i) {
			word[i] = Integer.parseInt(br.readLine());
		}

		boolean find = false;							// 初始未找到狼
		for (int i = 1; i < n && !find; ++i) {			// 假设第i位是狼,且未找到狼
			for (int j = i + 1; j <= n && !find; ++j) {	// 假设第j位是狼,且未找到狼
				boolean word1 = judge(i, j, word[i]);	// i说的话
				boolean word2 = judge(i, j, word[j]);	// j说的话
				if (word1 && !word2 || !word1 && word2) {// 一真一假
					int liePeopleNum = 0;				// 说谎的“好人”数
					// 遍历“好人”,找到说谎的“好人”数
					for (int k = 1; k <= n && liePeopleNum <= 1; ++k) {
						if (k != i && k != j && !judge(i, j, word[k])) {
							++liePeopleNum;		// k(“好人”)说假话,说谎的“好人”数加1
						}
					}
					if (1 == liePeopleNum) {			// 有且仅有一个“好人”说谎
						find = true;					// 已找到
						System.out.println(i + " " + j);// i和j是狼
					}
				}
			}
		}
		if (false == find) {
			System.out.println("No Solution");			// 未找到,无解
		}
	}
}

版权声明

  1. 转载、参考、引用必须在首页添加如下文字:
    [PAT(A) 1148 Werewolf - Simple Version(Java)逻辑推理—wowpH](https://blog.csdn.net/pfdvnah/article/details/95731165)
  2. 代码原创,公开引用不能删除首行注释(作者,版本号,时间等信息);
  3. 如果有疑问欢迎评论区留言,尽量解答;
  4. 如果有错误,还望大侠评论区指正。

你可能感兴趣的:(#,PAT(A),编程语言,#,Java,#,逻辑推理)