POJ 1063 Flip and Shift 最详细的解题报告

题目来源:Flip and Shift

题目大意:一个椭圆形的环形容器中有黑色和白色两种盘子,问你是否可以将黑色的盘子连续的放在一起。你可以有以下两种操作:

1、顺时针旋转所有的盘子

2、顺时针旋转3个盘子

 

解题思路:第一种操作表明你可以对任意一个盘子执行第二种操作,所以重点在于第二种操作。仔细分析一下会发现,第二种操作其实就是将某一个盘子当前的index加2,所以我们可以先统计一下黑色盘子的个数count,然后用一个数组将所有的盘子存起来,使数组中0-count所存放的是黑色盘子(通过下标加2的操作),如果到最后都是黑色盘子,则输出YES,反之NO。

 

具体算法(java版,可以直接AC)

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         Scanner scanner = new Scanner(System.in);
 7         int casNum = scanner.nextInt();
 8         for (int i = 0; i < casNum; i++) {
 9             int len = scanner.nextInt();
10             int[] array = new int[len];
11             int count = 0;
12             for (int j = 0; j < len; j++) {
13                 array[j] = scanner.nextInt();
14                 if (array[j] == 1) {
15                     count++;
16                 }
17             }
18 
19             for (int j = count ; j < len; j++) {
20                 if (array[j] == 1) {
21                     int index = j;
22                     while (index >= count) {
23                         index = (index + 2) % len;
24                     }
25                     while (array[index] == 1 && index < count) {
26                         index = (index + 2) % len;
27                     }
28                     if (index < count) {
29                         int temp = array[index];
30                         array[index] = array[j];
31                         array[j] = temp;
32                     }
33                 }
34             }
35             boolean flag = true;
36             for (int j = 0; j < count; j++) {
37                 if (array[j] == 0) {
38                     flag = false;
39                     break;
40                 }
41             }
42             if (flag) {
43                 System.out.println("YES");
44             } else {
45                 System.out.println("NO");
46             }
47         }
48     }
49 
50 }

 

posted @ 2014-10-19 09:39  PinXiong  阅读(238)  评论(0编辑  收藏  举报