Winter-2-STL-D The Blocks Problem 解题报告及测试数据

题解:
1、这道题总共有四种堆积木的方法,(1)是move onto 就是将a,b上面的积木恢复到原来的位置。将a堆在b上。(2)是move over 就是将a上面的积木恢复到原来的位置,将a堆在b所在的那一摞积木的最上面。(3)是pile onto 就是将b上面的积木恢复到原来的位置,将a及其上面的积木堆在b上。(4)pile over 就是将a及其上面的积木堆在b所在那一摞积木的最上面。
2、其实如果a,b上面都没有积木,那么pile onto、pile over 其实是和move onto、move over没有区别的,a,b上有积木,区别就在于是否需要将a或b上的积木恢复。所以可以拆分为小函数。(1)是函数restore(int c),将c上的积木恢复。(2)是函数pile(int a,int b)将a及其以上积木,堆在含有b那一摞积木的最上面。
3、那么四种方法可以简化为三步 ,(1)如果是move,那么就restore(a);(2)如果是onto 就restore(b);(3)最后pile(a,b)即可。
4、执行操作过程中,如果a==b 或者a和b在同一摞,则为无效指令,跳过即可,判断在islegal()中。
5、特别需要注意的是输出格式,最后一行的回车不可少,每一行每一个数字前面有一个空格,最后一个数字之后没有空格,不然算法是对了,但是PE就太忧伤了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int A[30][30],x,y,z,w;
char s1[10],s2[10];
int a,b,op,n;
void Find(int c,int &u,int &v){
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(A[i][j]==c){ u=i;v=j;}
}
bool islegal(){
if(a==b)return 0;
Find(a,x,y);
Find(b,z,w);
if(x==z)return 0;
return 1;
}
void restore(int p){
Find(p,x,y);
while(A[x][++y]!=-1){
int t= A[x][y];
A[t][0]=t;
A[x][y]=-1;
}
}
void pile(int a,int b){
Find(a,x,y);
Find(b,z,w);
while(A[z][++w]!=-1);w--;
while(A[x][y]!=-1){
A[z][++w]=A[x][y];
A[x][y++]=-1;
}
}
int main(){
//freopen("1.in","r",stdin);
scanf("%d",&n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)A[i][j]=-1;
A[i][0]=i;
}
while(scanf("%s",s1)!=EOF && s1[0]!='q'){
scanf("%d%s%d",&a,s2,&b);
if(islegal()==0)continue;
if(s1[0]=='m')restore(a);
if(s2[1]=='n')restore(b);
pile(a,b);
}
for(int i=0;i<n;i++){
printf("%d:",i);
int k=0;
while(A[i][k]!=-1)
printf(" %d",A[i][k++]);
printf("\n");
}
}
以下是测试数据:
sample input
10
move 6 on 5
move 4 over 8
pile 9 on 0
move 3 on 9
pile 1 on 9
move 1 over 0
pile 7 over 6
move 1 on 8
pile 5 on 3
move 2 on 5
quit
7
move 6 over 5
pile 3 on 0
pile 5 on 1
move 2 on 0
pile 0 on 1
move 0 over 2
move 1 over 1
move 5 over 3
pile 6 over 5
pile 3 over 1
quit
sample output
0: 0 9
1:
2:
3: 3 5 2
4: 4
5:
6: 6
7: 7
8: 8 1
9:
0:
1: 1 0 2 3 5 6
2:
3:
4: 4
5:
6:
Winter-2-STL-D The Blocks Problem 解题报告及测试数据的更多相关文章
- codeforces B. Routine Problem 解题报告
题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...
- HDU p1294 Rooted Trees Problem 解题报告
http://www.cnblogs.com/keam37/p/3639294.html keam所有 转载请注明出处 Problem Description Give you two definit ...
- 洛谷1303 A*B Problem 解题报告
洛谷1303 A*B Problem 本题地址:http://www.luogu.org/problem/show?pid=1303 题目描述 求两数的积. 输入输出格式 输入格式: 两个数 输出格式 ...
- [poj 2480] Longge's problem 解题报告 (欧拉函数)
题目链接:http://poj.org/problem?id=2480 题目大意: 题解: 我一直很欣赏数学题完美的复杂度 #include<cstring> #include<al ...
- UVa第五章STL应用 习题((解题报告))具体!
例题5--9 数据库 Database UVa 1592 #include<iostream> #include<stdio.h> #include<string.h&g ...
- 【LeetCode】365. Water and Jug Problem 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学题 相似题目 参考资料 日期 题目地址:http ...
- UOJ182 a^-1 + b problem 解题报告
题目描述 有一个长度为\(n(n\le 10^5)\)的数列,在模\(M\)意义下进行\(m(m \le50000)\)次操作,每次操作形如以下两种形式: 1 \(x\) 表示每个数加\(x(0 \l ...
- codeforces 798C.Mike and gcd problem 解题报告
题目意思:给出一个n个数的序列:a1,a2,...,an (n的范围[2,100000],ax的范围[1,1e9] ) 现在需要对序列a进行若干变换,来构造一个beautiful的序列: b1,b2, ...
- sgu 104 Little shop of flowers 解题报告及测试数据
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...
随机推荐
- 【NLP】新词发现
http://www.csdn.net/article/2013-05-08/2815186 http://blog.csdn.net/yuyu2223/article/details/7725705 ...
- ChemDraw Prime 15怎么绘制立体化学结构
众所周知,ChemDraw化学工具的最新版本是ChemOffice 15,其下还有三个适合不同用户的版本,下文详细指导如何使用入门版本ChemDraw Prime 15绘制立体化学结构. 立体化学结构 ...
- php队列算法[转]
<?php/*** php队列算法* * Create On 2010-6-4* Author Been* QQ:281443751* Email:binbin1129@126.com**/cl ...
- Spring_day02--log4j介绍_Spring整合web项目演示
log4j介绍 1 通过log4j可以看到程序运行过程中更详细的信息 (1)经常使用log4j查看日志 2 使用 (1)导入log4j的jar包 (2)复制log4j的配置文件,复制到src下面 3 ...
- C++ 访问控制 public, protected, private, 友元
1. 变量属性与继承之间的关系 #include <iostream> using namespace std; class A { public: int x; protected: i ...
- __init__()
__init__() 是类的内置方法,用于初始化类的内部状态,当我们去实例化一个对象的时候,默认就会执行 __init__() 下面的语句,例子如下: #!/usr/bin/env python #- ...
- 第十篇:Linux中权限的再讨论( 上 )
前言 在Linux系统中,用户分为 个权限位.好了,很多朋友对于Linux权限的了解就仅限于此了.但,Linux目录权限和文件权限一样吗?内核对于权限的检查过程又是怎样的? 如果你不清楚,本文将为你解 ...
- 自定义控件_VIewPager显示多个Item
一直以来想搞明白这个不完全的VIewPager是怎么做到的,有幸看到这片篇文章 有二种实现方法 1.设置的属性 1.clipChildren属性 2.setPageMargin 3.更新Item外界面 ...
- HDU 5900 QSC and Master
题目链接:传送门 题目大意:长度为n的key数组与value数组,若相邻的key互斥,则可以删去这两个数同时获得对应的两 个value值,问最多能获得多少 题目思路:区间DP 闲谈: 这个题一开始没有 ...
- ubuntu下安装myeclipse+破解
1.给myeclipseInstaller.run权限 chmod myeclipseInstaller.run 2.安装(结束时不启动,去掉√) ./myeclipseInstaller.run 3 ...