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

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;
}
相关推荐
weixin_4772716942 分钟前
根象:树根。基石。基于马王堆帛书《周易》原文及甲骨文还原周朝生活活动现象(《函谷门》原创)
算法·图搜索算法
普通网友1 小时前
多协议网络库设计
开发语言·c++·算法
努力努力再努力wz1 小时前
【Linux网络系列】:TCP 的秩序与策略:揭秘传输层如何从不可靠的网络中构建绝对可靠的通信信道
java·linux·开发语言·数据结构·c++·python·算法
汉克老师1 小时前
GESP2024年3月认证C++二级( 第二部分判断题(1-10))
c++·循环结构·分支结构·gesp二级·gesp2级
daxi1501 小时前
C语言从入门到进阶——第9讲:函数递归
c语言·开发语言·c++·算法·蓝桥杯
持续学习的程序员+12 小时前
强化学习Q-chunking算法
算法
Polaris北2 小时前
第二十七天打卡
开发语言·c++·算法
风吹乱了我的头发~3 小时前
Day30:2026年2月20日打卡
算法
blackicexs3 小时前
第五周第五天
算法
不吃橘子的橘猫3 小时前
《集成电路设计》复习资料2(设计基础与方法)
学习·算法·fpga开发·集成电路·仿真·半导体