九度OJ 1146:Flipping Pancake(翻饼子) (递归、游戏)
时间限制: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
思路:
著名的翻饼子游戏。要求最后状态是小饼在大饼的上面,求翻得次数。最多需要翻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(翻饼子) (递归、游戏)的更多相关文章
- 九度OJ 1006:ZOJ问题 (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:18621 解决:3197 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下: 1. ...
- 九度OJ 1122:吃糖果 (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1522 解决:1200 题目描述: 名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N ...
- 九度OJ 1084:整数拆分 (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2274 解决:914 题目描述: 一个整数总可以拆分为2的幂的和,例如: 7=1+2+4 7=1+2+2+2 7=1+1+1+4 7=1+1 ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
随机推荐
- LoadRunner安装破解
安装过程 1. 运行“setup.exe” 点击安装,其中会有提示缺少“Microsoft Visual C++ 2005 SP1等运行组件”,下载这些组件.这里安装“vcredist_x86.exe ...
- zoj 2615 Cells 栈的运用
题目链接:ZOJ - 2615 Scientists are conducting research on the behavior of a newly discovered Agamic Cell ...
- ArcObject开发时,axtoolbarcontrol中一些添加的按钮是灰色的问题
以Pan按钮为例,当axtoolbarcontrol设置好buddycontrol后,如果你有两个视图的话有些工具pagelayout视图下有用,有些在map视图下有用. 例如,在以下图的方式添加的p ...
- 【spring cloud】子模块启动报错com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect
spring cloud子模块启动报错 Caused by: java.lang.ClassNotFoundException: com.netflix.hystrix.contrib.javanic ...
- 【IntelliJ IDEA】spring boot项目在idea实现自动部署
转载参考自:https://www.cnblogs.com/winner-0715/p/6666579.html spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能 ...
- python selenium中调用js
python 中js中单引号和双引号混合编程 js = 'document.getElementsByName("m:ybzbxmbd:b_BIANHAO")[0].setAttr ...
- openssl之EVP系列之12---EVP_Seal系列函数介绍
openssl之EVP系列之12---EVP_Seal系列函数介绍 ---依据openssl doc/crypto/EVP_SealInit.pod翻译和自己的理解写成 (作者:Dra ...
- 转:java的各个拓展类库的推荐方案
from: 链接:https://www.zhihu.com/question/21142149/answer/109854408 Java是世界上最强大的编程语言之一,很多开发人员和大型企业都偏爱J ...
- js:|| 和 && 运算符 特殊用法
引用:http://www.jb51.net/article/21339.htm 先总结一下: 几乎所有语言中||和&&都遵循“短路”原理,如&&中第一个表达式为假就不 ...
- hint指定index的深入理解
http://czmmiao.iteye.com/blog/1480247创建一个表,含有位图index和b-tree index SQL> create table t as select o ...