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.

题目描述


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.

微信图片_20220528105045.png


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.


样例输入


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 


样例输出


0
1
1
0
1
0


题意:给出一个n表示矩阵的个数;

下面是n个矩阵每个矩阵给出行和列以及每个元素。

可以进行操作将相邻的两个元素同时增加 1 或者是减少 1 (当然可以进行若干次这样的操作),问是否可以将矩阵变化为每个元素都为零的矩阵

提议十分简单,按照题意进行模拟就完了。第一行从左向右第二行从右向左以此类推进行倒S形状的模拟。

附上代码:


#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
#define start int wuyt()
#define end return 0
int num[40][40];
start{
    int cnt=read;
    while(cnt--)
    {
        int n=read,m=read;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                scanf("%d",&num[i][j]);
        }
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            if(i%2==1)
            {
                for(int j=1;j<m;j++)
                {
                    int temp=0-num[i][j];
                    num[i][j]+=temp;
                    num[i][j+1]+=temp;
                    if(i==n&&j==m-1)
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag==1)
                {
                    int temp=0-num[i][m];
                    num[i][m]+=temp;
                    num[i+1][m]+=temp;
                }
            }
            else if(i%2==0)
            {
                for(int j=m;j>1;j--)
                {
                    int temp=0-num[i][j];
                    num[i][j]+=temp;
                    num[i][j-1]+=temp;
                    if(i==n&&j==2)
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag==1)
                {
                    int temp=0-num[i][1];
                    num[i][1]+=temp;
                    num[i+1][1]+=temp;
                }
            }
            if(flag==0) break;
        }
        /**printf("test:\n");
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                printf("%d ",num[i][j]);
            }
            printf("\n");
        }**/
        if(n%2==0)
        {
            if(num[n][1]==0)
                printf("0\n");
            else
                printf("1\n");
        }
        else if(n%2==1)
        {
            if(num[n][m]==0)
                printf("0\n");
            else
                printf("1\n");
        }
     }
  end;
}


有一点比较的就是在这里:

需要进行判断(73 行和 93 行)从左向右还是从右向左


微信图片_20220528105317.png


下面附上丑事:

掉进自己的思维漏洞:

///刚开始的程序都是输出0在输出模拟完成之后的矩阵之后也傻了眼,都是可以完成上面的操作的[手动狗头],然后才发现当开始没有加上变量flag的时候,只跳出了第一个for循环并且下面的语句也会执行,这样就导致所欲的元素都可以变换为零,bug比较浅,还好当时心态没崩 ,加上flag变量之后仅当flag==1的时候进行下面第二层for循环下面的操作这样一来问题迎刃而解


目录
相关文章
|
4月前
|
存储 机器学习/深度学习 算法框架/工具
张量(Tensor)、标量(scalar)、向量(vector)、矩阵(matrix)
张量(Tensor)、标量(scalar)、向量(vector)、矩阵(matrix)
73 1
|
6月前
|
C++
Open3D Transformation 变形
Open3D Transformation 变形
|
6月前
|
算法 计算机视觉
【Qt&OpenCV 直方图计算 split/calcHist/normalize】
【Qt&OpenCV 直方图计算 split/calcHist/normalize】
57 0
|
7月前
|
计算机视觉
LabVIEW在 XY Graph中选择一组点
LabVIEW在 XY Graph中选择一组点
42 2
|
机器学习/深度学习 算法
随机装箱特征模型(Random Binning Feature Model)代码
在上述代码中,我们首先定义了一个名为RandomBinningFeatureModel的类,并在其__init__方法中接受一个参数num_bins,表示装箱的数量。然后,我们定义了一个名为fit_transform的方法,该方法接受原始输入数据x作为参数,并返回转换后的特征张量xb。
163 0
|
JSON Java Android开发
Matrix 之 ApkChecker 的使用
Matrix 之 ApkChecker 的使用
270 0
Matrix 之 ApkChecker 的使用
|
算法 运维
基于数据流的异常检测:Robust Random Cut Forest
一、解决的问题 数据是实时产生的,对数据进行批处理所花费的成本太高了,数据产生的价值被低估 在高维数据下,如何能发现异常的维度? If my time-series data with 30 features yields an unusually high anomaly score.
11276 0