Eliminate the Conflict(HDU-4115)

本文探讨了在已知对手策略的情况下,如何通过2-SAT算法确定玩家是否能在剪刀石头布游戏中获胜,特别是在受到特定规则限制时。文章详细介绍了如何将游戏策略转化为2-SAT问题,并提供了一个具体的实现案例。

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

Problem Description

Conflicts are everywhere in the world, from the young to the elderly, from families to countries. Conflicts cause quarrels, fights or even wars. How wonderful the world will be if all conflicts can be eliminated.
Edward contributes his lifetime to invent a 'Conflict Resolution Terminal' and he has finally succeeded. This magic item has the ability to eliminate all the conflicts. It works like this:
If any two people have conflict, they should simply put their hands into the 'Conflict Resolution Terminal' (which is simply a plastic tube). Then they play 'Rock, Paper and Scissors' in it. After they have decided what they will play, the tube should be opened and no one will have the chance to change. Finally, the winner have the right to rule and the loser should obey it. Conflict Eliminated!
But the game is not that fair, because people may be following some patterns when they play, and if the pattern is founded by others, the others will win definitely.
Alice and Bob always have conflicts with each other so they use the 'Conflict Resolution Terminal' a lot. Sadly for Bob, Alice found his pattern and can predict how Bob plays precisely. She is very kind that doesn't want to take advantage of that. So she tells Bob about it and they come up with a new way of eliminate the conflict:
They will play the 'Rock, Paper and Scissors' for N round. Bob will set up some restricts on Alice.
But the restrict can only be in the form of "you must play the same (or different) on the ith and jth rounds". If Alice loses in any round or break any of the rules she loses, otherwise she wins.
Will Alice have a chance to win?

Input

The first line contains an integer T(1 <= T <= 50), indicating the number of test cases.
Each test case contains several lines.
The first line contains two integers N,M(1 <= N <= 10000, 1 <= M <= 10000), representing how many round they will play and how many restricts are there for Alice.
The next line contains N integers B1,B2, ...,BN, where Bi represents what item Bob will play in the ith round. 1 represents Rock, 2 represents Paper, 3 represents Scissors.
The following M lines each contains three integers A,B,K(1 <= A,B <= N,K = 0 or 1) represent a restrict for Alice. If K equals 0, Alice must play the same on Ath and Bth round. If K equals 1, she must play different items on Ath and Bthround.

Output

For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is "yes" or "no" represents whether Alice has a chance to win.

Sample Input

2
3 3
1 1 1
1 2 1
1 3 1
2 3 1
5 5
1 2 3 2 1
1 2 1
1 3 1
1 4 1
1 5 1
2 3 0

Sample Output

Case #1: no
Case #2: yes

题意:两个人玩剪刀石头布,Alice 已经知道 Bob 每次要出什么,一共玩 n 轮,首先给出 n 个数分别表示 Bob 每轮出拳,其中 1 代表剪刀,2 代表石头,3 代表布,Bob 对 Alice 做出一些限制:

给出 m 行,每行为 a、b、k,如果 k 是 0,代表 Alice 第 a 次与 b 次 出拳必须相同,如果 k 是 1,代表 Alice 第 a 次与第 b 次出拳必须不相同

一旦 Alice 破坏限制规则,或者输了一局,就代表 Alice 彻底输了,求根据所给输入判断 Alice 能否获胜

思路:由于 Alice 知道 Bob 的所有出拳结果,因此可用数组 a[N][2] 来保存出拳结果,然后对于 m 行要求作出 Alice 在第 i 轮选 0 还是 1 的推断即可,看是否有方法满足 n 个解

由于 Alice 一次也不能输,因此根据 Bob 的出拳,Alice 只能赢或者平局,即每次只有 2 种选择结果,由于有 3 种可供选择的情况,因此存在一些矛盾对:

假设第 a 次可以出 a1、a2,第 b 次可以出 b1、b2

1)若 k=0,要求 a、b 出拳相同,则:

  • 出拳 a1、b1 不同,矛盾,建立 <a1,b2>、<b1,a2>
  • 出拳 a1、b2 不同,矛盾,建立 <a1,b1>、<b2,a2>
  • 出拳 a2、b1 不同,矛盾,建立 <a2,b2>、<b1,a1>
  • 出拳 a2、b2 不同,矛盾,建立 <a2,b1>、<b2,a1>

2)若 k=1,要求 a、b 出拳不同,则:

  • 出拳 a1、b1 相同,矛盾,建立 <a1,b2>、 <b1,a2>
  • 出拳 a1、b2 相同,矛盾,建立 <a1,b1>、 <b2,a2>
  • 出拳 a2、b1 相同,矛盾,建立 <a2,b2>、 <b1,a1>
  • 出拳 a2、b2 相同,矛盾,建立 <a2,b1>、 <b2,a1>

根据如上规则,填入数据,进行 2-SAT 判断即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 16007
#define INF 0x3f3f3f3f
#define N 1001
#define LL long long
using namespace std;
bool vis[N*2];
int Stack[N*2],top;
vector<int> G[N*2];
void init(int n){
    memset(vis,false,sizeof(vis));
    for(int i=0;i<2*n;i++)
        G[i].clear();
}
void addAndClause(int x,int xval,int y,int yval) {
    x=x*2+xval;
    y=y*2+yval;
    G[x].push_back(y);
}
bool dfs(int x){
    if(vis[x^1])
        return false;

    if(vis[x])
        return true;
    vis[x]=true;

    Stack[top++]=x;

    for(int i=0;i<G[x].size();i++)
        if(!dfs(G[x][i]))
            return false;

    return true;
}
bool twoSAT(int n){
    for(int i=0;i<2*n;i+=2){
        if(!vis[i] && !vis[i+1]){
            top=0;

            if(!dfs(i)){
                while(top>0)/
                    vis[Stack[--top]]=false;

                if(!dfs(i+1))/
                    return false;
            }
        }
    }
    return true;
}
int a[N][2];
int main(){
    int t;
    int Case=1;
    scanf("%d",&t);

    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);

        init(n);
        for(int i=0;i<n;i++){
            scanf("%d",&a[i][0]);//Bob出拳的结果

            //Alice可能出拳的结果
            a[i][0]--;//与Bob打平,出拳情况1
            a[i][1]=(a[i][0]+1)%3;//取胜Bob,出拳情况2
        }

        while(m--){
            int x,y,k;
            scanf("%d%d%d",&x,&y,&k);
            x--,y--;

            if(k==0){//相同
                if(a[x][0]!=a[y][0]){//a1、b1不同
                    addAndClause(x,0,y,1);//建立<a1,b2>
                    addAndClause(y,0,x,1);//建立<b1,a2>
                }
                if(a[x][0]!=a[y][1]){//a1、b2不同
                    addAndClause(x,0,y,0);//建立<a1,b1>
                    addAndClause(y,1,x,1);//建立<b2,a2>
                }
                if(a[x][1]!=a[y][0]){//a2、b1不同
                    addAndClause(x,1,y,1);//建立<a2,b2>
                    addAndClause(y,0,x,0);//建立<b1,a1>
                }
                if(a[x][1]!=a[y][1]){//a2、b2不同
                    addAndClause(x,1,y,0);//建立<a2,b1>
                    addAndClause(y,1,x,0);//建立<b2,a1>
                }
            }
            else if(k==1){//不同
                if(a[x][0]==a[y][0]){//a1、b1相同
                    addAndClause(x,0,y,1);//建立<a1,b2>
                    addAndClause(y,0,x,1);//建立<b1,a2>
                }
                if(a[x][0]==a[y][1]){//a1、b2相同
                    addAndClause(x,0,y,0);//建立<a1,b1>
                    addAndClause(y,1,x,1);//建立<b2,a2>
                }
                if(a[x][1]==a[y][0]){//a2、b1相同
                    addAndClause(x,1,y,1);//建立<a2,b2>
                    addAndClause(y,0,x,0);//建立<b1,a1>
                }
                if(a[x][1]==a[y][1]){//a2、b2相同
                    addAndClause(x,1,y,0);//建立<a2,b1>
                    addAndClause(y,1,x,0);//建立<b2,a1>
                }
            }
        }

        printf("Case #%d: %s\n",Case++,twoSAT(n)?"yes":"no");
    }
    return 0;
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值