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 (1≤n≤1000,1≤m≤1000)

.Then we perform q(1≤q≤100,000)

operations:

1 x y: Swap row x and row y (1≤x,y≤n)

;

2 x y: Swap column x and column y (1≤x,y≤m)

;

3 x y: Add y to all elements in row x (1≤x≤n,1≤y≤10,000)

;

4 x y: Add y to all elements in column x (1≤x≤m,1≤y≤10,000)

;

 
Input
There are multiple test cases. The first line of input contains an integer T(1≤T≤20)

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.(1≤Mi,j≤10,000)

for all (1≤i≤n,1≤j≤m)

.
The following q

lines contains three integers a(1≤a≤4)

, 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)?'\n':' ');
     }
    return 0;
}
 
 

hdu 5671 矩阵变换的更多相关文章

  1. HDU 5671 Matrix 水题

    Matrix 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5671 Description There is a matrix M that has ...

  2. HDU 5671 Matrix

    Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  3. hdu 5671 Matrix 标记。。。有点晕

    Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem ...

  4. HDU 5671 矩阵

    Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  5. HDU 2819 隐式二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=2819 这道题乍一看是矩阵变换题,估计用矩阵之类的也可以做 但是分析一下就可以知道 要凑成对角线都是1,题目允许行 ...

  6. ZOJ 3690 &amp; HDU 3658 (矩阵高速幂+公式递推)

    ZOJ 3690 题意: 有n个人和m个数和一个k,如今每一个人能够选择一个数.假设相邻的两个人选择同样的数.那么这个数要大于k 求选择方案数. 思路: 打表推了非常久的公式都没推出来什么可行解,好不 ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. django 自定用户系统 以及 Django Model 定义语法

    http://www.tuicool.com/articles/jMzIr2 django使用自己的用户系统 http://www.jianshu.com/p/c10be59aad7a Django ...

  2. C++ 知识点 2

    基本类型常量 const int a; int const a; const int *a; int * const a; int const * a const; 之间的区别? const int ...

  3. 网站UV,与IP、PV

    什么是网站UV,与IP.PV在概念上的区别? UV(独立访客):即Unique Visitor,访问您网站的一台电脑客户端为一个访客.00:00-24:00内相同的客户端只被计算一次. PV(访问量) ...

  4. Scala学习笔记--文件IO

    补充: http://blog.csdn.net/lyrebing/article/details/20369445 http://developer.51cto.com/art/200907/134 ...

  5. BIOS讲解

    首先  BIOS其实没什么神奇的 就是  Bisic input/output System,所以基本输入输出系统是一块装入了启动和自检程序的EPROM或EEPROM集成块,实际上它是被固化在计算机R ...

  6. Caffe : Layer Catalogue(2)

    TanH / Hyperbolic Tangent 类型(type):TanH CPU 实现: ./src/caffe/layers/tanh_layer.cpp CUDA.GPU实现: ./src/ ...

  7. jquery easyui二次开发总结(二)

    1.easyui tab增加“关闭所有页”.“关闭非当前页”功能. //tab增加“关闭所有页”和“关闭非当前页”的功能 $("#tabs").tabs({ onAdd:funct ...

  8. mysql安装图解 mysql图文安装教程(详细说明)-[转]

    很多朋友刚开始接触mysql数据库服务器,下面是网友整理的一篇mysql的安装教程,步骤明细也有详细的说明. MySQL5.0版本的安装图解教程是给新手学习的,当前mysql5.0.96是最新的稳定版 ...

  9. OpenUrl 的跨平台实现

    OpenUrl 是 iOS 中 UIApplication 提供的一个函数,用于调用其它程序.实际上各个平台都有自己的实现,这里提供一个直接封装完的跨平台版本给大家.           Delphi ...

  10. Some General concepts in ISO C

    [ISO C11 Clause 3]对象(object):执行环境中数据存储的一块区域,它的内容可以用来表示值.-注释:对象可以具有特定的类型.--值(value):确定类型的对象的内容的确切含义.- ...