5A - Matrix
#include <iostream>
using namespace std; int n, m, q;
struct node {
int v; // 节点权值
int r; // 右侧节点在arr[]中的位置
int d; // 下侧节点在arr[]中的位置
node() {v = r = d = -;} // 初始化
} arr[ * ]; // 利用矩阵行列位置确定在arr[]中的位置
int locate(int x, int y)
{
return x * (m + ) + y;
} int main()
{
ios::sync_with_stdio(false);
cin.tie(); cin >> n >> m >> q; // 输入权值
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
cin >> arr[locate(i, j)].v; // 再次遍历确定节点的右侧和下侧 注意是从0开始!
for (int i = ; i <= n; i++) {
for (int j = ; j <= m; j++) {
arr[locate(i, j)].r = locate(i, j + );
arr[locate(i, j)].d = locate(i + , j);
}
} // 询问
int x1, y1, x2, y2, h, w;
while (q--) {
cin >> x1 >> y1 >> x2 >> y2 >> h >> w;
// 找到两个子矩阵左上角的左上角的位置
int p1 = , p2 = ;
for (int i = ; i < x1; i++)
p1 = arr[p1].d;
for (int i = ; i < y1; i++)
p1 = arr[p1].r;
for (int i = ; i < x2; i++)
p2 = arr[p2].d;
for (int i = ; i < y2; i++)
p2 = arr[p2].r; // 改变子矩阵边界的d、r
int r1 = p1, r2 = p2;
for (int i = ; i <= h; i++) {
r1 = arr[r1].d;
r2 = arr[r2].d;
swap(arr[r1].r, arr[r2].r);
}
for (int i = ; i <= w; i++) {
r1 = arr[r1].r;
r2 = arr[r2].r;
swap(arr[r1].d, arr[r2].d);
}
r1 = p1;
r2 = p2;
for (int i = ; i <= w; i++) {
r1 = arr[r1].r;
r2 = arr[r2].r;
swap(arr[r1].d, arr[r2].d);
}
for (int i = ; i <= h; i++) {
r1 = arr[r1].d;
r2 = arr[r2].d;
swap(arr[r1].r, arr[r2].r);
}
} // 输出改变后的矩阵
int p = ;
for (int i = ; i <= n; i++) {
bool flag = true;
p = arr[p].d;
int k = p;
for (int j = ; j <= m; j++) {
k = arr[k].r;
if (flag) {
cout << arr[k].v;
flag = false;
}
else cout << ' ' << arr[k].v;
}
cout << endl;
}
return ;
}
5A - Matrix的更多相关文章
- angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation
今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- Atitit Data Matrix dm码的原理与特点
Atitit Data Matrix dm码的原理与特点 Datamatrix原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)于 ...
- Android笔记——Matrix
转自:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate Matrix的数学原理 在Android中,如果你 ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- java中链表的数据(对象)位置交换
用LinkedList类的set方法把引用 对象换了就行 ,如 import java.util.LinkedList; public class Tffdsafsdafsad { public st ...
- elasticsearch2.x插件之一:marvel(配置)
Marvel是Elastic公司推出的商业监控方案,是用来监控Elasticsearch集群,历史状态的有力工具,便于性能优化以及故障诊断.监控主要分为六个层面,分别是集群层.节点层.索引层.分片层. ...
- 内存cache使用的场景
Q.业务场景内为什么要使用内存cache? A.为了利用内存cache的优点, 解决业务场景内的缺陷 Q.内存cache的优点和缺点 A.优点: 内存读写速度比磁盘块 缺点: 内存空间有限, 内存单价 ...
- Android 单例模式探讨
Singleton模式可以是很简单的,它的全部只需要一个类就可以完成(看看这章可怜的UML图).但是如果在“对象创建的次数以及何时被创建”这两点上较真起来,Singleton模式可以相当的复杂,比头五 ...
- js精度(摘)
Math.formatFloat = function (f, digit) { var m = Math.pow(10, digit); return parseInt(f * m, 1 ...
- File类中的list()和listFiles()方法
list()方法是返回某个目录下的所有文件和目录的文件名,返回的是String数组 listFiles()方法是返回某个目录下所有文件和目录的绝对路径,返回的是File数组 public class ...
- JavaScript 组件编写
说明 这是一个联系人名过滤组件,还提供可选的"大小写是否敏感"选项,默认大小写不敏感. 一.HTML 结构 <ul class="contacts"> ...
- 第18章-使用WebSocket和STOMP实现消息功能
Spring 4.0为WebSocket通信提供了支持,包括: 发送和接收消息的低层级API: 发送和接收消息的高级API: 用来发送消息的模板: 支持SockJS,用来解决浏览器端.服务器以及代理不 ...
- Java 正则表达式的实际应用
正则表达式最详细-----> | |目录 1匹配验证-验证Email是否正确 2在字符串中查询字符或者字符串 3常用正则表达式 4正则表达式语法 1匹配验证-验证Email是否正确 public ...
- css3的那些高级选择器一
css大家都不陌生了,从1996年12月css1正式推出,经历了1998年5月css2,再到2004年2月css2.1,最后一直到2010年推出的css3.css的推出给web带来巨大 的改变,使我们 ...