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

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

相关推荐
IronMurphy1 天前
【算法四十三】279. 完全平方数
算法
墨染天姬1 天前
【AI】Hermes的GEPA算法
人工智能·算法
papership1 天前
【入门级-数据结构-3、特殊树:完全二叉树的数组表示法】
数据结构·算法·链表
smj2302_796826521 天前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
山甫aa1 天前
差分数组 ----- 从零开始的数据结构
数据结构
早日退休!!!1 天前
《数据结构选型指南》笔记
数据结构·数据库·oracle
Beginner x_u1 天前
链表专题:JS 实现原理与高频算法题总结
javascript·算法·链表
丑八怪大丑1 天前
Java数据结构与集合源码
数据结构
c++之路1 天前
C++信号处理
开发语言·c++·信号处理
_深海凉_1 天前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展