当前位置:网站首页>The blocks problem (uva101) Purple Book p110vector application
The blocks problem (uva101) Purple Book p110vector application
2022-06-28 20:35:00 【Love_ Jacques】
Purple Book P110;vector Application ;UVa101 The Blocks Problem
Vjudge Please move the title address here
The main idea of the topic :
Input n (0<n<25), Get the number 0 To n-1 Wooden block , They are placed in order and numbered 0 To n-1 The location of . Now operate these wooden blocks , There are four types of operations .
- move a onto b: Put a piece of wood a、b Put the wooden blocks on the back to their original positions , And then a Put it in b On ;
- move a over b: hold a Put the wooden blocks on the back to their original positions , And then a Send to include b On the pile of ;
- pile a onto b: hold b Put the wooden blocks on the back to their original positions , And then a together with a Move the wooden block on to b On ;
- pile a over b: hold a together with a The upper wood block is moved to include b On the pile of .
When the input quit when , End the operation and output 0~n-1 The position of the wooden block .
Sample Input
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit
Sample Output
0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:
Topic analysis ( The idea is based on the purple book ):
data structure choice : Within each cell block The number of is dynamic , Solid can be used vector Simulate a cell , The cells have a total of n individual ,n Values determine , So you can open one. Each unit is vector Array of :vector<int> pile[26]
Algorithm design : The topic involves the movement of whole data and single data , And n The biggest only 25, The time complexity of the simulation method is sufficient .
The simulation process involves two steps :<1. Restore block <2. Move block;
Analyze the four instructions in the topic :
move Need to put a upper block All restored , Move alone a;
pile There is no need to restore a upper block, The whole section needs to be moved ;
onto Need to put b upper block All restored , requirement a And b adjacent ;
over No need to restore b upper block, Move again a Or the whole section .
Analyze the similarities and differences of the four instructions and combine the sequence of first recovery and then movement , Teacher liurujia skillfully handled the following ( Some places are different from the original code ):
//pa and pb by a and b Array subscript of ,ha and hb by a and b Of vector Subscript
if(order1=="move") clear_above(pa,ha);// take pa,ha Above the position block Restore
if(order2=="onto") clera_above(pb,hb);// take pb,hb Above the position block Restore
pile_onto(pa,ha,pb);// take pa,ha Position and above block Move to pb Apex
Module design : thus , The basic framework of the program is : Preprocessing – Input and initialization – simulation – Output –return 0;.
Code :
#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int n,a,b;
string order1,order2;
vector<int> pile[26];
void find_block(int num,int& h,int& p);
void clear_above(int h,int p);
void pile_onto(int p1,int h1,int p2);
void print();
int main()
{
// Read in and initialize
scanf("%d",&n);
for(int i=0;i<n;i++) pile[i].push_back(i);
// simulation
while(cin>>order1>>a>>order2>>b)
{
int ha,hb,pa,pb;
find_block(a,ha,pa);
find_block(b,hb,pb);
if(pa==pb) continue;
if(order1=="move") clear_above(ha,pa);
if(order2=="onto") clear_above(hb,pb);
pile_onto(pa,ha,pb);
//print();
}
// Output
print();
return 0;
}
void find_block(int num,int& h,int& p)
{
for(p=0;p<n;p++)
for(h=0;h<pile[p].size();h++)
if(pile[p][h]==num) return ;
}
void clear_above(int h,int p)
{
for(int i=h+1;i<pile[p].size();i++)
pile[pile[p][i]].push_back(pile[p][i]);
pile[p].resize(h+1);
return ;
}
void pile_onto(int p1,int h1,int p2)
{
for(int i=h1;i<pile[p1].size();i++)
pile[p2].push_back(pile[p1][i]);
pile[p1].resize(h1);
return ;
}
void print()
{
for(int i1=0;i1<n;i1++)
{
printf("%d:",i1);
for(int i2=0;i2<pile[i1].size();i2++) printf(" %d",pile[i1][i2]);
printf("\n");
}
}
Summary of key points and details :
- Teacher liurujia is calculating a Block and b Where the block is h And p when , Adopted Pass value by reference Methods , A function computes two local variables at the same time :
void find_block(int num,int& h,int& p)
- The choice of data structure vector Array , The functions used in the topic are
v.size();v.resize();v.push_back(); - Extract the common ground of the four instructions , Design two functions to reduce repeated code .
Updated on 2020.6.22
边栏推荐
- ThreadLocal principle
- 3. 整合 Listener
- How to analyze the relationship between enterprise digital transformation and data asset management?
- Keyword long
- Flask——总结
- Ref attribute, props configuration, mixin mixing, plug-in, scoped style
- 压缩与解压缩命令
- Why does next() in iterator need to be forcibly converted?
- 【Try to Hack】Cobalt Strike(一)
- TcWind 模式設定
猜你喜欢

APISIX 助力中东社交软件,实现本地化部署

Data standardization processing

C # connect to the database to complete the operation of adding, deleting, modifying and querying

Ref attribute, props configuration, mixin mixing, plug-in, scoped style

题解 Pie(POJ3122)超详细易懂的二分入门

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

Racher add / delete node
![[graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!](/img/e5/b6035abfa7d4bb59c3080d3b87ce45.jpg)
[graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!

学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证

Leetcode 36. 有效的数独(可以,一次过)
随机推荐
Head, tail view file
Fix the simulator that cannot be selected by flutter once
算力时代怎么「算」?「算网融合」先发优势很重要!
TcWind 模式設定
请问同业存单是否靠谱,安全吗
2022 welder (elementary) special operation certificate examination question bank and answers
LeetCode每日一题——30. 串联所有单词的子串
Lecture 30 linear algebra Lecture 4 linear equations
with torch.no_grad():的使用原因
Various types of long
LeetCode每日一题——324. 摆动排序 II
How to add logs to debug anr problems
怎么理解云原生数据库的易用性?
Leetcode 36. Effective Sudoku (yes, once)
Pipeline | and redirection >
【学习笔记】因子分析
Anr no response introduction
学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证
risc-v指令集
题解 The SetStack Computer(UVa12096)紫书P116STL的综合应用