模拟桌面自动整理, 先顶左,再顶上。

5 6

**##**

****#*

***##*

#*****

***#**

##****

##****

#*****

#*****

#*****

cpp 复制代码
#include<iostream> 
using namespace std;
#define MAX  1024

char a[MAX][MAX];
void H(char arr[][MAX], int n,int idx) {//n列数	,idx 某行
	int left = 0;
	int right = n - 1;
	char t;
	while (left < right) {
		int left = 0;
		int right = n - 1;

		while (arr[idx][left] == '#')
			left++;
		while (arr[idx][right] == '*')
			right--;
		if (left >= right)
			break;

		t = arr[idx][left];
		arr[idx][left] = arr[idx][right];
		arr[idx][right] = t; 
	}
}


void L(char arr[][MAX], int n, int idx) {//n行数	,idx 某列
	int top = 0;
	int down = n - 1;
	char t;
	while (top < down) {
		int top = 0;
		int right = n - 1;

		while (arr[top][idx] == '#')
			top++;
		while (arr[down][idx] == '*')
			down--;
		if (top >= down)
			break;

		t = arr[top][idx];
		arr[top][idx] = arr[down][idx];
		arr[down][idx] = t;
	}
}


 
void print(int h,int l) {
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < l; j++)
			cout << a[i][j];
		cout << endl;
	}
}

int main() {
	int h, l;
	cin >> h>>l;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < l; j++)
			cin >> a[i][j];
		getchar();
	}
	//print(h,l);

	for (int i = 0; i < h; i++) 
		H(a,l,i);
	

	for (int i = 0; i < h; i++) 	
		L(a,h,i);
	print(h, l);

	

	return 0;
}
相关推荐
DoraBigHead1 小时前
小哆啦解题记——异位词界的社交网络
算法
序属秋秋秋1 小时前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习
木头左2 小时前
逻辑回归的Python实现与优化
python·算法·逻辑回归
lifallen6 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree
web_Hsir8 小时前
vue3.2 前端动态分页算法
前端·算法
十秒耿直拆包选手8 小时前
Qt:主窗体(QMainwindow)初始化注意事项
c++·qt
霖0010 小时前
C++学习笔记三
运维·开发语言·c++·笔记·学习·fpga开发
mit6.82410 小时前
[shad-PS4] Vulkan渲染器 | 着色器_重新编译器 | SPIR-V 格式
c++·游戏引擎·ps4
地平线开发者10 小时前
征程 6M 部署 Omnidet 感知模型
算法·自动驾驶
秋说11 小时前
【PTA数据结构 | C语言版】线性表循环右移
c语言·数据结构·算法