您的位置:首页 > 其它

pku 1753 Flip Game(高斯消元+枚举)

2012-09-06 19:41 337 查看
Flip Game

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 20199Accepted: 8752
Description
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each
round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:

Choose any one of the 16 pieces.

Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).


Consider the following position as an example:

bwbw

wwww

bbwb

bwwb

Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

bwbw

bwww

wwwb

wwwb

The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.

Input
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
Output
Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the
goal, then write the word "Impossible" (without quotes).
Sample Input
bwwb
bbwb
bwwb
bwww

Sample Output
4

Source
Northeastern Europe 2000

编程错误点:原先只管最后的值异或而错,还有max_ans未赋初值0

#include<iostream>
#include<cstring>
using namespace std;
const int mm=937;
const int oo=1e9;
int map[mm][mm+1],b[mm][mm+1];
int ans[mm];
int var,equ;
int max_ans;
void debug();
void swap(int&a,int &b)
{
int tmp;tmp=a;a=b;b=tmp;
}
/*
int abs(int x)
{
if(x<0)return -x;return x;
}*/
int gcd(int a,int b)
{
int t;
while(b!=0)
{
t=b;b=a%b;a=t;
}return a;
}
int lcm(int a,int b)
{
return a*b/gcd(a,b);
}
void dfs(int z,int dep,int k,int kans)
{
ans[var-dep]=z;
if(dep==k)
{
for(int i=var-k-1;i>=0;i--)
{
int tmp;
tmp=map[i][var]%2;
for(int j=i+1;j<var;j++)
if(map[i][j])
tmp=(tmp-(map[i][j]*ans[j])%2+2)%2;
ans[i]=(tmp/map[i][i])%2;if(ans[i])++kans;
}
if(kans<max_ans)max_ans=kans;
//cout<<"ans="<<kans<<endl;
}
else
{
dfs(1,dep+1,k,kans+1);dfs(0,dep+1,k,kans);
}
}
int Guss()
{
int k,col;max_ans=0;
for(k=0,col=0;k<equ&&col<var;k++,col++)
{/**行变换消元*/
int max_i=k;
for(int i=k+1;i<equ;i++)
if(map[i][col]>map[max_i][col])max_i=i;
if(max_i!=k)
{//for(int i=k;i<var+1;i++)好像也OK
for(int i=col;i<var+1;i++)//是K不是col因为k--;
swap(map[k][i],map[max_i][i]);
}
/**除去对角线0,列变换*/
for(int i = 0; i <equ; ++i)//每一行主元素化为非零
if(!map[i][i])
{
int j;
for(j = i+1;j<var;++j)
if(map[i][j])
break;
if(j == var)
break;
for(int kk = 0;kk < equ; ++kk)
swap(map[kk][i],map[kk][j]);
}
/**遇到对角线0,k退1*/
if(map[k][col]==0)
{
k--;continue;
}
/**消元*/
for(int i=k+1;i<equ;i++)
if(map[i][col]!=0)
{
int LCM=lcm(map[k][col],map[i][col]);
int ta,tb;
ta=LCM/map[i][col];tb=LCM/map[k][col];
if(map[i][col]*map[k][col]<0)tb=-tb;
for(int j=col;j<var+1;j++)
{
map[i][j]=((map[i][j]*ta)%2-(map[k][j]*tb)%2+2)%2;
}
}
// cout<<"k="<<k<<endl;
}
//debug();
///判断回代
for(int i=k;i<equ;i++)
if(map[i][col])return -1;
if(var==k)
for(int i=k-1;i>=0;i--)
{
int tmp;
tmp=map[i][var]%2;
for(int j=i+1;j<var;j++)
if(map[i][j])
tmp=(tmp-(map[i][j]*ans[j])%2+2)%2;
ans[i]=(tmp/map[i][i])%2;if(ans[i])max_ans++;
}
else
{    max_ans=oo;
dfs(1,1,var-k,1);dfs(0,1,var-k,0);
}
return 0;
}
void debug()
{
for(int i=0;i<var;i++)
{for(int j=0;j<var+1;j++)
cout<<map[i][j];
cout<<"\n";
}
}
int main()
{
int num;
char s;
int k_ans=oo;
memset(map,0,sizeof(map));
memset(ans,0,sizeof(ans));
num=4;
var=equ=num*num;
for(int i=0;i<num;i++)
{
for(int j=0;j<num;j++)
{  map[i*num+j][i*num+j]=1;
if(i!=0)map[i*num+j][(i-1)*num+j]=1;
if(j!=0)map[i*num+j][i*num+j-1]=1;
if(i!=num-1)map[i*num+j][(i+1)*num+j]=1;
if(j!=num-1)map[i*num+j][i*num+j+1]=1;
cin>>s;
if(s=='b')map[i*num+j][var]=0;
else map[i*num+j][var]=1;
//cout<<map[i*num+j][var];
//cout<<"i="<<i<<"j="<<j<<endl;
}
}
for(int i=0;i<var;i++)
{for(int j=0;j<var;j++)
b[i][j]=map[i][j];
b[i][var]=map[i][var]^1;
}
//debug();
/*for(int i=0;i<var;i++)
{for(int j=0;j<var;j++)
cout<<b[i][j];
cout<<b[i][var]<<"\n";
}*/
int flag=Guss();
if(flag!=-1&&max_ans<k_ans)
k_ans=max_ans;
for(int i=0;i<var;i++)
{for(int j=0;j<var;j++)
map[i][j]=b[i][j];
map[i][var]=b[i][var];
}
flag=Guss();
if(flag!=-1&&max_ans<k_ans)
k_ans=max_ans;
if(k_ans==oo)cout<<"Impossible\n";
else
{
cout<<k_ans<<"\n";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: