POJ 3601:Tower of Hanoi
2016-05-11 19:08
309 查看
Tower of Hanoi
Description
The Tower of Hanoi is a puzzle consisting of three pegs and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another peg, obeying the following rules:
Only one disk may be moved at a time.
Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg.
No disk may be placed on top of a smaller disk.
For n disks, it is a well-known result that the optimal solution takes 2n − 1 moves.
To complicate the puzzle a little, we allow multiple disks to be of the same size. Moreover, equisized disks are mutually distinguishable. Their ordering at the beginning should be preserved at the end, though it may be disturbed during the process of solving
the puzzle.
Given the number of disks of each size, compute the number of moves that the optimal solution takes.
Input
The input contains multiple test cases. Each test case consists of two lines. The first line contains two integers n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 106). The second lines contains n integers a1, a2,
…, an (1 ≤ a1,a2, …, an ≤ 105). For each 1 ≤ i ≤ n, there are ai disks of size i. The input ends where EOF is met.
Output
For each test case, print the answer modulo m on a separate line.
Sample Input
Sample Output
Source
PKU Campus 2008 (POJ Founder Monthly Contest – 2008.05.10), xfxyjwf
题意还是求汉诺塔的最少挪动次数,不同的是这次有相同大小的盘子了。
当第k大盘子只有一个的时候很好办,想象一下,相同大小的盘子当成一个盘子,放一次放倒了,经过这一次,正好放正回来了。
当第k大盘子有多个的时候,挪动的时候假设k-1个盘子在柱子上已经放好了,那么相同大小的盘子要顺序一致,必须腾出两个柱子来挪,这部分是2*val[k],之前的那k-1个盘子再都使用当成一个盘子的方法,颠一次倒一次,正好放正了。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define eps 1e-8
#define INF 1<<25
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
const int maxn = 1e5 + 5;
const ll mod = 1e9 + 7;
const double PI = acos(-1.0);
int n, m;
int val[maxn], cnt[maxn], dp[maxn];
void solve()
{
int i, j, k;
repp(i, 1, n)
{
sa(val[i]);
}
repp(i, 1, n)
{
cnt[i] = ((cnt[i - 1] << 1) + val[i]) % m;
}
dp[1] = 2 * val[1] - 1;
repp(i, 2, n)
{
if (val[i] == 1)
{
dp[i] = cnt[i];
}
else
{
dp[i] = (2 * cnt[i - 1] + 2 * val[i] + dp[i - 1]) % m;
}
}
printf("%d\n", dp
);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("i.txt", "r", stdin);
freopen("o.txt", "w", stdout);
#endif
while (scanf("%d%d", &n, &m) != EOF)
{
solve();
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 1903 | Accepted: 651 |
The Tower of Hanoi is a puzzle consisting of three pegs and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another peg, obeying the following rules:
Only one disk may be moved at a time.
Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg.
No disk may be placed on top of a smaller disk.
For n disks, it is a well-known result that the optimal solution takes 2n − 1 moves.
To complicate the puzzle a little, we allow multiple disks to be of the same size. Moreover, equisized disks are mutually distinguishable. Their ordering at the beginning should be preserved at the end, though it may be disturbed during the process of solving
the puzzle.
Given the number of disks of each size, compute the number of moves that the optimal solution takes.
Input
The input contains multiple test cases. Each test case consists of two lines. The first line contains two integers n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 106). The second lines contains n integers a1, a2,
…, an (1 ≤ a1,a2, …, an ≤ 105). For each 1 ≤ i ≤ n, there are ai disks of size i. The input ends where EOF is met.
Output
For each test case, print the answer modulo m on a separate line.
Sample Input
1 1000 2 5 1000 1 1 1 1 1 5 1000 2 2 2 2 2 5 1000 1 2 1 2 1
Sample Output
3 31 123 41
Source
PKU Campus 2008 (POJ Founder Monthly Contest – 2008.05.10), xfxyjwf
题意还是求汉诺塔的最少挪动次数,不同的是这次有相同大小的盘子了。
当第k大盘子只有一个的时候很好办,想象一下,相同大小的盘子当成一个盘子,放一次放倒了,经过这一次,正好放正回来了。
当第k大盘子有多个的时候,挪动的时候假设k-1个盘子在柱子上已经放好了,那么相同大小的盘子要顺序一致,必须腾出两个柱子来挪,这部分是2*val[k],之前的那k-1个盘子再都使用当成一个盘子的方法,颠一次倒一次,正好放正了。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define eps 1e-8
#define INF 1<<25
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
const int maxn = 1e5 + 5;
const ll mod = 1e9 + 7;
const double PI = acos(-1.0);
int n, m;
int val[maxn], cnt[maxn], dp[maxn];
void solve()
{
int i, j, k;
repp(i, 1, n)
{
sa(val[i]);
}
repp(i, 1, n)
{
cnt[i] = ((cnt[i - 1] << 1) + val[i]) % m;
}
dp[1] = 2 * val[1] - 1;
repp(i, 2, n)
{
if (val[i] == 1)
{
dp[i] = cnt[i];
}
else
{
dp[i] = (2 * cnt[i - 1] + 2 * val[i] + dp[i - 1]) % m;
}
}
printf("%d\n", dp
);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("i.txt", "r", stdin);
freopen("o.txt", "w", stdout);
#endif
while (scanf("%d%d", &n, &m) != EOF)
{
solve();
}
return 0;
}
相关文章推荐
- Oracle LOB
- [SDOI 2014] [JZOJ 3625] 旅行
- BZOJ3521: [Poi2014]Salad Bar
- 三步解决网络稳定加载图片:使用到谷歌开源的Volley
- Android 常见bug汇总
- 闭包回调的写法
- 手写控件布局
- nyoj 115 dijkstra
- RabbitMQ 学习资料
- saltstack之(八)配置管理部署LAMP
- Linux下TC使用说明
- 【Android】17、体验活动的生命周期(下)
- Android startservice和bindservice
- ES5 数组方法every和some
- poj1083 贪心
- 关于finalize()方法(总结)
- 【连载】关系型数据库是如何工作的?(12) - 查询管理器之Statistics
- thinkphp模板页判断变量为空的方法
- 异步post请求之Block方法
- 【Android 基础】如何查看数字签名信息