您的位置:首页 > 其它

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: