poj1063 Flip and Shift
2011-11-21 09:25
260 查看
Flip and Shift
Description
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).
The goal of this puzzle is to gather the disks of the same color in adjacent positions using flips and shifts. (Figure 2)
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 file. 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
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5669 | Accepted: 2604 |
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).
The goal of this puzzle is to gather the disks of the same color in adjacent positions using flips and shifts. (Figure 2)
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 file. 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
Sample Output
YES NO
#include<stdio.h> #include<cmath> /* 如果是奇数,那么一定是‘YES’ 如果是偶数,则统计在奇数位上和偶数位上白棋的个数,如果他们相差小于2,那么也一定是'YES' */ int main() { int n; // freopen("E:\\c++\\oj\\t.txt","rt",stdin); scanf("%d",&n); while(n--) { int m,s[101],i; int ji=0,ou=0; scanf("%d",&m); if(m%2) { for(i=1;i<=m;i++) scanf("%d",&s[i]); printf("YES\n"); } else { for(i=1;i<=m;i++) { scanf("%d",&s[i]); if(!s[i] && i%2) ji++; if(!s[i] && !(i%2)) ou++; } if((ji-ou)<2 && (ji-ou)>-2) printf("YES\n"); else printf("NO\n"); } } return 0; }
相关文章推荐
- POJ1063 Flip and Shift
- zoj1028-Flip and Shift
- 模拟+搜索poj 1063 Flip and Shift
- JOJ 1051: Flip and Shift 解题报告
- pku1063 Flip and Shift
- Flip and Shift -- ACM PKU 1063 解题报告
- ACM学习-POJ-1063-Flip and Shift
- Flip and Shift,珠子交换问题(不给力的标题)
- poj 1063 Flip and Shift
- zoj 1028 Flip and Shift
- zoj 1028 Flip and Shift(数学)
- poj1063 Flip and Shift
- zoj 1028 Flip and Shift
- POJ 1063 Flip and Shift(找规律题)
- ZOJ 1028 Flip and Shift
- zoj1028 Flip and Shift
- ACM学习-POJ-1063-Flip and Shift
- [agc019d]Shift and Flip
- POJ 1063 Flip and Shift 已被翻译
- POJ-1063 Flip and Shift 数学分析&运用守恒定理