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(),然后分情况组合解决问题

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

相关推荐
Sashtok2 小时前
cpp基础知识(10)
c++·后端
qq_348231852 小时前
大模型算法: 反向传播
算法
电科_银尘2 小时前
【大模型算法与工程 27】训练配方:批量、学习率、精度与梯度裁剪
学习·算法·机器学习
专注仿真3 小时前
智能体训练之模仿学习:GAIL 算法详解与实战
python·算法·仿真·多智能体强化学习·afsim智能体训练
whitelbwwww3 小时前
CUDA C++实战--中级
开发语言·前端·c++
leobertlan5 小时前
痛苦系列 | DSP-04 截断时空的“窗”:FIR 滤波器设计与线性相位
算法
老师我太想进步了20265 小时前
【数据结构-12】树与二叉树的结构、概念
数据结构
ShineWinsu6 小时前
对于Qt:QMessageBox的解析
开发语言·c++·qt·面试·客户端·qmessagebox·消息对话框
秋天的怀念_David6 小时前
算法一千零一夜 · 第三十五夜 · 恢复风暴
java·程序人生·算法