The Blocks Problem

题目:

代码:

#include <iostream>

#include <vector>

using namespace std;

const int N = 30;

typedef pair<int, int> PII;

vector<int> pN;

int n;

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

{

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

{

for(int j = 0; j < pi.size(); j++)

{

if(pij == x)

{

return {i, j};

}

}

}

}

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

{

for(int j = y + 1; j < px.size(); j++)

{

int t = pxj;

pt.push_back(t);

}

px.resize(y + 1);

}

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

{

for(int j = y1; j < px1.size(); j++)

{

px2.push_back(px1j);

}

px1.resize(y1);

}

int main()

{

cin >> n;

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

{

pi.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 < pi.size(); j++)

{

cout << " " << pij;

}

cout << endl;

}

return 0;

}

总结:

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

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

相关推荐
careathers3 分钟前
【数据结构】栈
java·数据结构
远航计算机8 分钟前
GEO 选题从哪来?用一份 Query 词库把一年内容排出来
大数据·人工智能·算法·aigc
宣宣猪的小花园.8 分钟前
【机器学习】神经网络与表征:为什么深度学习能自动提取特征
人工智能·嵌入式硬件·算法·机器学习
mmmmath_323 分钟前
LeetCode.018.四数之和
数据结构·算法·leetcode
AIGCmagic社区24 分钟前
腾讯混元 AuK 技术报告拆读:1.5B 统一语音生成与编辑,蒸馏后 4 步无 CFG 提速 4.5 倍
人工智能·算法·aigc·ai多模态
大熊背25 分钟前
基于照明色度表征的颜色恒常性的白平衡算法实现
算法·白平衡·色温·色度坐标·普朗克曲线
高亦真32 分钟前
今天是学习嵌入式的第38天
linux·学习·算法
蒸蒸yyyyzwd39 分钟前
cpp 选手备战秋招学习笔记 day36
c++
用户69586106727111 小时前
从源码到加载器:__attribute__((constructor)) / __attribute__((destructor))的跨平台实现原理详解
c++
淡海水1 小时前
11-02-Unity-Mono-vs-IL2CPP-两种脚本后端的数据结构行为差异
数据结构·unity·游戏引擎·il2cpp·mono