UVa 101 (模拟) The Blocks Problem

题意:

有n个木块及n个木块堆,初始状态是第i个木块在第i个木块堆上。对应有四种操作,然后输出最终状态。

分析:

用一个vector<int>模拟一个木块堆,进行相应操作即可。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <string>
 5 using namespace std;
 6
 7 const int maxn = 30;
 8 int n;
 9 vector<int> pile[maxn];
10
11 void find_block(int a, int& p, int& h)
12 {
13     for(int i = 0; i < n; ++i)
14         for(int j = 0; j < pile[i].size(); ++j)
15             if(pile[i][j] == a)
16             { p = i; h = j; }
17 }
18
19 void clear_block(int p, int h)
20 {//将第p堆高度为h的上方的木块归位
21     for(int i = h+1; i < pile[p].size(); ++i)
22     {
23         int k = pile[p][i];
24         pile[k].push_back(k);
25     }
26     pile[p].resize(h+1);
27 }
28
29 void move_onto(int pa, int h, int pb)
30 {//将pa堆高度为h及以上的木块摞到pb堆上
31     for(int i = h; i < pile[pa].size(); ++i)
32         pile[pb].push_back(pile[pa][i]);
33     pile[pa].resize(h);
34 }
35
36 int main()
37 {
38     //freopen("in.txt", "r", stdin);
39     int a, b;
40     scanf("%d", &n);
41     for(int i = 0; i < n; ++i) pile[i].push_back(i);
42     string s1, s2;
43     while(cin >> s1 >> a >> s2 >> b)
44     {
45         int pa, pb, ha, hb;
46         find_block(a, pa, ha);
47         find_block(b, pb, hb);
48         if(pa == pb) continue;
49         if(s2 == "onto") clear_block(pb, hb);
50         if(s1 == "move") clear_block(pa, ha);
51         move_onto(pa, ha, pb);
52     }
53     for(int i = 0; i < n; ++i)
54     {
55         printf("%d:", i);
56         for(int j = 0; j < pile[i].size(); ++j) printf(" %d", pile[i][j]);
57         printf("\n");
58     }
59
60
61     return 0;
62 }

代码君

时间: 2024-11-08 22:58:26

UVa 101 (模拟) The Blocks Problem的相关文章

uva 101 The Blocks Problem (模拟)

uva 101  The Blocks Problem Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of planning and robotics (STRIPS) used a block world in which a robot arm per

uva 101 The Blocks Problem (基本算法-模拟)

 The Blocks Problem  Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of planning and robotics (STRIPS) used a block world in which a robot arm performed

UVA - 101 The Blocks Problem(STL,模拟)

The Blocks Problem Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an e

101 - The Blocks Problem

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=37   The Blocks Problem  Background Many areas of Computer Science use simple, abstract domains for both analytical and empiric

POJ 1208 The Blocks Problem

The Blocks Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5397   Accepted: 2312 Description Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of

UVA101 HDU1612 POJ1208 The Blocks Problem

问题链接:UVA101 HDU1612 POJ1208 The Blocks Problem. 这是一个模拟题,程序过程都是套路. 程序中用到了STL的容器类vector. 这个程序在UVA和POJ中都AC,可是在HDU中是"Presentation Error". AC通过的C++语言程序如下: /* UVA101 HDU1612 POJ1208 The Blocks Problem */ #include <iostream> #include <string&g

【UVA】434-Matty&#39;s Blocks

一道很容易想复杂的题,给出主视图和右视图,计算最少能用几个正方体组成对应的视图,以及最多还能加几块正方体. 求最多添加其实就是求出最多的正方体数减去最少的,主要就是最少的不好求. 一开始各种模拟就是不对,之后发现,只需要统计两个视图的高度个数就可以了(简直了) 14390495 434 Matty's Blocks Accepted C++ 0.016 2014-10-21 11:35:11 #include<cstdio> #include<cstring> #include&l

uva 101 History Grading

Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put several historical events into chronological order. Students who order all the events co

UVA101 The Blocks Problem

 The Blocks Problem  Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AIstudy of planning and robotics (STRIPS) used a block world in which arobot arm performed ta