HDOJ 3484 Matrix Game

来源:互联网 发布:js random 编辑:程序博客网 时间:2024/05/17 23:58


枚举对应的行列。。。暴力hash。。。

Matrix Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 559    Accepted Submission(s): 282


Problem Description
At the start of the matrix game, we have an n * m chessboard in which each grid is painted alternatively in white or black. Every time, we can apply one of the two following operations:
Row flip operation: we can change the color of every grid in a single row.
Column swap operation: we can swap two columns (i.e., switch the colors between corresponding grids).
The task of the problem is, determine whether it’s possible to reach the target from the original chessboard by applying the two operations several times. Print ‘Yes’ or ‘No’ for each case.
 

Input
There are several test cases. For each case, there are two integers n and m in the first line (1 ≤ n, m ≤ 100), followed by two n * m 0/1 matrixes (0 stands for white color and 1 stands for black color) which are the original chessboard and the target chessboard respectively.
The input ends up with two negative numbers, which should not be processed as a case.
 

Output
For each test case, print ‘Yes’ or ‘No’ to tell whether it’s possible to reach the target.
 

Sample Input
2 21 11 00 00 12 21 11 00 00 0-1 -1
 

Sample Output
YesNo
 

Source
2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU
 

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>using namespace std;int n,m;int M1[111][111],M2[111][111];unsigned long long int ha1[111],ha3[111];const unsigned long long int mod=67126311511LL;void gethahashsh(){    memset(ha1,0,sizeof(ha1));    for(int i=0;i<m;i++)    {        for(int j=0;j<n;j++)        {            ha1[i]=(ha1[i]*2+M2[j][i])%mod;        }    }    sort(ha1,ha1+m);}bool ck(int x1,int x2){    int rev[111];    memset(rev,0,sizeof(rev));    for(int i=0;i<n;i++)        if(M1[i][x1]!=M2[i][x2]) rev[i]=1;    memset(ha3,0,sizeof(ha3));    for(int i=0;i<m;i++)    {        for(int j=0;j<n;j++)        {            ha3[i]=(ha3[i]*2+(M1[j][i]+rev[j])%2)%mod;        }    }    sort(ha3,ha3+m);    for(int i=0;i<m;i++) if(ha1[i]!=ha3[i]) return false;    return true;}int main(){while(scanf("%d%d",&n,&m)!=EOF&&~n&&~m){    for(int i=0;i<n;i++)        for(int j=0;j<m;j++)            scanf("%d",&M1[i][j]);    for(int i=0;i<n;i++)        for(int j=0;j<m;j++)            scanf("%d",&M2[i][j]);    gethahashsh();    bool flag=false;    for(int j=0;j<m&&!flag;j++)    {        if(ck(0,j)) flag=true;    }    flag?puts("Yes"):puts("No");}    return 0;}




0 0