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

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;
}
相关推荐
焦耳加热14 分钟前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
wan5555cn21 分钟前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
u6061 小时前
常用排序算法核心知识点梳理
算法·排序
鹅毛在路上了2 小时前
C++, ffmpeg, libavcodec-RTSP拉流,opencv实时预览
c++·opencv·ffmpeg
John_ToDebug2 小时前
定制 ResourceBundle 的实现与 DuiLib 思想在 Chromium 架构下的应用解析
c++·chrome·ui
蒋星熠3 小时前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
小欣加油3 小时前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展
3Cloudream4 小时前
LeetCode 003. 无重复字符的最长子串 - 滑动窗口与哈希表详解
算法·leetcode·字符串·双指针·滑动窗口·哈希表·中等
王璐WL4 小时前
【c++】c++第一课:命名空间
数据结构·c++·算法
aramae4 小时前
C++ -- 模板
开发语言·c++·笔记·其他