Hackerrank :Gena Playing Hanoi
来源:互联网 发布:js 字符串函数大全 编辑:程序博客网 时间:2024/05/16 19:53
Gena Playing Hanoi
The Tower of Hanoi is a famous game consisting of
- Only one disc can be moved at a time.
- Each move consists of taking the topmost disc from a stack and moving it to the top of another stack.
- No disc may be placed on top of a smaller disc.
Gena has a modified version of the Tower of Hanoi. His Hanoi has
Note: Gena's rods are numbered from
Input Format
The first line contains a single integer,
The second line contains
Constraints
1≤N≤10
Output Format
Print the minimum number of moves Gena must make to restore the tower to its initial, ordered state on the first rod.
Sample Input
31 4 1
Sample Output
3
Explanation
这个题做法是深搜,没想到。最多10个盘子,4个塔,于是一个整数20位就可以表示当前状态,枚举移动的过程。
代码:
#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 INF 0x3fffffff#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))const int maxn = 1e3 + 5;const ll mod = 1000000;const double PI = acos(-1.0);int n;int d[1 << 20];queue<int>que;void solve(){int i, j, k;int mask = 0;sa(n);rep(i, 0, n){sa(k);k--;mask |= ((k) << 2 * i);}rep(i, 0, 1 << 20){d[i] = INF;}d[mask] = 0;que.push(mask);while (!que.empty()){k = que.front();que.pop();if (k == 0)break;vector<int>b[4];rep(i, 0, n){b[(k >> (2 * i)) & 3].push_back(i);}rep(i, 0, 4){b[i].push_back(INF);}rep(i, 0, 4){rep(j, 0, 4){if (b[i][0] >= b[j][0]){continue;}int p = b[i][0];int to = k ^ ((i^j) << (2 * p));if (d[to] < INF){continue;}d[to] = d[k] + 1;que.push(to);}}}printf("%d\n", d[0]);}int main(){solve();return 0;}
- Hackerrank :Gena Playing Hanoi
- hackerRank
- HackerRank
- HackerRank
- HackerRank
- HackerRank
- HackerRank
- HackerRank
- Hanoi
- Hanoi
- Hanoi
- Hanoi。。。。
- Hanoi
- hanoi
- Hanoi
- hanoi
- hanoi
- hanoi
- poj 3565(线段相交)
- tornado入门 - 模板
- Virtualbox-debian 8 调整屏幕分辨率
- hdu1846——Brave Game(巴什博奕)
- 单例
- Hackerrank :Gena Playing Hanoi
- 使用单片机的定时器0和中断实现9到0的倒计时功能,并用数码管显示
- Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalArgumentException: java.net.
- 39-Remove Duplicates from Sorted Array
- Hibernate实现数据的增删改查(CRUD)操作
- 【HUSTOJ】1000: 一个伟大的题
- 搜索数据库某列名
- Hdu 1372 Knight Moves
- 笔记3:FileWebRequest 与 FileWebResponse