hdu3484(Matrix Game 枚举暴力)

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 2 1 1 1 0 0 0 0 1 2 2 1 1 1 0 0 0 0 0 -1 -1
 

Sample Output
  
  
Yes No
 
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int ma[105][105],tar[105][105],tmp[105][105];
int n,m;
void init()
{
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
            scanf("%d",&ma[i][j]);
    }
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
            scanf("%d",&tar[i][j]);
    }
}
void cpy(int c)
{
    for (int i=1;i<=m;i++)
    {
        for (int j=1;j<=n;j++)
        {
            tmp[j][i]=ma[j][i];
        }
    }
}
void change_col(int a,int b)
{
    for (int i=1;i<=n;i++)
    {
        int temp=tmp[i][a];
        tmp[i][a]=tmp[i][b];
        tmp[i][b]=temp;
    }
}
void change_row(int c)
{
    for (int i=1;i<=m;i++)
        tmp[c][i]^=1;
}
bool ok(int a,int b)
{
    for (int i=1;i<=n;i++)
    {
        if (tar[i][a]!=tmp[i][b])
            return false;
    }
    return true;
}
int main()
{
    while (scanf("%d%d",&n,&m))
    {
        if (n==-1) break;
        init();
        bool ans=0;
        for (int i=1;i<=m;i++)
        {
            cpy(i);
            change_col(1,i);
            for (int j=1;j<=n;j++)
            {
                if (tmp[j][1]!=tar[j][1])
                    change_row(j);
            }
            for (int j=2;j<=m;j++)
            {
                for (int k=j;k<=m;k++)
                {
                    ans=ok(j,k);
                    if (ans){
                        change_col(j,k);
                         break;
                    }
                }
            }
            if (ans) break;
        }
        if (ans) puts("Yes");
        else
            puts("No");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值