• 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.

    样例输入

    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:
    ");
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    printf("%d ",num[i][j]);
                }
                printf("
    ");
            }**/
            if(n%2==0)
            {
                if(num[n][1]==0)
                    printf("0
    ");
                else
                    printf("1
    ");
            }
            else if(n%2==1)
            {
                if(num[n][m]==0)
                    printf("0
    ");
                else
                    printf("1
    ");
            }
         }
    	end;
    }
    
    

    下面开始罗嗦大佬直接略过
    有一点比较的就是在这里:
    需要进行判断(73 行和 93 行)从左向右还是从右向左
    在这里插入图片描述
    下面附上丑事:
    掉进自己的思维漏洞:
    ///刚开始的程序都是输出0在输出模拟完成之后的矩阵之后也傻了眼,都是可以完成上面的操作的[手动狗头],然后才发现当开始没有加上变量flag的时候,只跳出了第一个for循环并且下面的语句也会执行,这样就导致所欲的元素都可以变换为零,bug比较浅,还好当时心态没崩 ,加上flag变量之后仅当flag==1的时候进行下面第二层for循环下面的操作这样一来问题迎刃而解

  • 相关阅读:
    LA3971组装电脑
    LA3971组装电脑
    LA3905流星
    LA3905流星
    LA3902网络
    LA3902网络
    LA3708墓地雕塑
    洛谷 P2330 [SCOI2005]繁忙的都市(最小生成树)
    最小生成树 & 洛谷P3366【模板】最小生成树 & 洛谷P2820 局域网
    洛谷 P1372 又是毕业季I
  • 原文地址:https://www.cnblogs.com/PushyTao/p/13144219.html
Copyright © 2020-2023  润新知