USACO 6.5 The Clocks

The Clocks
IOI‘94 - Day 2

Consider nine clocks arranged in a 3x3 array thusly:

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

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

|-------|    |-------|    |-------|
|       |    |       |    |       |
|   O   |    |   O---|    |   O   |
|   |   |    |       |    |   |   |
|-------|    |-------|    |-------|
    G            H            I

The goal is to find a minimal sequence of moves to return all the dials to 12 o‘clock. Nine different ways to turn the dials on the clocks are supplied via a table below; each way is called a move. Select for each move a number 1 through 9 which will cause the dials of the affected clocks (see next table) to be turned 90 degrees clockwise.

Move Affected clocks
1 ABDE
2 ABC
3 BCEF
4 ADG
5 BDEFH
6 CFI
7 DEGH
8 GHI
9 EFHI

Example

Each number represents a time according to following table:

9 9 12       9 12 12       9 12 12        12 12 12      12 12 12
6 6 6  5 ->  9  9  9  8->  9  9  9  4 ->  12  9  9  9-> 12 12 12
6 3 6        6  6  6       9  9  9        12  9  9      12 12 12

[But this might or might not be the `correct‘ answer; see below.]

PROGRAM NAME: clocks

INPUT FORMAT

Lines 1-3: Three lines of three space-separated numbers; each number represents the start time of one clock, 3, 6, 9, or 12. The ordering of the numbers corresponds to the first example above.

SAMPLE INPUT (file clocks.in)

9 9 12
6 6 6
6 3 6

OUTPUT FORMAT

A single line that contains a space separated list of the shortest sequence of moves (designated by numbers) which returns all the clocks to 12:00. If there is more than one solution, print the one which gives the lowest number when the moves are concatenated (e.g., 5 2 4 6 < 9 3 1 1).

SAMPLE OUTPUT (file clocks.out)

4 5 8 9

————————————————————————————————————题解4^9果断暴搜然后秒过……用了一个指向函数的指针减少代码量
  1 /*
  2 LANG: C++
  3 PROG: clocks
  4 */
  5 #include <iostream>
  6 #include <cstdio>
  7 #include <algorithm>
  8 #include <cstring>
  9 #include <cmath>
 10 #define siji(i,x,y) for(int i=(x) ; i <= (y) ; ++i)
 11 #define xiaosiji(i,x,y) for(int i = (x) ; i < (y); ++i)
 12 #define gongzi(j,x,y) for(int j = (x) ; j >= (y) ; --j)
 13 #define ivorysi
 14 #define mo 11447
 15 #define eps 1e-8
 16 #define o(x) ((x)*(x))
 17 using namespace std;
 18 typedef long long ll;
 19 int clo[4][4],rec[10],ans[10],all=10000;
 20 void span(int &a) {
 21     a=(a+1)%4;
 22 }
 23 bool check() {
 24     siji(i,1,3) {
 25         siji(j,1,3) {
 26             if(clo[i][j]!=3)return false;
 27         }
 28     }
 29     return true;
 30 }
 31 void del1() {
 32     span(clo[1][1]);
 33     span(clo[1][2]);
 34     span(clo[2][1]);
 35     span(clo[2][2]);
 36 }
 37 void del2() {
 38     span(clo[1][1]);
 39     span(clo[1][2]);
 40     span(clo[1][3]);
 41 }
 42 void del3() {
 43     span(clo[1][2]);
 44     span(clo[1][3]);
 45     span(clo[2][2]);
 46     span(clo[2][3]);
 47 }
 48 void del4() {
 49     span(clo[1][1]);
 50     span(clo[2][1]);
 51     span(clo[3][1]);
 52 }
 53 void del5() {
 54     span(clo[1][2]);
 55     span(clo[2][1]);
 56     span(clo[2][2]);
 57     span(clo[2][3]);
 58     span(clo[3][2]);
 59 }
 60 void del6() {
 61     span(clo[1][3]);
 62     span(clo[2][3]);
 63     span(clo[3][3]);
 64 }
 65 void del7() {
 66     span(clo[2][1]);
 67     span(clo[2][2]);
 68     span(clo[3][1]);
 69     span(clo[3][2]);
 70 }
 71 void del8() {
 72     span(clo[3][1]);
 73     span(clo[3][2]);
 74     span(clo[3][3]);
 75 }
 76 void del9() {
 77     span(clo[2][2]);
 78     span(clo[2][3]);
 79     span(clo[3][2]);
 80     span(clo[3][3]);
 81 }
 82 void dfs(int dep,int times) {
 83     if(times>=all) return;
 84     if(dep>9) {
 85         if(!check()) return;
 86         all=times;
 87         memcpy(ans,rec,sizeof(rec));
 88         return;
 89     }
 90     void (*cur)();
 91     if(dep==1) cur=&del1;
 92     else if(dep==2) cur=&del2;
 93     else if(dep==3) cur=&del3;
 94     else if(dep==4) cur=&del4;
 95     else if(dep==5) cur=&del5;
 96     else if(dep==6) cur=&del6;
 97     else if(dep==7) cur=&del7;
 98     else if(dep==8) cur=&del8;
 99     else if(dep==9) cur=&del9;
100
101     siji(i,1,3) {
102         (*cur)();
103         rec[dep]=i;
104         dfs(dep+1,times+i);
105     }
106     (*cur)();
107     rec[dep]=0;
108     dfs(dep+1,times);
109 }
110 void solve() {
111     siji(i,1,3) {
112         siji(j,1,3) {
113             scanf("%d",&clo[i][j]);
114             clo[i][j]/=3;
115             --clo[i][j];
116         }
117     }
118     int cnt=0;
119     dfs(1,0);
120     siji(i,1,9) {
121         siji(j,1,ans[i]) {
122             ++cnt;
123             printf("%d%c",i," \n"[cnt==all]);
124         }
125     }
126 }
127 int main(int argc, char const *argv[])
128 {
129 #ifdef ivorysi
130     freopen("clocks.in","r",stdin);
131     freopen("clocks.out","w",stdout);
132 #else
133     freopen("f1.in","r",stdin);
134     //freopen("f1.out","w",stdout);
135 #endif
136     solve();
137     return 0;
138 }
 
时间: 2024-08-06 11:24:37

USACO 6.5 The Clocks的相关文章

【USACO 1.4.2】时钟

[题目描述] 考虑将如此安排在一个 3 x 3 行列中的九个时钟: |-------| |-------| |-------| | | | | | | | |---O | |---O | | O | | | | | | | |-------| |-------| |-------| A B C |-------| |-------| |-------| | | | | | | | O | | O | | O | | | | | | | | | | |-------| |-------| |---

洛谷 P1213 时钟 &amp;&amp;IOI 1994 The Clocks

IOI 1994 The Clocks 题目描述 考虑将如此安排在一个 3 x 3 行列中的九个时钟: 目标要找一个最小的移动顺序将所有的指针指向12点.下面原表格列出了9种不同的旋转指针的方法,每一种方法都叫一次移动.选择1到9号移动方法,将会使在表格中对应的时钟的指针顺时针旋转90度. 移动方法 受影响的时钟 1 ABDE 2 ABC 3 BCEF 4 ADG 5 BDEFH 6 CFI 7 DEGH 8 GHI 9 EFHI Example [但这可能不是正确的方法,请看下面] 输入输出格

COGS 696. [IOI1996][USACO 2.3] 最长前缀

★   输入文件:prefix.in   输出文件:prefix.out   简单对比时间限制:1 s   内存限制:128 MB 描述 USACO 2.3.1 IOI96 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 P 中的元素可以通过串联(元素可以重复使用,相当于 Pascal 中的 “+” 运算符)组成一个序列 S ,那么我们认为序列 S 可以分解为 P 中的元素.元素不一定要全部出现(如下例中B

USACO prefix TrieTree + DP

/* ID:kevin_s1 PROG:prefix LANG:C++ */ #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <map> #include <set> #include <algorithm> #include <cstdlib>

【USACO 1.3.4】牛式

[題目描述 ] 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ---------- * * * * * * ---------- * * * * 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0. 注意一下在美国的学校中教的"部分乘积",第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序找出所有的牛式. [格式] INPUT FORMAT: (f

USACO Chapter 1 Section 1.1

USACO的题解和翻译已经很多了... 我只是把自己刷的代码保存一下. 1.PROB Your Ride Is Here 1 /* 2 ID:xiekeyi1 3 PROG:ride 4 LANG:C++ 5 */ 6 7 #include<bits/stdc++.h> 8 using namespace std ; 9 10 int main() 11 { 12 freopen("ride.in","r",stdin); 13 freopen(&quo

USACO Your Ride Is Here

[USACO]Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do,

usaco月赛,2017.1总结

T1:跳舞的奶牛 大致题意:一个体积为k的舞台能够同时容纳k只奶牛一起跳舞,他们每头奶牛的跳舞时间不同,如果有一只奶牛跳完了第k+1头奶牛就会立刻上场跳舞,当所有奶牛跳完舞以后我们认为这次表演结束.现在给出奶牛个数,最多用时,每头奶牛的跳舞时间.求舞台最小为多大. 思路:本来写了个程序以为这道题很简单,刚开始排一下序然后就行了,结果交了以后发现只过了五组,然后才发现这道题不能改变顺序(所以说为什么我改变顺序了还是能过五组,usaco的数据也好水......),所以说我想到了堆,然后就用堆写了一下

插入排序的优化【不靠谱地讲可以优化到O(nlogn)】 USACO 丑数

首先我们先介绍一下普通的插排,就是我们现在一般写的那种,效率是O(n^2)的. 普通的插排基于的思想就是找位置,然后插入进去,其他在它后面的元素全部后移,下面是普通插排的代码: 1 #include<iostream> 2 #include<fstream> 3 #include<stdio.h> 4 using namespace std; 5 int a[200000]; 6 int p[200000]; 7 8 int main(){ 9 ios::sync_wi