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

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

相关推荐
专注仿真39 分钟前
梯度提升类(XFBoost)算法详解与代码实例
开发语言·人工智能·python·算法·大模型训练·控制算法
小豆庄1 小时前
视频遮罩追踪退场后ID撞号的排查—多目标场景下的失效
opencv·算法·bug·视频·python3.11
Tisfy1 小时前
LeetCode 3882.网格图中最小异或路径:动态规划
算法·leetcode·动态规划
D e re k ZH2 小时前
P5025 [SNOI2017] 炸弹
c++·算法
A boy CDEF girl2 小时前
算法题专栏:优选算法
数据结构·算法·leetcode
没落之王2 小时前
三易五行看宋朝的三界确立
大数据·人工智能·算法
wuminyu3 小时前
Java字节码学习总结:字节码如何加载、解析和执行
java·linux·c语言·jvm·c++
qq_406981033 小时前
c++的decltype是什么
开发语言·c++
坤坤子吖3 小时前
算法学习——单调队列
开发语言·c++·笔记·学习·算法
杜 硕4 小时前
从零开始的C++生活——第六讲——01
开发语言·c++·生活