uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
挺水的模拟题,刚开始题目看错了,poj竟然过了。。。无奈。uva果断wa了
搞清题目意思后改了一下,过了uva。
题目要求模拟木块移动:
有n(0<n<25)快block,有5种操作:
move a onto b 在将a搬到b上之前,先把a和b上的积木放回原來的位置
move a over b在将a搬到b所在的那堆积木上前,先把a上的积木放回原來的位罝
pile a onto b 将包括a本身和上方的积木一起放到b上,在放之前b上方的积木放回原来的位置
pile a over b 将包括a本身和上放的积木一起搬到到b所在的那堆上
quit结束命令,前四个动作中若ab在同一堆中,则不做改变。
我用的vector模拟,其实用线性表也不错的,纯当练习stl了。
代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int>::iterator VI; vector <int> v[30];
int rec[30];
int n; VI Find(vector <int> &v, int num) {
for (VI i = v.begin(); i != v.end(); i++)
if (*i == num) {
return i;
}
return v.begin() - 1;
}//find num in vector v void check() {
/* for (int i = 0; i < n; i++)
cout << rec[i] << ' ';
cout << endl;
*/
for (int i = 0; i < n; i++) {
cout << i << ':';
if (v[i].empty()) {
cout << endl;
continue;
}
cout << ' ' << v[i][0];
for (int j = 1; j < v[i].size(); j++)
cout << ' ' << v[i][j];
cout << endl;
}
} int main() {
cin >> n;
for (int i = 0; i < n; i++) {
v[i].clear();
v[i].push_back(i);
rec[i] = i;
}//for init
string ts;
int tn1, tn2;
while (cin >> ts && ts != "quit") {
if (ts == "move") {
cin >> tn1 >> ts >> tn2;
if (rec[tn1] == rec[tn2]) continue;
while (v[rec[tn1]].back() != tn1) {
rec[v[rec[tn1]].back()] = v[rec[tn1]].back();
v[v[rec[tn1]].back()].push_back(v[rec[tn1]].back());
v[rec[tn1]].pop_back();
}
v[rec[tn1]].pop_back();
if (ts == "onto") {
VI i;
while (v[rec[tn2]].back() != tn2) {
rec[v[rec[tn2]].back()] = v[rec[tn2]].back();
v[v[rec[tn2]].back()].push_back(v[rec[tn2]].back());
v[rec[tn2]].pop_back();
}
v[rec[tn2]].push_back(tn1);
rec[tn1] = rec[tn2];
}//move onto
else {
v[rec[tn2]].push_back(tn1);
rec[tn1] = rec[tn2];
}//move over
}
else {
cin >> tn1 >> ts >> tn2;
if (rec[tn1] == rec[tn2]) continue;
if (ts == "onto") {
while (v[rec[tn2]].back() != tn2) {
rec[v[rec[tn2]].back()] = v[rec[tn2]].back();
v[v[rec[tn2]].back()].push_back(v[rec[tn2]].back());
v[rec[tn2]].pop_back();
}
VI pos1 = Find(v[rec[tn1]], tn1), pos2 = Find(v[rec[tn2]], tn2);
int tmp[25], cnt = 0;
for (VI i = v[rec[tn1]].end() - 1; i >= pos1; i--) {
// cout << "*i=" << *i << endl;
tmp[cnt++] = *i;
v[rec[tn1]].erase(i);
rec[*i] = rec[tn2];
}
for (int i = 0; i < cnt / 2; i++) {
int t = tmp[i];
tmp[i] = tmp[cnt - i - 1];
tmp[cnt - i - 1] = t;
}
v[rec[tn2]].insert(pos2 + 1, tmp, tmp + cnt); }//pile onto
else {
VI pos1 = Find(v[rec[tn1]], tn1);
int tmp[25], cnt = 0;
for (VI i = v[rec[tn1]].end() - 1; i >= pos1; i--) {
// cout << "*i=" << *i << endl;
tmp[cnt++] = *i;
v[rec[tn1]].erase(i);
rec[*i] = rec[tn2];
}
for (int i = 0; i < cnt / 2; i++) {
int t = tmp[i];
tmp[i] = tmp[cnt - i - 1];
tmp[cnt - i - 1] = t;
}
v[rec[tn2]].insert(v[rec[tn2]].end(), tmp, tmp + cnt);
}//pile over
}
}//while
check();
return 0;
}
uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟的更多相关文章
- POJ 1208 The Blocks Problem
The Blocks Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5397 Accepted: 231 ...
- POJ 1208 The Blocks Problem --vector
http://poj.org/problem?id=1208 晚点仔细看 https://blog.csdn.net/yxz8102/article/details/53098575 #include ...
- B -- POJ 1208 The Blocks Problem
参考:https://blog.csdn.net/yxz8102/article/details/53098575 https://www.cnblogs.com/tanjuntao/p/867892 ...
- PKU 1208 The Blocks Problem(模拟+list应用)
题目大意:原题链接 关键是正确理解题目意思 首先:介绍一下list容器的一些操作:参考链接 list<int> c1; c1.unique(); 去重. c1.r ...
- The Blocks Problem(vector)
题目链接:http://poj.org/problem?id=1208 The Blocks Problem Time Limit: 1000MS Memory Limit: 10000K Tot ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- UVa 101 The Blocks Problem Vector基本操作
UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...
- UVa 101 - The Blocks Problem(积木问题,指令操作)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 【UVA - 101】The Blocks Problem(vector+模拟)
The Blocks Problem Descriptions:(英语就不说了,直接上翻译吧) 初始时从左到右有n个木块,编号为0~n-1,要求实现下列四种操作: move a onto b: 把a和 ...
随机推荐
- URAL 2073 Log Files (模拟)
题意:给定 n 场比赛让你把名称,时间,比赛情况按要求输出. 析:很简单么,按照要求输出就好,注意如果曾经AC的题再交错了,结果也是AC的. 代码如下: #pragma comment(linker, ...
- 转载sublime text3中package Control
Sublime Text 3 安装Package Control 原来Subl3安装Package Control很麻烦,现在简单的方法来了 一.简单的安装方法 使用Ctrl+`快捷键或者通过View ...
- AFNetworking上传文件
-(void)upload{ AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSD ...
- Labeled ContentControl & LabeledControl【项目】
LabeledTextBoxControl: C#定义 using System; using System.Collections.Generic; using System.Linq; using ...
- jquery 显示“加载状态 结束”
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- VC++内存区域
转载声明:本文转载自http://blog.csdn.net/sjxbf/article/details/6441730 一.预备知识—程序的内存分配 一个由c/c++编译的程序占用的内存分为以下几个 ...
- CSS Layout
fontline-heightcolormarginpaddingbordertext-alignbackground widthheightfloatcleardisplay 定位属性 属 性 描 ...
- ARM Cortex-M instructions
ARM Cortex-M instruction sets ARMCortex-M Thumb Thumb-2 Hardwaremultiply Hardwaredivide Saturatedmat ...
- SAP MRP的计算步骤
SAP MRP的计算步骤,物料需求计划(简称为MRP)与主生产计划一样属于ERP计划管理体系,它主要解决企业生产中的物料需求与供给之间的关系,即无论是对独立需求的物料,还是相关需求的物料, ...
- linux scp 服务器远程拷贝
一.将本机文件复制到远程服务器上 #scp /home/administrator/news.txt root@192.168.1.1:/etc/squid /home/administrator/ ...