九度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 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
随机推荐
- FZU 1056 扫雷游戏【搜索】
Accept: 2584 Submit: 6790Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 扫雷是 ...
- Codeforces 903F Clear The Matrix(状态压缩DP)
题目链接 Clear The Matrix 题意 给定一个$4 * n$的矩形,里面的元素为$'.'$或$'*'$.现在有$4$种正方形可以覆盖掉$'*'$,正方形的边长分别为$1,2,3,4$. 求 ...
- 2017 ACM-ICPC EC-Final 记录
北京赛区结束后就以为自己的赛季结束了……但是还是保持着做题量 那天突然接到通知,去打EC-Final 但是这是一个临时组起来的队伍,另外两位队友原来一起组的比较熟,我就需要适应一下. 于是我们临时训练 ...
- python安装在windows server2008,故障排除
python安装在windows server2008,故障排除 我也在虚拟机上的win2008安装python2.7.9多次回滚了.搜了一通 Windows Server 2003/2008无法 ...
- 安全搜索引擎Shodan(搜蛋)命令行模式使用TIPS
https://www.shodan.io/ 与谷歌通过网址来搜索互联网的方式不同,Shodan通过互联网背后的通道来搜索信息.它就象是一种“黑暗”的谷歌,不断在寻找服务器.网络摄像头.打印机.路由器 ...
- Can''t find the channel handler for deviceType 工行 个人网银 错误
背景描述:系统Win7,浏览器IE8.登录工商银行个人网银的时候,输入帐号密码和验证码后,出现空白页面,上面一句话 Can''t find the channel handler for devic ...
- 【2048小游戏】——原生js爬坑之遍历算法显示二维数组内容
引言:做2048小游戏会将横纵方向的数字内容,存储在一个二维数组中,要将这个二维数组中的内容显示在页面上,就一定要用遍历算法来实现了. 一.二维数组存储 首先考虑用二维数组存储所有行数,列数 ...
- Ethernet帧格式
一.Ethernet帧格式的发展 1980 DEC,Intel,Xerox制订了Ethernet I的标准 1982 DEC,Intel,Xerox又制订了Ehternet II的标准 1982 IE ...
- 微信小程序 - 下载图片并且显示进度
lastUpDate: 2018-8-12 请把请求地址加入到downLoadFile 提示:首先得要在公众号设置对应的downLoadFile地址. downImg : 下载图片 wxml js d ...
- Android开发之短信验证码示例
在说Android中的短信验证码这个知识点前,我们首先来了解下聚合数据 聚合数据介绍 聚合数据是一家国内最大的基础数据API提供商,专业从事互联网数据服务.免费提供从天气查询.空气质量.地图坐标到金融 ...