#include
#include
#include
#define Max 100
#define True 1
#define False 0
#define notFound -1
#define Input "input.txt"
#define Output "output.txt"
/*--------------------------数据定义-------------------------*/
int b1[Max][Max]={0},b0[Max][Max]={0},b[Max][Max]={0};
int count=0;
int n=0,m=0,num=0;
int *w=NULL;
void trans1(int x);//行变形
void trans2(int p,int q); //列变换
void acopy(int a[Max][Max],int b[Max][Max]);
int same(int x,int y);
/*--------------------------算法实现-------------------------*/
int same(int x,int y){ //比较两列是否完全一致
int i;
for(i=0;i if(b0[i][x]!=b1[i][y]) return False;
}
return True;
}
void acopy(int a[Max][Max],int b[Max][Max]){ //将数组b中的数据copy到a中去
int i,j;
for(i=0;i for(j=0;j a[i][j]=b[i][j];
}
}
}
void trans1(int x){ //将x这一行整体翻过来
int i;
for(i=0;i b1[x][i]=b1[x][i]^1;
}
count++;
}
void swap(int *a,int *b){ //数据交换
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void trans2(int p,int q){ //将p、q两列交换
int i;
if(p==q) return ;
for(i=0;i swap(&b1[i][p],&b1[i][q]);
}
count++;
}
int bestTrans(){
int best;
int j,p,q;
int found;
best=n+m+1;
count=0;
acopy(b,b1);
for(j=0;j acopy(b1,b); //将数组b中的数据copy到b1中去
count=0;
trans2(0,j); //进行列交换
for(p=0;p if(b0[p][0]!=b1[p][0]) trans1(p); //进行行变形
}
for(p=0;p found=False;
for(q=p;q if(same(p,q)){
trans2(p,q); //进行列交换
found=True;
break;
}
}
if(!found)break;
}
if(found&&count }
if(best else return notFound;
}
/*------------------------读写文件操作------------------------*/
void readFile(){
FILE * fp;
int i,j;
int k;
fp=fopen(Input,"r");
if(fp==NULL){
printf("读文件出错!\n");
exit(-1);
}
fscanf(fp,"%d\n",&num);
if(num<=0){
printf("文件数据不合法,程序无法继续执行。\n");
exit(-1);
}
w=(int *)malloc(num*sizeof(int));
k=0;
while(!feof(fp)){
fscanf(fp,"%d %d\n",&m,&n);
printf("%d %d\n",m,n);
for(i=0;i for(j=0;j fscanf(fp,"%d ",&b0[i][j]);
printf("%d ",b0[i][j]);
}
fscanf(fp,"\n");
printf("\n");
}
for(i=0;i for(j=0;j fscanf(fp,"%d ",&b1[i][j]);
printf("%d ",b1[i][j]);
}
fscanf(fp,"\n");
printf("\n");
}
w[k]=bestTrans();
k++;
}
fclose(fp);
}
void writeFile(){
FILE * fp;
int i;
fp=fopen(Output,"w");
if(fp==NULL){
printf("写入文件出错\n");
exit(-1);
}
for(i=0;i fprintf(fp,"%d\n",w[i]);
}
free(w);
fclose(fp);
}
/*------------------------主函数------------------------*/
int main(){
readFile();
writeFile();
return 0;
}
阅读(1101) | 评论(0) | 转发(0) |