Matrix Transformation(差分)

问题 C: Matrix Transformation

问题 C: Matrix Transformation

  • 题目描述

  • You have an integer matrix A, with R rows and C columns. That means it has R rows with each row containing C integers. Two integers are adjacent if their container cells share an edge. For example, in the following grid
    在这里插入图片描述

    (0, 1), (4, 5), (1, 4), (5, 2) are adjacent but (0, 4), (2, 6), (5, 7) are not adjacent.

  • You are allowed to do only one kind of operation in the matrix. In each step you will select two adjacent cells and increase or decrease those two adjacent values by 1, i.e., both values are increased by 1 or both values are decreased by 1.
    Given a matrix, determine whether it is possible to transform it to a zero matrix by applying the allowed operations. A zero matrix is the one where each of its entries is zero.

  • 输入
    The first input line contains a positive integer, n, indicating the number of matrices. Each matrix starts with a line containing R (2 ≤ R ≤ 30) and C (2 ≤ C ≤ 30) separated by a single space. Each of the next R lines contains C integers. Each of these integers is between -20 and +20 inclusive.
    Assume that each input matrix will have at least one non-zero value.

  • 输出
    For each matrix (test case), output a single integer on a line by itself indicating whether or not it can be transformed to a zero matrix. Output the integer 0 (zero) if the matrix can be transformed to a zero matrix and 1 (one) if it cannot.

  • 样例输入 Copy
    6
    3 3
    -2 2 2
    1 1 0
    2 -2 -2
    3 3
    -1 0 1
    -2 -1 1
    0 1 2
    3 3
    -1 0 1
    0 2 -1
    -1 1 2
    3 3
    -1 2 1
    -1 -1 -3
    1 1 -1
    2 3
    0 -2 3
    1 3 1
    2 3
    3 1 1
    2 0 1

  • 样例输出 Copy
    0
    1
    1
    0
    1
    0

  • 题意分析:就是相邻的两个数可以同时加一个数也可以同时减一个数,但前提是相邻
    在这里插入图片描述
    就像这个图一样,0和1相邻 ,1和4也相邻。而0和4就不相邻
    最终结果其全部为零。

  • 思路分析:就是说从左上我那个右下让他为零
    | 1 |2 | 3 |
    | 4 | 5 | 6 |
    | 9 | 8 | 7 |
    就是说要他们全部为零的话,想让 | 1 | 为零 可以把它与| 2 |或者| 4 | 来处理 ,最后是| 1 为零,到| 2 |时 我们已经使| 1 | 为零了 所以只能 让他与| 3 |或者| 5 |来在一起处理。

  • 所以我们可以进行向右或者向下的操作, 这样也很麻烦,不好考虑;

  • 这样的话我们可以进行压缩,把二维压到一维。就是让向右向下的路 都只能向下,把这些数都处理到,最后一行,再向右处理,判断最后一个数是否是零。

  • 简单的说一下这个==“处理”==就是假两个数相邻,1,6 把第一个处理成0 需要同时减一也就是 0,5;而当时-1,6时 处理成零就是同时加一 成 0,7。其实就是减去前一个数,(而不是加哦!)

  • 说的有点复杂,不懂的话结合代码看一下能不能帮助你理解吧!

  • 代码帮助理解

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <algorithm>
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=1e6+5;
const double pi=3.1415926;
const int INF=1e9;
const int dx[5]= {1,5,10,25};

ll n,m,r,l,p,t,sum,res,ans,cnt;
ll temp;
ll a[1000][1000],primee[maxn],b[maxn];
char str[100][100],s[maxn];
ll dp[1000][1000];
double ss[1000];

int main() {
	cin>>t;
	while(t--) {
		cin>>n>>m;
		sum=0;
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=m; j++) {
				scanf("%lld",&dp[i][j]);
				dp[i][j]-=dp[i-1][j];
				///	cout<<dp[i][j]<<" ";
			}
		}
		sum=dp[n][1];
		for(int i=2; i<=m; i++)
		{
			if(sum>0)
			{
				sum=dp[n][i]-sum;
			}
			else sum=dp[n][i]-sum;
		}
		if(sum==0) cout<<0<<endl;
		else cout<<1<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值