zoukankan      html  css  js  c++  java
  • poj 1166 The Clocks (暴搜)

    The Clocks
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15944   Accepted: 6493

    Description

    |-------|    |-------|    |-------|
    
    | | | | | | |
    |---O | |---O | | O |
    | | | | | |
    |-------| |-------| |-------|
    A B C

    |-------| |-------| |-------|
    | | | | | |
    | O | | O | | O |
    | | | | | | | | |
    |-------| |-------| |-------|
    D E F

    |-------| |-------| |-------|
    | | | | | |
    | O | | O---| | O |
    | | | | | | | |
    |-------| |-------| |-------|
    G H I
    (Figure 1)

    There are nine clocks in a 3*3 array (figure 1). The goal is to return all the dials to 12 o'clock with as few moves as possible. There are nine different allowed ways to turn the dials on the clocks. Each such way is called a move. Select for each move a number 1 to 9. That number will turn the dials 90' (degrees) clockwise on those clocks which are affected according to figure 2 below. 
    Move   Affected clocks
    

    1 ABDE
    2 ABC
    3 BCEF
    4 ADG
    5 BDEFH
    6 CFI
    7 DEGH
    8 GHI
    9 EFHI
    (Figure 2)

    Input

    Your program is to read from standard input. Nine numbers give the start positions of the dials. 0=12 o'clock, 1=3 o'clock, 2=6 o'clock, 3=9 o'clock.

    Output

    Your program is to write to standard output. Output a shortest sorted sequence of moves (numbers), which returns all the dials to 12 o'clock. You are convinced that the answer is unique.

    Sample Input

    3 3 0
    2 2 2
    2 1 2

    Sample Output

    4 5 8 9

    分析:开始想通BFS,但是还要一个很大的数组记录路径,写半天没写出来。所以看别人的博客,copy了以下方法。主要是先进行哪种移动,后进行哪种移动,对结果没有影响,所以可以按照固定的顺序去搜索。

    Java AC 代码

    import java.util.Scanner;
    
    public class TheClocks_1166 {
        
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int a[] = new int[10];          //数组a是输入的数据
            for(int i = 1; i <= 9; i++) {
                a[i] = sc.nextInt();
            }
            int b[] = new int[10];         
            int c[] = new int[10];
            for(b[1]=0;b[1]<=3;b[1]++)      //数组b用来存储9种移动方式,其中每种都可以使用0~3次,然后暴力搜索下去
            for(b[2]=0;b[2]<=3;b[2]++)      //共 4的9次方 种可能
            for(b[3]=0;b[3]<=3;b[3]++)      //因为AAB移动和ABA移动所造成的结果一样,故顺序可以固定
            for(b[4]=0;b[4]<=3;b[4]++)
            for(b[5]=0;b[5]<=3;b[5]++)
            for(b[6]=0;b[6]<=3;b[6]++)
            for(b[7]=0;b[7]<=3;b[7]++)
            for(b[8]=0;b[8]<=3;b[8]++)
            for(b[9]=0;b[9]<=3;b[9]++)  
            {
               c[1]=(a[1]+b[1]+b[2]+b[4])%4;  //数组c表示移动之后的钟表的状态
               c[2]=(a[2]+b[1]+b[2]+b[3]+b[5])%4; //加上移动方式对某一个钟表造成的影响
               c[3]=(a[3]+b[2]+b[3]+b[6])%4;
               c[4]=(a[4]+b[1]+b[4]+b[5]+b[7])%4;
               c[5]=(a[5]+b[1]+b[3]+b[5]+b[7]+b[9])%4;
               c[6]=(a[6]+b[3]+b[5]+b[6]+b[9])%4;
               c[7]=(a[7]+b[4]+b[7]+b[8])%4;
               c[8]=(a[8]+b[5]+b[7]+b[8]+b[9])%4;
               c[9]=(a[9]+b[6]+b[8]+b[9])%4;
               if(c[1]+c[2]+c[3]+c[4]+c[5]+c[6]+c[7]+c[8]+c[9]==0) //当都是0的时候,输出
               {
                 for(int i=0;i<b[1];i++) System.out.print("1 ");  
                 for(int i=0;i<b[2];i++) System.out.print("2 ");
                 for(int i=0;i<b[3];i++) System.out.print("3 ");
                 for(int i=0;i<b[4];i++) System.out.print("4 ");
                 for(int i=0;i<b[5];i++) System.out.print("5 ");
                 for(int i=0;i<b[6];i++) System.out.print("6 ");
                 for(int i=0;i<b[7];i++) System.out.print("7 ");
                 for(int i=0;i<b[8];i++) System.out.print("8 ");
                 for(int i=0;i<b[9];i++) System.out.print("9 ");
                 System.out.print("
    ");
                 return;
               }
            }    
        }
    }
  • 相关阅读:
    RecyclerView,ListView first item get focus
    一种粗暴快速的Android全屏幕适配方案(转)
    谈谈单元测试之(一):为什么要进行烦人的单元测试(转)
    SCIgen与野鸡期刊的梗
    百度搜索仍然是【最快解决工作问题的方法】
    搭建minima主题的github博客网站
    本科期间的第一个专利:改卷筒的故事
    学习消费思维的【贫穷与富有】
    我的联系方式
    学习 GitHub 有什么好处?
  • 原文地址:https://www.cnblogs.com/kkkkkk/p/5539086.html
Copyright © 2011-2022 走看看