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

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;
}
相关推荐
Omigeq1 天前
1.4 - 曲线生成轨迹优化算法(以BSpline和ReedsShepp为例) - Python运动规划库教程(Python Motion Planning)
开发语言·人工智能·python·算法·机器人
郭涤生1 天前
C++模板元编程理论基础简介
c++
CheerWWW1 天前
C++学习笔记——栈内存与堆内存、宏、auto、std::array
c++·笔记·学习
网络工程小王1 天前
【大模型(LLM)的业务开发】学习笔记
人工智能·算法·机器学习
y = xⁿ1 天前
【Leet Code 】滑动窗口
java·算法·leetcode
WBluuue1 天前
数据结构与算法:二项式定理和二项式反演
c++·算法
nianniannnn1 天前
力扣104.二叉树的最大深度 110. 平衡二叉树
算法·leetcode·深度优先
_深海凉_1 天前
LeetCode热题100-只出现一次的数字
算法·leetcode·职场和发展
yashuk1 天前
C语言 vs. C++ ,哪个更适合初学者?
c语言·c++·面向对象编程·初学者·学习路径
-许平安-1 天前
MCP项目笔记十(客户端 MCPClient)
c++·笔记·ai·raii·mcp·pluginapi·plugin system