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

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

相关推荐
霖大侠8 分钟前
Decoupled and Reusable Adaptation for Efficient Cross-Modal Transfer
人工智能·深度学习·算法·机器学习·transformer
Sunsets_Red17 分钟前
浅谈博弈论
c++·学习·编程·博弈论·信息学竞赛·巴巴博弈
OOJO26 分钟前
哈希表实现
数据结构·哈希算法·散列表
ALex_zry38 分钟前
C++26 std::inplace_vector 详解:零堆分配的定容向量
开发语言·c++
卷无止境1 小时前
C++ 类型转换变迁之路:从隐式转换到 static_cast
c++·后端
Sam09271 小时前
【AI 算法精讲 16】BPE 分词:从字节对到子词
人工智能·python·算法·ai
LJHclasstore_luo1 小时前
【题解】WebGoC 102683.大雨过后
算法·goc编程
MrZhao4001 小时前
Agent 如何持续工作:任务持久化、后台执行与定时唤醒
算法
用户701720739001 小时前
密码学之分组密码
算法