POJ 1166 The Clocks
来源:互联网 发布:淘宝pc客户端是什么 编辑:程序博客网 时间:2024/05/19 18:16
高斯消元第四题,这次的题很精彩~~
题目大意:
给出9个钟表的状态,给出九种操作,问最少要操作几次能把所有的钟表调回12点。
解题思路:
对于9个钟表分别列方程,然后高斯消元即可。由于这次左边的方程系数不是0就是1,所以不用找最大值~
下面是代码:
#include <set>#include <map>#include <queue>#include <math.h>#include <vector>#include <string>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#define eps 1e-6#define pi acos(-1.0)#define inf 107374182#define inf64 1152921504606846976#define lc l,m,tr<<1#define rc m + 1,r,tr<<1|1#define iabs(x) ((x) > 0 ? (x) : -(x))#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))#define clearall(A, X) memset(A, X, sizeof(A))#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))#define memcopyall(A, X) memcpy(A , X ,sizeof(X))#define max( x, y ) ( ((x) > (y)) ? (x) : (y) )#define min( x, y ) ( ((x) < (y)) ? (x) : (y) )using namespace std;struct node{ long long num[15]; node() { clearall(num,0); } void clen() { clearall(num,0); }};struct node matrix[15];int n,m,len;bool free_x[15];long long X[15],p;void Debug(void){ puts(""); int i, j; for (i = 0; i < m; i++) { for (j = 0; j < n + 1; j++) { cout << matrix[i].num[j] << " "; } cout << endl; } cout << endl;}int Guass(){ int i,j,k,col; clearall(X,0); clearall(free_x,1);//把解集清空,所有变量都标为自由变量 for (k = 0,col = 0; k < m && col < n; ++k, ++col) //枚举行列 { //printf("%d\n",k); //Debug(); int max_r = k;//找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差) while(matrix[max_r].num[col]==0&&max_r<m)max_r++; /*for (i = k + 1; i < m; ++i) { if (iabs(matrix[i].num[col]) > iabs(matrix[max_r].num[col])) max_r = i; }*/ if (max_r != k) //交换 { for (i = k; i < n + 1; ++i) swap(matrix[k].num[i],matrix[max_r].num[i]); } /*if (matrix[k].num[col]!=0 ) //如果对应该列都为0,枚举该行的下一列 { k--; continue; }*/ for (i = k + 1; i < m; ++i) //将k后边的col进行初等变换成行阶梯矩阵 { if (matrix[i].num[col]!=0) { long long x1=matrix[i].num[col],x2=matrix[k].num[col]; for (j = col; j < n + 1; ++j) { matrix[i].num[j] = matrix[i].num[j] *x2- x1*matrix[k].num[j]; matrix[i].num[j] = (matrix[i].num[j]%p+p)%p; } //Debug(); } } } //Debug(); // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0). 即R(A) != R(A')无解 /*for (i = k; i < m; ++i) { if (iabs(matrix[i].num[col]) >eps) return -1; }*/ // 2. 无穷解的情况: 在n * (n + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵. // 且出现的行数即为自由变元的个数. 即R(A) = R(A') < n //printf("%d %d\n",k,n); /*if (k < n) { //注释处为求多解的自由变量 // 首先,自由变元有n - k个,即不确定的变元至少有n - k个. int num = 0,freeidx; for (i = k - 1; i >= 0; --i) { num = 0;// 用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元. double tmp = matrix[i].num[n]; // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第m行. // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的. for (j = 0; j < n; ++j) { if (iabs(matrix[i].num[j]) > eps && free_x[j]) { num++; freeidx = j; } } if (num > 1) continue; // 无法求解出确定的变元. // 说明就只有一个不确定的变元free_index,那么可以求解出该变元,且该变元是确定的. tmp = matrix[i].num[n]; for (j = 0; j < n; ++j) { if (iabs(matrix[i].num[j])>eps && j != freeidx) tmp -= matrix[i].num[j]*X[j]; } X[freeidx] = tmp/matrix[i].num[freeidx]; free_x[freeidx] = 0; } return n - k; }*/ // 3. 唯一解的情况: 在n * (n + 1)的增广阵中形成严格的上三角阵. // 计算出Xn-1, Xn-2 ... X0. for (i = k - 1; i >= 0; --i) { long long tmp = matrix[i].num[n]; for (j = i + 1; j < n; ++j) { tmp =((tmp- matrix[i].num[j]*X[j])%p+p)%p; } while(tmp%matrix[i].num[i])tmp+=p; X[i] = ((tmp/matrix[i].num[i])%p+p)%p; } return 0;}const char s[9][10]= {"ABDE","ABC","BCEF","ADG","BDEFH","CFI","DEGH","GHI","EFHI"};const int num[9]= {4,3,4,3,5,3,4,3,4};int main(){ p=4; n=9; m=9; clearall(matrix,0); for(int i=0; i<9; i++) { scanf("%d",&matrix[i].num[9]); matrix[i].num[9]=(4-matrix[i].num[9])%4; for(int j=0; j<num[i];j++) { matrix[s[i][j]-'A'].num[i]=1; } } //Debug(); Guass(); bool flat=false; //Debug(); for(int i=0; i<n; i++) { //printf("%lld\n",X[i]); for(int j=0;j<X[i];j++) { if(flat)printf(" "); printf("%d",i+1); flat=true; } } //Debug(); puts(""); return 0;}
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 The Clocks 1166
- 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 1166 The Clocks 暴力枚举
- zip4j -- Java处理zip压缩文件的完整解决方案
- voj1565 环形dp 记忆化搜索
- 时间带给他们的不是遗忘,而是深深爱上
- hdu2846 字典树 判子串
- .net类型反射与晚期绑定
- POJ 1166 The Clocks
- 三句话
- Android2.3.4内核编译完整记录
- 12.解决CCScale9Sprite或者CCControlButton无法使用的问题。
- 各品牌电脑U盘启动的快捷键总结
- 张瑞敏谈人不成熟的五个特征
- (萌O(∩_∩)O)哈希知识点小结
- iOS后台定位实现
- Android 带你从源码的角度解析Scroller的滚动实现原理