链接
n个人, n个描述,+5代表5号人是好人,共两个狼人,两个说谎,其中一个是狼人,求可能的方案
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n+1);
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
vector<int> lie, a(n + 1, 1);
a[i] = a[j] = -1;
for (int k = 1; k <= n; k++)
if (v[k] * a[abs(v[k])] < 0) lie.push_back(k);
if (lie.size() == 2 && a[lie[0]] + a[lie[1]] == 0) {
cout << i << " " << j;
return 0;
}
}
}
cout << "No Solution";
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
int a[maxn], n, b[maxn], c[maxn];
pair<int, int> res;
bool cmp(pair<int,int> a, pair<int,int> b){
if(a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
bool check(int i, int j){
memset(b, 0, sizeof(b));
vector<int> tmp;
for(int k=1;k<=n;k++) a[k] = c[k]; //初始化
a[i] = -a[i]; a[j] = -a[j]; //说谎者
for(int k=1;k<=n;k++){
if(b[abs(a[k])] == 0){ //未被标记过
b[abs(a[k])] = a[k]>0?1:-1;
}
else if(b[abs(a[k])] != (a[k]>0?1:-1)){
return false;
} //两人说法矛盾
}
if(b[i] > 0) return false; //有一个必须是狼人
for(int k=1;k<=n;k++){
if(b[k]<=0) tmp.push_back(k);
}
if(tmp.size() != 2) return false; //狼人数不为2
res = make_pair(tmp[0], tmp[1]);
return true;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>c[i];
}
bool flag = 0;
vector<pair<int,int> > ans;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(check(i,j)){
flag = 1;
ans.push_back(res);
}
if(check(j, i)){
flag = 1;
ans.push_back(res);
}
}
}
if(!flag){
cout<<"No Solution"<<endl;
}else{
sort(ans.begin(), ans.end(), cmp);
printf("%d %d\n", ans[0].first, ans[0].second);
}
}
PAT A1048 Werewolf - Simple Version [硬核模拟]
原文:https://www.cnblogs.com/doragd/p/11454804.html