栈,队列,链表,邻接表(未完成)

在这里插入图片描述
在这里插入图片描述

“ “ 后进先出 ” ” 的线性数据结构,能进出元素的一端是栈顶,另一端是栈底
在这里插入图片描述

基础
入栈 inline void push(int a){ stack[++top]=a; }
出栈 inline int pop() { return stack[top--]; }
栈空条件 inline bool empty(){ return top<1; }

在这里插入图片描述

表达式计算
中缀表达式

最常见, “ “ ( 1 − 2 ) ∗ 3 (1-2)*3 (12)3 " " "

前缀表达式

波兰式, “ ∗ 3 − 1 “*3-1 31 2 2 2 ” ”

后缀表达式

逆波兰式, “ “ 1 1 1 2 − 3 ∗ 2-3* 23 ” ”

后缀表达式求值:

(1)遇到数,进栈
(2)遇到运算符,取出栈顶的两个数进行计算,把结果入栈
(3)栈中剩下一个数即为后缀表达式的值

中缀转后缀

(1)如果遇到一个数,输出该数
(2)如果遇左括号,把左括号入栈
(3)如果遇右括号,不断取出栈顶并输出,直到栈顶为左括号,然后把左括号出栈
(4)如果遇到运算符,只要栈顶符号优先级不低于新符号,就不断取出栈顶并输出,最后把新符号进栈,优先级乘除 > > >加减 > > >左括号

中缀表达式的递归法求值

(1)在 L L L~ R R R中考虑没有被任何括号包含的运算符
若存在加减号,选其最后一个,分成左右两半递归,结果相加减,返回
若存在乘除号,选其中最后一个,分成左右两半递归,结果相乘除,返回
(2)若不存在没有被任何括号包含的运算符
若首尾字符是括号,递归求解 S [ L + 1 S[L+1 S[L+1~ R − 1 ] R-1] R1],把结果返回
否则,说明区间 S [ L S[L S[L~ R ] R] R]是一个数,直接返回数值
在这里插入图片描述

单调栈

单调栈就是栈里元素是单调的
借助单调性处理问题的思想在于及时排除不可能的选项,保持策略集合的高度有效性和秩序性
在这里插入图片描述

队列

“ “ 先进先出 " " "的线性数据结构
在这里插入图片描述

基础
入队
出队
队空条件
队列元素个数

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

链表

在这里插入图片描述
在这里插入图片描述

参考代码1·下标模拟指针
struct Node{
	int value;//结点的值 
	Node *prev, *next;//结点前后结点的地址 
};
Node *head, *tail;
//创建两个地址 
/*
一个结构体代表一个结点 
每一个结构体有对应的编号
但是这个编号不是我们自己设定的 
结构体中的next,prev,需要找到当前结点的前后结点
不可能是在结构体中放结构体,这样就是包含关系了
它仅仅只是需要记录 代表它前后结点的结构体 的编号
所以用指针,这个指针就是 代表它前后结点的结构体 的编号
*/ 

void initalize() {
	head = new Node();//新建一个头端点 
	tail = new Node();//新建一个尾端点 
	head->next = tail; 
	tail->prev = head;//将两个点互相关联 
}

void insert(Node *p, int val){
	//设把q插在q与y之间
	//p->next为y 
	q = new Node();//新建一个点q 
	q->value = val;//赋值 
	p->next->prev = q;//y的前驱赋为q 
	q->next = p->next;//q的后驱设为y 
	p->next = q;//p的后驱为q 
	q->prev = p;//q的前驱为p 
}

void remove(Node *p){
	//设把插在q与y之间的p删掉 
	p->prev->next = p->next;//q的后驱设为y 
	p->next->prev = p->prev;//y的前驱设为q 
	delete p;//删除结点p 
}

void recycle(){
	while(head != tail){
		head = head->next;//不断后移head 
		delete head->prev;//删除原先的head 
	}
	delete tail;//删除最后的点 
}
参考代码1·数组模拟链表

在这里插入图片描述
在这里插入图片描述

A - Working routine

Vasiliy finally got to work, where there is a huge amount of tasks waiting for him. Vasiliy is given a matrix consisting of n rows and m columns and q tasks. Each task is to swap two submatrices of the given matrix.

For each task Vasiliy knows six integers ai, bi, ci, di, hi, wi, where ai is the index of the row where the top-left corner of the first rectangle is located, bi is the index of its column, ci is the index of the row of the top-left corner of the second rectangle, di is the index of its column, hi is the height of the rectangle and wi is its width.

It’s guaranteed that two rectangles in one query do not overlap and do not touch, that is, no cell belongs to both rectangles, and no two cells belonging to different rectangles share a side. However, rectangles are allowed to share an angle.

Vasiliy wants to know how the matrix will look like after all tasks are performed.

input

The first line of the input contains three integers n, m and q (2 ≤ n, m ≤ 1000, 1 ≤ q ≤ 10 000) — the number of rows and columns in matrix, and the number of tasks Vasiliy has to perform.

Then follow n lines containing m integers vi, j (1 ≤ vi, j ≤ 109) each — initial values of the cells of the matrix.

Each of the following q lines contains six integers ai, bi, ci, di, hi, wi (1 ≤ ai, ci, hi ≤ n, 1 ≤ bi, di, wi ≤ m).

output

Print n lines containing m integers each — the resulting matrix.

题目大意

有q个操作在n*m的矩阵上,(a1,a2)是第一个矩阵左上角的坐标,(a3,a4)是第二个矩阵左上角坐标,a4是高,a5是宽,交换两个矩阵,输出最终的矩阵。

题解

对于每一个点记录它下方以及右边的编号,每次矩形交换,对 矩形周边的点 的下右编号进行维护。
保持 ( 0 , 0 ) (0,0) 0,0点不变,每次寻找 ( i , j ) (i,j) i,j点,便从 ( 0 , 0 ) (0,0) 0,0向下 i i i,向右 j j j
注意:
对矩形的四个边进行维护的时候,已修改一条边,对下一条边修改时,不能从 ( 0 , 0 ) (0,0) 0,0点开始,因为矩形的部分点已经更改,却又没有更改完全。
举个例子:
在这里插入图片描述
红色边框是整个大矩阵,蓝色底的是要更改的 3 ∗ 3 3*3 33小矩阵
根据题意
标1的需要更改 下方点 的编号
标2的需要更改 右方点 的编号
每次交换两个更改矩阵的 下右编号 即可

代码
#include<bits/stdc++.h>
using namespace std;

const int M = 1005;
struct psx {int v, r, u;} p[M*M];
//v是数值,r是右边的点的编号,u是下边点的编号
int n, m, q, cnt = -1, num1, num2, a[7];
//a【】存了两个左上角以及高宽
int a1, a2, a3, a4;
//a1,a2,a3,a4是用来标记矩形的点
int id[M][M] = {};

void first() {
	for(int i = 0; i <= n; i++)
		for(int j = 0; j <= m; j++)
			id[i][j] = ++cnt;//对所有点进行编号
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			scanf("%d", &p[ id[i][j] ].v);//存储数值
	for(int i = 0; i <= n; i++)
		for(int j = 0; j <= m; j++){
			p[ id[i][j] ].r = id[i][j+1];
			p[ id[i][j] ].u = id[i+1][j];//与下边和右边的点建立联系
		}
}
void heng(){
	a1 = p[a1].r;
	a2 = p[a2].r;
	a3 = p[a3].r;
	a4 = p[a4].r;
	for(int i = 1; i <= a[6]; i++){
		swap(p[a1].u, p[a2].u);
		swap(p[a3].u, p[a4].u);
		if(i == a[6]) {
			num1 = a1;
			num2 = a2;
		}
		a1 = p[a1].r;
		a2 = p[a2].r;
		a3 = p[a3].r;
		a4 = p[a4].r;
	}
}

void left(){
	num1 = 0; num2 = 0;
	for(int i = 1; i < a[1]; i++)
		num1 = p[num1].u;
	for(int i = 1; i < a[3]; i++)
		num2 = p[num2].u;
	for(int i = 1; i < a[2]; i++) 
		num1 = p[num1].r;
	for(int i = 1; i < a[4]; i++) 
		num2 = p[num2].r;
	//四个for循环。找到矩形左上方点的
	a1 = num1; num1 = p[num1].u;
	a2 = num2; num2 = p[num2].u;
	for(int i = 1; i <= a[5]; i++){
		swap(p[num1].r, p[num2].r);
		if(i == a[5]) {
			a3 = num1;
			a4 = num2;
		}
		num1 = p[num1].u;
		num2 = p[num2].u;
	} 
}

void right(){
	num1 = p[num1].u;
	num2 = p[num2].u;
	for(int i = 1; i <= a[5]; i++){
		swap(p[num1].r, p[num2].r);
		num1 = p[num1].u;
		num2 = p[num2].u;
	} 
}


int main(){
	scanf("%d%d%d", &n, &m, &q);
	first();
	while( q-- ){
		for(int i = 1; i <= 6; i++)
			scanf("%d", &a[i]);	
		left();//修改左边一列
		heng();//修改上下两行
		right();//修改右边一列
	}
	num2 = p[0].r;//找到(0,1)点
	for(int i = 1; i <= n; i++) {
		num1 = p[num2].u;
		num2 = num1;//num2记录已遍历点最后一行的第一个点的编号,每次往下一行
		for(int j = 1; j <= m; j++){
			printf("%d ", p[num1].v);
			num1 = p[num1].r;//往右移
		}
		printf("\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值