Flip and Shift

本文介绍了一种磁盘翻转谜题的解决算法,该谜题要求通过翻转和移动磁盘来使相同颜色的磁盘相邻。文中提供了两种算法实现方案,一种基于黑白磁盘数量的奇偶性判断,另一种则通过交换操作实现目标状态。

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

 

比较通用的解决方案:

 

另一种方法:

### Flip-Flops in Digital Electronics Flip-flops are fundamental components in digital electronics, serving as the basic building blocks for sequential logic circuits. They are used to store binary data and are essential in the design of registers, counters, and memory units. Flip-flops are bistable multivibrators, meaning they have two stable states, which can be used to represent binary data (0 or 1). ### Types of Flip-Flops There are several types of flip-flops, each with its own characteristics and applications: - **SR Flip-Flop (Set-Reset)**: This is one of the simplest types of flip-flops. It has two inputs, Set (S) and Reset (R), and two outputs, Q and Q'. The SR flip-flop can be constructed using NAND or NOR gates. It has three possible input combinations: set, reset, and hold. However, the SR flip-flop has an undefined state when both S and R are active simultaneously[^1]. - **D Flip-Flop (Data or Delay)**: The D flip-flop is widely used in digital systems for data storage. It has a single data input (D), a clock input (CLK), and outputs Q and Q'. The D flip-flop captures the value of the D input at the rising or falling edge of the clock signal and holds it until the next clock edge. This makes it ideal for use in shift registers and data storage applications. - **JK Flip-Flop**: The JK flip-flop is an enhanced version of the SR flip-flop that eliminates the undefined state. It has two inputs, J and K, which correspond to the Set and Reset functions. When both J and K are high, the flip-flop toggles its state on the clock edge. This feature allows the JK flip-flop to be used in a wide range of applications, including counters and shift registers. - **T Flip-Flop (Toggle)**: The T flip-flop is a specialized type of JK flip-flop where the J and K inputs are tied together. It has a single input (T), and when T is high, the flip-flop toggles its state on the clock edge. The T flip-flop is commonly used in binary counters. ### Applications of Flip-Flops Flip-flops are used in various applications within digital electronics: - **Registers**: Flip-flops are used to build registers, which are used to store multiple bits of data. A register is typically composed of a series of D flip-flops connected in parallel, with a common clock signal controlling the storage of data. - **Counters**: Flip-flops are used to construct counters, which count the number of clock pulses or events. Counters can be designed to count up, count down, or follow a specific sequence. Binary counters, ring counters, and Johnson counters are examples of counters built using flip-flops. - **Memory Units**: In larger systems, flip-flops are used as the basic storage elements in memory units. They form the basis of static random-access memory (SRAM), where each memory cell consists of several transistors configured as flip-flops. - **Synchronization**: Flip-flops are used to synchronize signals between different clock domains. As mentioned earlier, when signals are passed between different clock domains, metastability can occur. To mitigate this issue, synchronizing flip-flops are used to ensure that the signals are properly aligned with the destination clock domain. - **State Machines**: Flip-flops are used to implement finite state machines (FSMs), which are used to control the operation of digital systems. FSMs use flip-flops to store the current state and transition to the next state based on input conditions. In summary, flip-flops are crucial in the design and implementation of digital circuits, providing the necessary functionality for data storage, synchronization, and control in a variety of digital systems. ```verilog // Example of a D Flip-Flop in Verilog module d_flip_flop ( input clk, input d, output reg q ); always @(posedge clk) begin q <= d; end endmodule ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值