时间限制:1 秒

内存限制:32 兆

特殊判题:是

提交:265

解决:116

题目描述:

We start with a stack n of pancakes of distinct sizes. The problem is to convert the stack to one in which the pancakes are in size order with the smallest on the top and the largest on the bottom. To do this, we are allowed to flip the top k pancakes over
as a unit (so the k-th pancake is now on top and the pancake previously on top is now in the k-th position).

For example: This problem is to write a program, which finds a sequence of at most (2n - 3) flips, which converts a given stack of pancakes to a sorted stack.

输入:

Each line of the input gives a separate data set as a sequence of numbers separated by spaces. The first number on each line gives the number, N, of pancakes in the data set. The input ends when N is 0 (zero) with no other data on the line. The remainder
of the data set are the numbers 1 through N in some order giving the initial pancake stack.

The numbers indicate the relative sizes of the pancakes. N will be, at most, 30.

输出:

For each data set, the output is a single-space separated sequence of numbers on a line. The first number on each line, K, gives the number of flips required to sort the pancakes. This number is followed by a sequence of K numbers, each of which gives
the number of pancakes to flip on the corresponding sorting step. There may be several correct solutions for some datasets. For instance 3 3 2 3 is also a solution to the first problem below.

样例输入:
3 1 3 2
5 4 3 2 5 1
0
样例输出:
3 2 3 2
3 3 4 5
来源:
2009年北京大学计算机研究生机试真题

思路:

著名的翻饼子游戏。要求最后状态是小饼在大饼的上面,求翻得次数。最多需要翻2n-3次。

这种题要用逆向思维以及递归方法来分析,考察最终状态及前一步,逐步往前考察,从而发现游戏思路。

代码:

#include <stdio.h>

#define N 30

int n, k;
int a[N+1], r[N+1], step[2*N+1]; void swap(int *x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
} void print(int *x)
{
int j;
for (j=1; j<n; j++)
printf("%d ", x[j]);
printf("%d\n", x[j]);
} void flip(int i)
{
int j;
for (j=1; j<=i/2; j++)
{
swap(&a[j], &a[i-j+1]);
r[a[j]] = j;
r[a[i-j+1]] = i-j+1;
}
step[k++] = i;
//printf("=====");
//print(a);
//print(r);
} int main(void)
{
int i; while (scanf("%d", &n) != EOF && n)
{
for(i=1; i<=n; i++)
{
scanf("%d", &a[i]);
r[a[i]] = i;
} k = 0;
for (i=n; i>=2; i--)
{
if (r[i] != i)
{
if (r[i] != 1)
flip(r[i]);
flip(i);
}
} printf("%d ", k);
for (i=0; i<k-1; i++)
printf("%d ", step[i]);
printf("%d\n", step[i]); } return 0;
}
/**************************************************************
Problem: 1146
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:912 kb
****************************************************************/

九度OJ 1146:Flipping Pancake(翻饼子) (递归、游戏)的更多相关文章

  1. 九度OJ 1006:ZOJ问题 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:18621 解决:3197 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下: 1. ...

  2. 九度OJ 1122:吃糖果 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1522 解决:1200 题目描述: 名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N ...

  3. 九度OJ 1084:整数拆分 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2274 解决:914 题目描述: 一个整数总可以拆分为2的幂的和,例如: 7=1+2+4 7=1+2+2+2 7=1+1+1+4 7=1+1 ...

  4. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  7. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  8. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  9. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

随机推荐

  1. webpack常用配置项配置文件介绍

    一.webpack基础 1.在项目中生成package.json:在项目根目录中输入npm init,根据提示输入相应信息.(也可以不生成package.json文件,但是package.json是很 ...

  2. Codeforces 583 DIV2 GCD Table 贪心

    原题链接:http://codeforces.com/problemset/problem/583/C 题意: 大概就是给你个gcd表,让你还原整个序列. 题解: 由$GCD(a,a)=a$,我们知道 ...

  3. JSP介绍与语法-java之JSP学习第一天(非原创)

    文章大纲 一.JSP 简介二.JSP 生命周期三.JSP 语法四.学习资料下载五.参考文章   一.JSP 简介 1. 什么是Java Server Pages? JSP全称Java Server P ...

  4. (入门SpringBoot)SpringBoot后台验证(八)

    后台验证的作用主要是防止postman...等等工具的恶意提交,前后台都判断数据,双保险. .可以在SpringBoot传参数 加上NotNull.... //分组Default,分组的好处就是可重复 ...

  5. Eclipse4.4以上版本不能使用easyExplorer,采用OpenExplorer

    如果想在Ecipse里打开目录,一直用easyExplorer,可是现在版本升级了easyExplorer不好使,可以用OpenExplorer到https://github.com/samsonw/ ...

  6. 用NSLogger代替NSLog输出调试信息

    安装 NSLogger分为两部分,LoggerClient和NSLogger Viewer,你的App需要导入前者,后者是一个独立的mac应用,NSLogger所有的调试信息将输出到这个应用中. 安装 ...

  7. ThinkPHP示例:CURD

    完整的控制器文件: class IndexAction extends Action { // 查询数据 public function index() { $Form = M("Form& ...

  8. pandas常用操作

    删除某列: concatdfs.drop('Unnamed: 0',axis=1) 打印所有列名: .columns

  9. 【Java编程】JDBC注入攻击-Statement 与 PreparedStatement

    在上一篇[Java编程]建立一个简单的JDBC连接-Drivers, Connection, Statement and PreparedStatement我们介绍了怎样使用JDBC驱动建立一个简单的 ...

  10. openLayers加载高德地图

    之前用openlayers对高德,百度,腾讯,bing,supermap,天地图,arcgis,google等地图进行了对接,今天简单介绍一下openlayers+高德: 在Openlayers.La ...