Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴了个模板,改了改,过了 附代码#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<cmath> #include<queue> using namespace std; typedef long long LL; const double pi=acos(-1.0); int equ[30][31]; int ans[30]; // 有equ个方程,var个变元。增广阵行数为equ, 分别为0到equ - 1,列数为var + 1,分别为0到var. // 解集.x[maxn]; // 判断是否是不确定的变元. //------------------------------------ inline int gcd(int a, int b) { int t; while (b != 0) { t = b; b = a % b; a = t; } return a; } inline int lcm(int a, int b) { return a * b / gcd(a, b); } // 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数) void Gauss(int equ,int a[][31],int x[]) { int i, j, k; int var=equ; int max_r; // 当前这列绝对值最大的行. int col; // 当前处理的列. int ta, tb; int LCM; int temp; // 转换为阶梯阵. col = 0; // 当前处理的列. for (k = 0; k < equ && col < var; k++, col++) { // 枚举当前处理的行. // 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差) max_r = k; for (i = k + 1; i < equ; i++) { if (abs(a[i][col]) > abs(a[max_r][col])) max_r = i; } if (max_r != k) { // 与第k行交换. for (j = k; j < var + 1; j++) swap(a[k][j], a[max_r][j]); } if (a[k][col] == 0) { // 说明该col列第k行以下全是0了,则处理当前行的下一列. k--; continue; } for (i = k + 1; i < equ; i++) { // 枚举要删去的行. if (a[i][col] != 0) { LCM = lcm(abs(a[i][col]), abs(a[k][col])); ta = LCM / abs(a[i][col]), tb = LCM / abs(a[k][col]); if (a[i][col] * a[k][col] < 0) tb = -tb; // 异号的情况是两个数相加. for (j = col; j < var + 1; j++) { a[i][j] = (a[i][j] * ta - a[k][j] * tb+10000)%2; } } } } // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵. // 计算出Xn-1, Xn-2 ... X0. for (i = var - 1; i >= 0; i--) { temp = a[i][var]; for (j = i + 1; j < var; j++) { if (a[i][j] != 0) temp -= a[i][j] * x[j]; } if(a[i][i]) x[i] = temp / a[i][i]; else x[i]=0; x[i]=(x[i]+10000)%2; } } int di[]={0,1,0,-1}; int dj[]={1,0,-1,0}; bool inlim(int x,int y) { if(x<0 || x>=5 || y<0 || y>=6) return false; return true; } int main() { //freopen("input.txt","r",stdin); int cases,tc=0; scanf("%d",&cases); while(cases--) { tc++; printf("PUZZLE #%d\n",tc); memset(equ,0,sizeof equ); memset(ans,0,sizeof ans); for(int i=0;i<5;i++) { for(int j=0;j<6;j++) { scanf("%d",&equ[i*6+j][30]); equ[i*6+j][i*6+j]=1; for(int k=0;k<4;k++) { int ni=i+di[k]; int nj=j+dj[k]; if(inlim(ni,nj)) { equ[i*6+j][ni*6+nj]=1; } } } } Gauss(30,equ,ans); for(int i=0;i<30;i++) { printf("%d",ans[i]); if((i+1)%6!=0) printf(" "); else printf("\n"); } } //fclose(stdin); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator