标签:sample ima clu char put array range order mini
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 |
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.]
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. |
9 9 12 6 6 6 6 3 6
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).
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 }
标签:sample ima clu char put array range order mini
原文地址:http://www.cnblogs.com/ivorysi/p/7041115.html