Poj-1166-The Clocks-枚举
来源:互联网 发布:给文件加密的算法 编辑:程序博客网 时间:2024/06/11 00:52
题面:
http://poj.org/problem?id=1166
大意:
给出一个3x3的棋盘,每个格子上有一个指针,要么指向12点(0)要么指向3点(1)要么指向6点(2)要么指向9点(3)。有9个旋转的方案,编号1-9,如1方案为将a11、a12、a13、a21上的指针顺时针旋转90度。问找出操作数最少的方案序列,使得每个格子上的指针都指向12点
AC代码:
int mat[4][4];//矩阵int times[10];//每种操作执行的次数bool check(int x1,int x2,int x3,int x4,int x5,int x6,int x7,int x8,int x9)//检验是否都指向了12点{ if((mat[1][1] + x1 + x2 + x4) % 4 != 0) return false; if((mat[1][2] + x1 + x2 + x3 + x5) % 4 != 0) return false; if((mat[1][3] + x2 + x3 + x6) % 4 != 0) return false; if((mat[2][1] + x1 + x4 + x5 + x7) % 4 != 0) return false; if((mat[2][2] + x1 + x3 + x5 + x7 + x9) % 4 != 0) return false; if((mat[2][3] + x3 + x5 + x6 + x9) % 4 != 0) return false; if((mat[3][1] + x4 + x7 + x8) % 4 != 0) return false; if((mat[3][2] + x5 + x7 + x8 + x9) % 4 != 0) return false; if((mat[3][3] + x6 + x8 + x9) % 4 != 0) return false; return true;}int main(){ //输入矩阵 for(int i = 1; i <= 3; ++i) for(int j = 1; j <= 3; ++j) cin >> mat[i][j]; //枚举每种操作执行的次数,只能执行0-3次否则没意义 for(int x1 = 0; x1 <= 3; ++x1) for(int x2 = 0; x2 <= 3; ++x2) for(int x3 = 0; x3 <= 3; ++x3) for(int x4 = 0; x4 <= 3; ++x4) for(int x5 = 0; x5 <= 3; ++x5) for(int x6 = 0; x6 <= 3; ++x6) for(int x7 = 0; x7 <= 3; ++x7) for(int x8 = 0; x8 <= 3; ++x8) for(int x9 = 0; x9 <= 3; ++x9) if(check(x1,x2,x3,x4,x5,x6,x7,x8,x9) == true)//检验 { //记录执行的次数 times[1] = x1; times[2] = x2; times[3] = x3; times[4] = x4; times[5] = x5; times[6] = x6; times[7] = x7; times[8] = x8; times[9] = x9; break; } //输出 for(int i = 1; i <= 9; ++i) { int t = times[i]; for(int j = 1; j <= t; ++j) printf("%d ",i); } return 0;}
解决方法:
指针是在转圈圈,总是在0-3之间取值,于是就想到对4取模
为什么会选择枚举呢,一是数据规模很小,二是想不到其他的好方法
绘制一个矩阵,将每种操作对矩阵元素造成的影响写上,如下图,就可以进行枚举了
阅读全文
0 0
- Poj-1166-The Clocks-枚举
- poj 1166 The Clocks 暴力枚举
- POJ 1166 The Clocks(搜索枚举)
- POJ 1166 The clocks
- POJ 1166 The Clocks
- POJ 1166 The Clocks
- POJ 1166 The Clocks
- poj 1166 The Clocks
- POJ 1166 The Clocks
- poj 1166 The Clocks
- poj 1166 The Clocks
- poj 1166 The Clocks
- poj The Clocks 1166
- POJ 1166 The Clocks
- POJ 1166:The Clocks
- POJ 1166 The Clocks
- POJ The Clocks 1166
- POJ 1166 The Clocks 笔记
- EDA时序电路的Verilog设计
- Hive的体系结构
- [leetcode] 第16周作业
- 把一个java程序打成jar包
- 使用Random对象生成n个[a,b]区间的随机数
- Poj-1166-The Clocks-枚举
- Java学习笔记(2)
- 使用有道API完成在线翻译功能
- 多少人的感情败给了这四个字
- 五分钟掌握微信小程序轮播图
- MySql关键字汇总
- L3
- 内存溢出和内存泄漏的区别、产生原因以及解决方案
- 单片机温习