zoukankan      html  css  js  c++  java
  • hdu 5671 矩阵变换

    Matrix

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 853    Accepted Submission(s): 360


    Problem Description
    There is a matrix M that has n rows and m columns (1n1000,1m1000) .Then we perform q(1q100,000) operations:

    1 x y: Swap row x and row y (1x,yn) ;

    2 x y: Swap column x and column y (1x,ym) ;

    3 x y: Add y to all elements in row x (1xn,1y10,000) ;

    4 x y: Add y to all elements in column x (1xm,1y10,000) ;
     
    Input
    There are multiple test cases. The first line of input contains an integer T(1T20) indicating the number of test cases. For each test case:

    The first line contains three integers n , m and q .
    The following n lines describe the matrix M.(1Mi,j10,000) for all (1in,1jm) .
    The following q lines contains three integers a(1a4) , x and y .
     
    Output
    For each test case, output the matrix M after all q operations.
     
    Sample Input
    2 3 4 2 1 2 3 4 2 3 4 5 3 4 5 6 1 1 2 3 1 10 2 2 2 1 10 10 1 1 1 2 2 1 2
     
    Sample Output
    12 13 14 15 1 2 3 4 3 4 5 6 1 10 10 1
    题目大意:就是简单的四种矩阵变换,相信学过线代的人都很容易理解。
    思路分析:以前没怎么做过对矩阵进行处理的题目,做这道题目的时候是有些蒙比的,
    但是有一点我是可以肯定的,暴力做(进行一次变换对矩阵处理一次)是必死的,其实
    我们完全可以储存每一次的变换然后最后再将所进行的变化直接一次性输出,这样就大
    大缩减了运算量,具体实现方法是开a,b,c,d四个数组用来储存相对应的四种变化,a[i],
    b[i]储存的是当前i行(列)是最开始的第a[i]行(b[i]列),c[a[i]]则代表当前第i行加减
    的数值。
    代码:
    /*
     首先要进行的矩阵变换次数q非常多,
     如果每进行一次变换就对矩阵整体进行操作,
     势必TLE。
    */
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <map>
    using namespace std;
    const int maxn=1000+10;
    int maps[maxn][maxn];
    int a[maxn],b[maxn],c[maxn],d[maxn];//a,b数组用来储存当前行或者列是原来第几行或者第几列
    //而c,m数组用来储存该行或该列整体加减了多少。
    int main()
    {
         int T;
         scanf("%d",&T);
         while(T--)
         {
             int n,m,q;
             int s,x,y;
             scanf("%d%d%d",&n,&m,&q);
             memset(a,0,sizeof(a));
             memset(b,0,sizeof(b));
             memset(c,0,sizeof(c));
             memset(d,0,sizeof(d));
             for(int i=1;i<=n;i++)
                a[i]=i;
             for(int i=1;i<=m;i++)
                b[i]=i;
             for(int i=1;i<=n;i++)
             for(int j=1;j<=m;j++)
                scanf("%d",&maps[i][j]);
             while(q--)
             {
                 scanf("%d%d%d",&s,&x,&y);
                 if(s==1)
                 swap(a[x],a[y]);
                 if(s==2)
                  swap(b[x],b[y]);
                 if(s==3)
                 c[a[x]]+=y;
                 if(s==4)
                 d[b[x]]+=y;
             }
             for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                printf("%d%c",maps[a[i]][b[j]]+c[a[i]]+d[b[j]],(j==m)?' ':' ');
         }
        return 0;
    }
     
     
  • 相关阅读:
    pipeline+sonar
    ThinkPHP 3.2.3 使用 PHPExcel 处理 Excel 表格
    mac下finder子目录直接打开终端
    golang之交叉编译设置
    cocos2dx 3.0 编译工程
    2dx 3.0环境配置(mac)
    golang调用动态库
    qt下用启动图
    qt在动态库里面加载widget的例子
    qt笔记
  • 原文地址:https://www.cnblogs.com/xuejianye/p/5440896.html
Copyright © 2011-2022 走看看