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

博客围绕狼人杀逻辑推理题展开,给定N个玩家,其中有2个狼人,至少一个但非全部狼人说谎,且恰好有2个说谎者。介绍了输入输出规范和示例,分析解题思路为假设某两位是狼,判断真假情况,还提及代码版权声明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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<M such that a[i]=b[i] (i≤k) and a[k+1]<b[k+1], then A is said to be smaller than B. In case there is no solution, simply print No Solution.

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.youkuaiyun.com/pfdvnah/article/details/95731165)
  2. 代码原创,公开引用不能删除首行注释(作者,版本号,时间等信息);
  3. 如果有疑问欢迎评论区留言,尽量解答;
  4. 如果有错误,还望大侠评论区指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值