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循环下面的操作这样一来问题迎刃而解


目录
相关文章
|
5月前
|
C++
Open3D Transformation 变形
Open3D Transformation 变形
|
6月前
|
计算机视觉
LabVIEW在 XY Graph中选择一组点
LabVIEW在 XY Graph中选择一组点
37 2
|
机器学习/深度学习 自然语言处理 数据可视化
简单的知识图谱可视化+绘制nx.Graph()时报错TypeError: ‘_AxesStack‘ object is not callable
简单的知识图谱可视化+绘制nx.Graph()时报错TypeError: ‘_AxesStack‘ object is not callable
191 0
|
机器学习/深度学习 算法
随机装箱特征模型(Random Binning Feature Model)代码
在上述代码中,我们首先定义了一个名为RandomBinningFeatureModel的类,并在其__init__方法中接受一个参数num_bins,表示装箱的数量。然后,我们定义了一个名为fit_transform的方法,该方法接受原始输入数据x作为参数,并返回转换后的特征张量xb。
156 0
|
数据可视化 Python
Numpy:利用Numpy库建立可视化输入的二次函数数据点集np.linspace+np.random.shuffle+np.random.normal
Numpy:利用Numpy库建立可视化输入的二次函数数据点集np.linspace+np.random.shuffle+np.random.normal
Numpy:利用Numpy库建立可视化输入的二次函数数据点集np.linspace+np.random.shuffle+np.random.normal
|
Python
使用Numpy将数据集中的data和target同时shuffle
假设现在有图像数据imgs和对应标签targets。数据维度分别如下 imgs.shape = (num, channel, width, height) targets.shape = (num, class) 因为通常我们需要将数据打散,这样的好处是可以让模型训练更具鲁棒性,那么如何同时打散da...
1384 0
|
机器学习/深度学习 资源调度 算法
|
算法
关于CS231N-Assignment1-KNN中no-loop矩阵乘法代码的讲解
在使用无循环的算法进行计算距离的效率是很高的 可以看到No loop算法使用的时间远远小于之前两种算法 Two loop version took 56.
1288 0