ZOJ 1028 Flip and Shift

本文介绍了一种由黑白磁盘组成的环形谜题,玩家可通过翻转三个连续磁盘或整体顺时针移动来操作。目标是将相同颜色的磁盘聚集在一起。文章提供了一个判断初始状态是否能达成目标状态的程序实现。
Flip and Shift

Time Limit: 2 Seconds                                     Memory Limit: 65536 KB                            

This puzzle consists of a random sequence of m black disks and   n white disks on an oval-shaped track, with a turnstile capable of flipping   (i.e., reversing) three consecutive disks. In Figure 1, there are 8 black disks and 10  white disks on the track. You may spin the turnstile to flip the three disks   in it or shift one position clockwise for each of the disks on the track (Figure 1).





  Figure 1. A flip and a shift



  The goal of this puzzle is to gather the disks of the same color in adjacent   positions using flips and shifts. (Figure 2)





  Figure 2. A goal sequence



  You are to write a program which decides whether a given sequence can reach   a goal or not. If a goal is reachable, then write a message ��YES��; otherwise, write a message ��NO��.


Input

  The input consists of T test cases. The number of test cases ) (T is given in   the first line of the input. Each of the next T lines gives a test case. A  test case consists of an integer, representing the sum of m and n, and a sequence  of m+n 0s and 1s, representing an initial sequence. A 0 denotes a white disk and  a 1 denotes a black disk. The sum of m and n is at least 10 and does not exceed 30.  There is a space between numbers.


Output

  The output should print either ��YES�� or ��NO�� for each test case, one per line.   


Sample Input

  2
  18 0 0 1 0 1 1 1 1 0 1 0 0 1 0 0 0 0 1
  14 1 1 0 0 1 1 1 0 0 1 1 0 1 0


Output for the Sample Input

YES
  NO

 

找规律 分析的太棒了http://www.cnblogs.com/scau20110726/archive/2013/06/12/3133078.html,类似的题目 一定要注意分析问题的 实质,抓住问题的核心,就能找到突破口。

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int T,n,val;
    cin>>T;
    while(T--)
    {
        int tot0=0;
        int tot1=0;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>val;
            if(val&&i&1)
                tot1++;
            else if(val&&i%2==0)
                tot0++;
        }

        if(n&1||(n%2==0&&abs(tot1-tot0)<=1))
        {
            cout<<"YES"<<endl;continue;
        }
        else
            cout<<"NO"<<endl;
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值