原本以为是一道很简单的模拟题,结果写了一个小时。。。很长时间不碰算法题,的确手感差很多。不过我觉得随着刷题慢慢多起来应该会好的。

题目的意思也有点含糊,需要自己去猜,大概意思就是槽里有一堆木头,每个槽刚开始的时候只有一个,需要移过来移过去,有四种方式,这四种方式都是针对木头而言的,因此我们必须时刻记录每个木头的位置。当然还需要数据结构记录槽的状态,最后需要输出。

四种移动方式有一些是共通的,因此需要将其抽象成函数,我这里抽象了三个函数:

void ret_block(int x);

将木头x头顶的木头归还到原本的槽里面(i号木头到i号槽)

void mve(int x, int idx);

将木头x移动到槽idx的位置,之所以不叫move是因为不想和标准库的move函数冲突

void pile(int x, int idx);

将木头x以及其头顶的所有木头移动到槽idx

虽然使用三个函数简化了四种操作,但是我觉得自己分离的不够清晰,按道理讲pile函数应该调用mve函数,因为一个是移动一个木头,一个是移动一堆木头,可是因为使用的是vector,导致无法随机插入。

顺带吐槽一下:我为了控制不输出最后的换行专门写了一个Newline函数类,但是直接报错。。有的OJ要求不能有,有的又要求必须有。。

看了一下别人的题解,有两点收获:

  • 不用判断整个字符串再确定是什么命令,判断一下首字母就可以了
  • 可以使用erase函数整块删除。自己就是因为不知道这个函数写的比较复杂,还是要对STL更加熟悉才行
    再研究了一下书上的题解,发现果然pile函数可以和move函数合并,而且可是使用resize函数进行删除。

对于这种多种指令的,我们要提取出指令之间的共同点,编写函数以减少重复代码。

#include <iostream>
#include <vector>
#include <string>
#include <array>using namespace std;namespace {const string QUIT = "quit";const string MOVE = "move";const string ONTO = "onto";const string OVER = "over";const string PILE = "pile";constexpr int MAXN = 25 + 5;array<vector<int>, MAXN> blocks;array<pair<int, int>, MAXN> pos;int n;
}void init() {cin >> n;for (int i = 0; i < n; ++i) {blocks[i].push_back(i);pos[i] = {i, 0};}
}void mve(int x, int idx) {blocks[idx].push_back(x);blocks[pos[x].first].pop_back();pos[x].first = idx;pos[x].second = blocks[idx].size() - 1;
}void pile(int x, int idx) {auto &block_x = blocks[pos[x].first];auto &block_dst = blocks[idx];int origin_pos = pos[x].second;int y;for (int i = origin_pos; i < block_x.size(); ++i) {y = block_x[i];block_dst.push_back(y);pos[y].first = idx;pos[y].second = block_dst.size() - 1;}for (int i = block_x.size() - 1; i >= origin_pos; --i) block_x.pop_back();
}void ret_block(int x) {//return blocks that are stacked on top of xauto &block_x = blocks[pos[x].first];int y;for (int i = block_x.size() - 1; i > pos[x].second; --i) {y = block_x.back();mve(y, y);}
}void work() {int x, y;string action, prep;while (cin >> action) {if (action == QUIT) break;cin >> x >> prep >> y;if (pos[x].first == pos[y].first) continue;if (action == MOVE) {if (prep == ONTO) {//move x onto yret_block(x);ret_block(y);mve(x, pos[y].first);} else {ret_block(x);mve(x, pos[y].first);}} else {if (prep == ONTO) {ret_block(y);pile(x, pos[y].first);} else {pile(x, pos[y].first);}}}
}class Newline {bool first;
public:Newline(bool _fisrt = true):first(_fisrt) {}inline void operator ()();
};inline void Newline::operator()() {if (first) {first = false;} else {cout << "\n";}
}void print() {Newline newline;for (int i = 0; i < n; ++i) {//newline();cout << i << ":";for (auto x : blocks[i]) {cout << " " << x;}cout << "\n";}
}int main() {ios::sync_with_stdio(false);init();work();print();
}

UVA - 101:The Blocks Problem相关推荐

  1. uva 101 The Blocks Problem

    1.     move a onto b在將a搬到b上之前,先將a和b上的積木放回原來的位置(例如:1就放回1的最開始位罝) 2. move a over b在將a搬到b所在的那堆積木之上之前,先將a ...

  2. Uva 101 the block problem 木块问题(算法竞赛经典入门)STL vector

    Uva 101 the block problem 木块问题 题目大意: 输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置.现对这些木块进行操作,操作分为四种. 1.mov ...

  3. the blocks problem(uva 101 or poj 1208)

    题目描述见:uva 101 or poj 1208 关键在于彻底理解题目中搬积木的几个命令的含义,见具体分析 如果还不能理解题意,那么找一个正确通过的代码,编译并输入测试数据,查看其每一个命令的执行情 ...

  4. UVa101 - The Blocks Problem

    //UVa101 - The Blocks Problem #include<iostream> #include<cstdio> #include<string> ...

  5. 题解 The Blocks Problem(UVa101)紫书P110vector的应用

    紫书P110:vector的应用:UVa101 The Blocks Problem Vjudge题目地址请移步此处 题目大意: 输入n (0<n<25),得到编号为0到n-1的木块,分别 ...

  6. UVA - 524 Prime Ring Problem

    题目链接: UVA - 524 Prime Ring Problem Description(素数环) A ring is composed of n (even number) circles as ...

  7. 《算法竞赛入门经典》 例题5-2 木块问题(The Blocks Problem,UVa 101)

    原题及翻译 Many areas of Computer Science use simple, abstract domains for both analytical and empirical ...

  8. Uva 101:木块问题 The Blocks Problem(详细说明)+(难点)

    算法竞赛入门经典 开始接触ACM,老师推荐了紫皮书,但是C++渣渣的我读题都很费劲,搜罗博主文章发现几乎都是 原题+代码 .现尽己所能整理,比较啰(xiang)嗦(xi),希望能对同起步小白有所帮助, ...

  9. 【UVA】11991 Easy Problem from Rujia Liu? (整数v第k次出现在什么位置)

    https://vjudge.net/problem/UVA-11991 题目大意:就是给你一个序列,然后给出k和v,看整数v第k次出现在该序列的什么位置,没有的话就输出0 结构体(略复杂): #in ...

最新文章

  1. 写一个函数,要求输入一个字符串和一个字符长度,根据字符长度对该字符串进行分隔
  2. Xcode7  创建纯代码的项目
  3. 文档自动排序长短_css 文档流
  4. BZOJ2093 : [Poi2010]Frog
  5. “要么你去驾驭生命,要么生命驾驭你。你的心态决定谁是坐骑,谁是骑师。”...
  6. python编程入门到实践笔记-python基础(《Python编程:从入门到实践》读书笔记)...
  7. Android和.NET通用的AES算法
  8. 用计算机弹奏体面6,抖音能用计算器按出音乐有哪些?计算器乐谱分享
  9. 杭电4786--Fibonacci Tree(生成树)
  10. CDA二级建模分析师考试相关
  11. Minimum Class Confusion for Versatile Domain Adaptation
  12. 如何开通一个微信公众号
  13. Excel输入公式计算只显示公式不出结果
  14. java 过滤字符串_java 过滤字符串方法实现
  15. js实现简单的倒计时功能
  16. MIT JOS lab2内存管理实验记录
  17. 《Java8实战》第3章 Lambda 表达式
  18. eclipse离线安装PMD(含PMD离线包)
  19. 基于SC-LIO-SAM的SLAM实践
  20. 安卓界面UI设计的尺寸标注问题

热门文章

  1. 关于DJANGO MODELS的个人理解和RELATED_NAME的使用
  2. How to fix the bug “Expected required, optional, or repeated.”?
  3. 即时聊天IM之二 openfire 整合现有系统用户
  4. OpenFire源码学习之十九:在openfire中使用redis插件(上)
  5. 百度文档搜索与Google文档搜索的简单比较
  6. ADO.NET中在C/S模式中使用的连接池
  7. python高阶函数介绍_python高级特性和高阶函数及使用详解
  8. python自动补全库_这个库厉害了,自动补全Python代码,节省50%敲码时间
  9. android studio gradle 学习,学习Android Studio里的Gradle
  10. mysql delete temporary denied_这些错误是什么意思?djang中的mysql