hiho一下 第五十七周 高斯消元·二(高斯消元解异或方程组)
2015-08-02 10:48
393 查看
//package project1; import java.io.*; import java.util.*; public class Main{ FastScanner in; int[][] A; int[] x; int Major[]; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; void Gauss(int equ,int var){ int row,col; for(row=col=0;row<equ&&col<var;row++,col++){ int max_r=row; for(int i=row+1;i<equ;i++){ if(A[row][col]==1)break; if(A[i][col]==1){max_r=i;break;} } if(max_r!=row){ for(int j=col;j<var+1;j++){ int temp=A[max_r][j]; A[max_r][j]=A[row][j]; A[row][j]=temp; } } if(A[row][col]==0){ row--;continue; } for(int i=row+1;i<equ;i++){ if(A[i][col]==0)continue; for(int j=col;j<var+1;j++){ A[i][j]^=A[row][j]; } } Major[row]=col; } dfs_freevar(var,row-1,col-1); } void print(){ int ans=0; for(int i=0;i<30;i++) if(x[i]==1)ans++; System.out.println(ans); for(int i=0;i<30;i++){ if(x[i]==1){ int xx=i/6+1,yy=i%6+1; System.out.println(xx+" "+yy); } } } void dfs_freevar(int n,int r,int var){ if(r==-1||var==-1){ print(); return; } if(Major[r]==var){ int y=A[r] ; for(int i=var+1;i<n;i++) { y^=(x[i]*A[r][i]); } x[var]=y; dfs_freevar(n,r-1,var-1); } else{ for(int i=0;i<2;i++){ x[var]=i; dfs_freevar(n,r,var-1); } } } void input(){ in=new FastScanner(System.in); A=new int[35][35]; x=new int[35]; Major=new int[35]; String s=new String(); int cnt=0; for(int i=0;i<5;i++){ s=in.nextLine(); for(int j=0;j<6;j++){ if(s.charAt(j)=='1')A[cnt++][30]=0; else A[cnt++][30]=1; } } for(int i=0;i<5;i++){ for(int j=0;j<6;j++){ A[i*6+j][i*6+j]=1; for(int k=0;k<4;k++){ int xx=dx[k]+i,yy=dy[k]+j; if(xx>=0&&xx<5&&yy>=0&&yy<6){ A[xx*6+yy][i*6+j]=1; } } } } Gauss(30,30); } int max(int a,int b){ if(a>b)return a; else return b; } int min(int a,int b){ if(a>b)return b; return a; } public static void main(String args[]){ new Main().input(); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
相关文章推荐
- Appium - WebView测试(Android)
- 单例模式
- 为什么margin-top值不是作用域父元素
- 假期
- php-面向对象(3)
- 余弦推倒
- 并查集的介绍及实现
- 概率霍夫变换来检测直线——HoughLinesP参数分析
- Velocity + log4j + Spring MVC + Spirng + MyBatis + Maven整合
- Delphi DLL的创建、静态 以及动态调用
- php-面向对象(2)
- 1099. Build A Binary Search Tree (30)
- leetcode-Maximum Subarray
- js中window和document对象及如何操作iframe
- php-面向对象(1)
- JavaScript整理(2)--DOM
- Android进阶学习之路——View事件的传递机制
- 3*3方阵旋转算法
- 第三课,T语言数据类型(版本TC5.0)
- 局域网内部建设的web serverice服务器被外网访问调试过程。