POJ 1166 The Clocks(暴力搜索)
2012-09-24 23:58
274 查看
ACM选修课布置的第二题,第一题貌似是贪心+枚举,看着题意挺纠结的,而且没看懂题意就先放下了。这题之前在USACO上貌似做过类似的题,当时写的很恶心,这次写这题的时候思路很轻松,写起来也比较简单。这题也没多想,直接枚举没种操作的次数,因为执行四次同样的操作和没执行过的状态是一样的,而且每种操作的执行顺序和最终结果无关,所以每种操作的状态最多就有4种,总共就种操作,最多就4^9种情况,暴力枚举无压力也就没写bfs,感觉bfs要快很多,算了,不在水题上纠结了……
#include <iostream> #include <string.h> #include <cstdio> #include <cmath> using namespace std; int clock[10],ans; int temp[10],step[10]; int turn[9][9]= { {1,1,0,1,1,0,0,0,0}, {1,1,1,0,0,0,0,0,0}, {0,1,1,0,1,1,0,0,0}, {1,0,0,1,0,0,1,0,0}, {0,1,0,1,1,1,0,1,0}, {0,0,1,0,0,1,0,0,1}, {0,0,0,1,1,0,1,1,0}, {0,0,0,0,0,0,1,1,1}, {0,0,0,0,1,1,0,1,1} }; void dfs(int value,int deep) { if(deep==9) { bool ok=true; for(int i=0;i<9;i++) { if(clock[i]%4!=0) { ok=false; break; } } if(ok) { if(ans>value) { ans=value; memcpy(step,temp,sizeof(step)); } } return; } for(int i=0;i<4;i++) { temp[deep]=i; for(int j=0;j<9;j++) clock[j]=(clock[j]+turn[deep][j]*i)%4; dfs(i+1,deep+1); for(int j=0;j<9;j++) clock[j]=(clock[j]-turn[deep][j]*i)%4; } } int main() { for(int i=0; i<9; i++) { scanf("%d",&clock[i]); } ans=100000; dfs(0,0); bool ok=true; for(int i=0;i<9;i++) { while(step[i]--) { if(ok) { printf("%d",i+1); ok=false; } else printf(" %d",i+1); } } printf("\n"); }
相关文章推荐
- poj 1166 The Clocks 暴力搜索
- C语言中的拨钟问题(枚举)(暴力搜索)POJ1166
- poj 1166 The Clocks (暴力|| dfs)
- POJ 1166 The Clocks(搜索枚举)
- poj 1166 The Clocks 暴力枚举
- poj_1166_The Clocks(暴力)
- POJ 1166 暴力搜索 即 枚举
- poj 1166 The Clocks
- POJ 1054 暴力搜索
- poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)
- POJ 2965 The Pilots Brothers' refrigerator(暴力搜索)
- poj 1166 简单搜索
- POJ 1753 Flip Game 暴力搜索(dfs加枚举)
- poj1014.Dividing (暴力 && 搜索)
- POJ 1416 Shredding Company [暴力搜索 简单剪枝]
- POJ 1753 (暴力搜索)
- POJ 1166 The clocks
- POJ 2329 (暴力+搜索bfs)
- POJ 1166 The Clocks(高斯消元)
- POJ 1166 The Clocks(拨钟问题)