The Blocks Problem

题目:

代码:

#include <iostream>

#include <vector>

using namespace std;

const int N = 30;

typedef pair<int, int> PII;

vector<int> p[N];

int n;

PII find(int x)//找到编号为x的木块的位置

{

for(int i = 0; i <n; i++)

{

for(int j = 0; j < p[i].size(); j++)

{

if(p[i][j] == x)

{

return {i, j};

}

}

}

}

void clean(int x, int y)//把(x, y)位置的木块上方木块归位

{

for(int j = y + 1; j < p[x].size(); j++)

{

int t = p[x][j];

p[t].push_back(t);

}

p[x].resize(y + 1);

}

void move(int x1, int y1, int x2)//把(x1, y1)及以上的木块挪到x2最上面

{

for(int j = y1; j < p[x1].size(); j++)

{

p[x2].push_back(p[x1][j]);

}

p[x1].resize(y1);

}

int main()

{

cin >> n;

for(int i = 0; i < n; i++)

{

p[i].push_back(i);

}

string op1, op2;

int a, b;

while(cin >> op1 >> a >> op2 >> b)

{

int x1, y1, x2, y2;

PII pa, pb;

pa = find(a);

pb = find(b);

x1 = pa.first; y1 = pa.second;

x2 = pb.first; y2 = pb.second;

if(x1 == x2) continue;

if(op1 == "move")

{

clean(x1, y1);

}

if(op2 == "onto")

{

clean(x2, y2);

}

move(x1, y1, x2);

}

for(int i = 0; i < n; i++)

{

cout << i << ":";

for(int j = 0; j < p[i].size(); j++)

{

cout << " " << p[i][j];

}

cout << endl;

}

return 0;

}

总结:

本题主要考察vector数组的使用,可以通过抽象出操作find()、clear()、move(),然后分情况组合解决问题

坚持编程,我一直在路上!

相关推荐
杜子不疼.1 天前
【C++ AI 大模型接入 SDK】 - DeepSeek 模型接入(上)
开发语言·c++·chatgpt
石山代码1 天前
C++ 内存分区 堆区
java·开发语言·c++
心中有国也有家1 天前
cann-recipes-infer:昇腾 NPU 推理的“菜谱集合”
经验分享·笔记·学习·算法
绝知此事1 天前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
碧海银沙音频科技研究院1 天前
通话AEC与语音识别AEC的软硬回采链路
深度学习·算法·语音识别
csdn_aspnet1 天前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
LuminousCPP1 天前
数据结构 - 线性表第四篇:C 语言通讯录优化升级全记录(踩坑 + 思考)
c语言·开发语言·数据结构·经验分享·笔记·学习
张小姐的猫1 天前
【Linux】多线程 —— 线程互斥
linux·运维·服务器·c++
AI算法沐枫1 天前
深度学习python代码处理科研测序数据
数据结构·人工智能·python·深度学习·决策树·机器学习·线性回归