893AChess For Three

来源:互联网 发布:卫浴行业数据 编辑:程序博客网 时间:2024/06/07 05:09

模拟

题意

三个人下棋,每次两个人下,赢的人和第三个人下棋。,每次记录赢的那个人。
第一次只能第一个alex和Bob下,他们其中一个胜利的人,然后和carl下,我们就用carl替换输掉bob,每次用看比赛的人替换掉输掉的那个人。

                A. Chess For Three                time limit per test1 second                memory limit per test256 megabytes                inputstandard input                outputstandard output

Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it’s a bit difficult for them because chess is a game for two players, not three.

So they play with each other according to following rules:

Alex and Bob play the first game, and Carl is spectating;
When the game ends, the one who lost the game becomes the spectator in the next game, and the one who was spectating plays against the winner.
Alex, Bob and Carl play in such a way that there are no draws.

Today they have played n games, and for each of these games they remember who was the winner. They decided to make up a log of games describing who won each game. But now they doubt if the information in the log is correct, and they want to know if the situation described in the log they made up was possible (that is, no game is won by someone who is spectating if Alex, Bob and Carl play according to the rules). Help them to check it!

Input
The first line contains one integer n (1 ≤ n ≤ 100) — the number of games Alex, Bob and Carl played.

Then n lines follow, describing the game log. i-th line contains one integer ai (1 ≤ ai ≤ 3) which is equal to 1 if Alex won i-th game, to 2 if Bob won i-th game and 3 if Carl won i-th game.

Output
Print YES if the situation described in the log was possible. Otherwise print NO.

Examples
input
3
1
1
2
output
YES
input
2
1
2
output
NO
Note
In the first example the possible situation is:

Alex wins, Carl starts playing instead of Bob;
Alex wins, Bob replaces Carl;
Bob wins.
The situation in the second example is impossible because Bob loses the first game, so he cannot win the second one.

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int main(){    int n,a,b,c,temp,q[110];    int flag;    while(cin>>n)    {        for(int i=0; i<n; i++)            cin>>q[i];        a=1,b=2,c=3;        for(int i=0; i<n; i++)        {            flag=0;            if(q[i]==a||q[i]==b)              {                if(q[i]==a)                {                    temp=b;                    b=c;                    c=temp;                }                if(q[i]==b)                {                    temp=a;                    a=c;                    c=temp;                }                flag=1;            }            if(!flag)                break;        }        if(!flag)            puts("NO");        else            puts("YES");    }    return 0;}
原创粉丝点击