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 ...
随机推荐
- 性能调试工具——oprofile
oprofile利用cpu硬件层面提供的性能计数器(performance counter),通过计数采样,帮助我们从进程.函数.代码层面找出占用cpu的"罪魁祸首". 常用命令 ...
- spring中aop原理
- js控制图片放大缩小的简易版
js代码: function bb_img_onmousewheel(e, o) { var zoom = parseInt(o.style.zoom, 10) || 100; zoom += eve ...
- Linux yum失败解决
Linux yum失败解决 问题: 在CentOS 5.5中需要使用yum安装程序,出现错误: There was a problem importing one of the Python modu ...
- logistic growth model . 求解方法 (高斯牛顿法)
https://www.stat.tamu.edu/~jnewton/604/chap4.pdf http://www.metla.fi/silvafennica/full/sf33/sf334327 ...
- Win7怎么进入安全模式 三种轻松进入Win7安全模式方法
发布时间:2013-05-27 11:23 作者:电脑百事网原创 来源:www.pc841.com 13783次阅读 win7的安全模式和XP如出一辙,在安全模式里我们可以删除顽固文件.查杀病毒.解除 ...
- 现代C++学习笔记之二入门篇1
现代 C++ 强调: 基于堆栈的范围,而非堆或静态全局范围. 自动类型推理,而非显式类型名称. 智能指针而不是原始指针. std::string 和 std::wstring 类型(请参见 <s ...
- libtool: syntax error near unexpected token `]*
../libtool: line 543: syntax error near unexpected token `]*' ../libtool: line 543: ` *[\[\~\#\ ...
- POJ - 1328 Radar Installation(贪心区间选点+小学平面几何)
Input The input consists of several test cases. The first line of each case contains two integers n ...
- 见过最好的mybatis学习网站
http://blog.csdn.net/techbirds_bao/article/details/9233599/