您的位置:首页 > 其它

zoj 3353 Chess Board(高斯消元)

2014-08-24 22:10 246 查看
Chess Board
Time Limit: 5 Seconds      Memory Limit: 65536 KB

There's an N X M board which filled with black and white chesses.
In each move, the player can flip one of these N X M chesses, meanwhile some chesses of its 8 neighbors will switch into its opposite color(from black to white and from white
to black).
Here's four change pattern of the flip operation:
1.   .*.
* *   ('*' denotes the position which will change its color, '.' denotes that the color will stay the same.)
.*.   It means the chesses of the choosen position's up, down, left and right will switch its color.
(Don't forgot the chess which the player choose, it'll also switch its color.)

2.   **.
* *
*..   It means the chesses of its upper-left, up, left, right and bottom-left will switch its color.

3.   .**
* *
.*.   It means the chesses of its up, upper-right, left, right and down will switch its color.

4.   .**
* .
.**   It means the chesses of its up, upper-right, left, down and bottom-right will switch its color.

At the beginning, the player should choose one of these four patterns to flip (the player can only use one pattern in a single game), then try to switch the board into
all white. By the way, we want to find a solution with minimal number of operations, if ties, select the smaller index of pattern.

Input

There are multiple test cases (no more than 150).
The first line of each case contains two integer numbers N and M (1 <= N, M <= 15), indicating the width and the height of the board.
The following N lines containing M characters each describe the initial state of the board. '0' and '1' correspond to white and black, respectively. 

Input ends with 0 0.

Output

For each test case output the optimal solution, the pattern should be choosen follows by the minimal number of operations.
If none of these four pattern can switch the board into all white, output "Impossible" for the test case.

Sample Input

1 1
1
3 2
11
10
11
5 5
00000
00110
01110
00100
00000
0 0

Sample Output

1 1
4 1
3 1

Author: OUYANG, Jialin
Source: ZOJ Monthly, July 2010

高斯消元,直接上模版就行了。

注意初始化,我开始的时候方向都弄反了,怎么也不出结果。

代码有点搓,凑合看吧。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int a[230][230],x[230];//系数矩阵,解数组
int equ,var;//方程数,变元数

void Debug()
{
for(int i=0;i<equ;i++)
{
for(int j=0;j<=var;j++)
printf("%d ",a[i][j]);
puts("");
}
puts("");
}

int gcd(int a,int b) //最大公约数
{
if(b==0) return a;
if(a<0) return gcd(-a,b);
if(b<0) return gcd(a,-b);
return gcd(b,a%b);
}

int lcm(int a,int b) //最小公倍数
{
return a/gcd(a,b)*b;
}

int Gauss()
{
int k,col,max_r;//k为当前处理的方程的下标,col为x的下标,max_r为最大元素行
//求出max_r,并与当前行交换是为了求解时减小误差

for(k=0,col=0;k<equ&&col<var;k++,col++) //处理所有的方程
{
max_r=k;
for(int i=k+1;i<equ;i++) //求出当前列元素最大的那一行
if(a[i][col]>a[max_r][col]) max_r=i;

if(max_r!=k) //交换两行的元素
for(int i=0;i<=var;i++)
{
swap(a[max_r][i],a[k][i]);
}

if(a[k][col]==0) //若为0,则这列所有元素都为0,无需处理
{
k--;
continue;
}

for(int i=k+1;i<equ;i++) //不为0时,将所有行当前列的元素置0,并处理所有其他元素(这里是二进制)
{
if(a[i][col])
for(int j=col;j<=var;j++)
{
a[i][j]=a[i][j]^a[k][j];
}
}

/* //通用性处理各行元素
for(int i=k+1;i<equ;i++)
{
if(a[i][col])
{
int LCM=lcm(a[k][col],a[i][col]);
int ta=LCM/a[i][col],tb=LCM/a[k][col];

if(ta*tb<0) tb=-tb;
for(int j=col;j<=var;j++)
{
a[i][j]=(a[i][j]*ta%2-a[k][j]*tb%2+2)%2;
}
}
}
*/
}

//Debug();

for(int i=k;i<equ;i++) //无解的情况
if(a[i][var]) return -1;

for(int i=0;i<equ;i++) //处理系数矩阵对角线元素均不为0
if(!a[i][i])
{
int j;
for(j=i+1;j<var;j++)
if(a[i][j]) break;

if(j>=var) break;

for(int r=0;r<equ;r++)
swap(a[r][i],a[r][j]);
}

if(k<var) //多解情况,下面分两种情况讨论(二进制枚举,全部设置为0)
{
//二进制枚举
int lim=(1<<(var-k)),ans=10000;
for(int i=0;i<lim;i++)
{
int temp=i,sum=0;
for(int j=var-1;j>=k;j--)
{
x[j]=temp%2;temp/=2;
}
for(int j=k-1;j>=0;j--)
{
temp=a[j][var]%2;
for(int r=j+1;r<var;r++)
temp=(temp-a[j][r]*x[r]%2+2)%2;

x[j]=(temp/a[j][j])%2;
}
for(int j=0;j<var;j++)
if(x[j]!=0)
{
sum++;
}
ans=min(ans,sum);
}
return ans;

/* 全部设置为0
for(int j=var-1;j>=var-k;j--)
{
x[j]=0;
}
for(int j=var-k-1;j>=0;j--)
{
temp=a[j][var];
for(int r=j+1;r<var;r++)
temp-=a[j][r]*x[r];

x[j]=temp/a[j][j];
}
return var-k;
*/
}

int ans=0; //唯一解的情况
for(int j=var-1;j>=0;j--)
{
int temp=a[j][var]%2;
for(int r=j+1;r<var;r++)
temp=(temp-a[j][r]*x[r]%2+2)%2;

x[j]=(temp/a[j][j])%2;
}
for(int j=0;j<var;j++)
if(x[j]!=0)
{
ans++;
}
return ans;
}

int m,n;

int judge(int x,int y)
{
if(x>=0&&x<m&&y>=0&&y<n) return 1;
return 0;
}

void init1() //系数矩阵初始化
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));

for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(judge(i-1,j)) a[i*n+j][(i-1)*n+j]=1;
if(judge(i+1,j)) a[i*n+j][(i+1)*n+j]=1;
if(judge(i,j-1)) a[i*n+j][i*n+j-1]=1;
if(judge(i,j+1)) a[i*n+j][i*n+j+1]=1;
a[i*n+j][i*n+j]=1;
}
}

void init2() //系数矩阵初始化
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));

for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(judge(i+1,j)) a[i*n+j][(i+1)*n+j]=1;
if(judge(i+1,j+1)) a[i*n+j][(i+1)*n+j+1]=1;
if(judge(i-1,j+1)) a[i*n+j][(i-1)*n+j+1]=1;
if(judge(i,j+1)) a[i*n+j][i*n+j+1]=1;
if(judge(i,j-1)) a[i*n+j][i*n+j-1]=1;
a[i*n+j][i*n+j]=1;
}
}

void init3() //系数矩阵初始化
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));

for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(judge(i+1,j)) a[i*n+j][(i+1)*n+j]=1;
if(judge(i+1,j-1)) a[i*n+j][(i+1)*n+j-1]=1;
if(judge(i-1,j)) a[i*n+j][(i-1)*n+j]=1;
if(judge(i,j+1)) a[i*n+j][i*n+j+1]=1;
if(judge(i,j-1)) a[i*n+j][i*n+j-1]=1;
a[i*n+j][i*n+j]=1;
}
}

void init4() //系数矩阵初始化
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));

for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(judge(i+1,j)) a[i*n+j][(i+1)*n+j]=1;
if(judge(i-1,j)) a[i*n+j][(i-1)*n+j]=1;
if(judge(i,j+1)) a[i*n+j][i*n+j+1]=1;
if(judge(i+1,j-1)) a[i*n+j][(i+1)*n+j-1]=1;
if(judge(i-1,j-1)) a[i*n+j][(i-1)*n+j-1]=1;
a[i*n+j][i*n+j]=1;
}
}
int main()
{
char s[230],t[20];
memset(s,0,sizeof(s));
memset(t,0,sizeof(t));
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==0&&n==0) break;
equ=var=m*n;
for(int i=0;i<m;i++)
{
scanf("%s",t);
if(i==0) strcpy(s,t);
else strcat(s,t);
}
//printf("%s\n",s);
init1();
for(int i=0;i<equ;i++)
{
a[i][var]=(s[i]=='1')?1:0;
}
//Debug();
int ans1=Gauss();

init2();
for(int i=0;i<equ;i++)
{
a[i][var]=(s[i]=='1')?1:0;
}
//Debug();
int ans2=Gauss();

init3();
for(int i=0;i<equ;i++)
{
a[i][var]=(s[i]=='1')?1:0;
}
//Debug();
int ans3=Gauss();

init4();
for(int i=0;i<equ;i++)
{
a[i][var]=(s[i]=='1')?1:0;
}
//Debug();
int ans4=Gauss();

int ans=100000,p=-1;
if(ans>ans1&&ans1!=-1)
{
ans=ans1;p=1;
}
if(ans>ans2&&ans2!=-1)
{
ans=ans2;p=2;
}
if(ans>ans3&&ans3!=-1)
{
ans=ans3;p=3;
}
if(ans>ans4&&ans4!=-1)
{
ans=ans4;p=4;
}
//printf("%d %d %d %d\n",ans1,ans2,ans3,ans4);
if(p==-1)
printf("Impossible\n");
else
printf("%d %d\n",p,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: