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

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;
}
相关推荐
曼巴UE55 小时前
UE 客户端 需要的网络同步概念总结
网络·c++·ue5
『昊纸』℃5 小时前
C语言学习心得集合 篇1
c语言·算法·编程基础·学习心得·实践操作
云深麋鹿5 小时前
C++ | 继承
开发语言·c++
小辉同志5 小时前
Epoll+线程池
开发语言·c++·c·线程池·epoll
史迪仔01125 小时前
[QML] Qt Quick Dialogs 模块使用指南
开发语言·前端·c++·qt
Chase_______5 小时前
LeetCode 1456:定长子串中元音的最大数目
算法·leetcode
小O的算法实验室5 小时前
2026年IEEE IOTJ,DNA序列启发相似性驱动粒子群算法+无人机与基站部署,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
谭欣辰5 小时前
Floyd算法:动态规划解最短路径
c++·算法·图论
计算机安禾5 小时前
【Linux从入门到精通】第12篇:进程的前后台切换与信号控制
linux·运维·算法
6Hzlia5 小时前
【Hot 100 刷题计划】 LeetCode 84. 柱状图中最大的矩形 | C++ 两次单调栈基础扫法
c++·算法·leetcode