IEEEXtreme 10.0 - Mancala'h
这是 meelo 原创的 IEEEXtreme极限编程大赛题解
Xtreme 10.0 - Mancala'h
题目来源 第10届IEEE极限编程大赛
https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mancalah
You and your friend Alessandro are taking part to an archaeological mission that aims to explore a newly discovered tomb of an ancient pharaoh in Egypt. After an adventurous trip through tunnels, doors and rooms, you and your fellow archaeologists arrive in front of a huge closed door and find a mysterious artefact that appears to be a sort of puzzle for opening the door. The artefact is composed of some inscriptions and a massive wooden disc with many bins carved at the perimeter. Some of the bins contain seeds.
On a side of the artefact, an inscription says: "Harvest all the seeds in the first bin. Sow them in the following bins, one-by-one. Rotate the disc by one step. Repeat."
On the other side of the artefact, a second inscription says: "How far you can go before starting to endless repeat the same harvesting pattern? How far can you go backwards?"
After a fellow archaeologist translated the inscriptions, your friend Alessandro exclaims: "I know this game: it’s Mancala’h! I know that from every possible configuration, the game evolves to a periodic status, meaning that at some point you start repeating the same pattern. For example, the configuration [1, 1, 1] evolves to [0, 2, 1] because you take the seed in the first bin and put it in the second bin. Then, after a clockwise rotation of the disc, the second bin with two seeds becomes the first and we can remove the leading zero from notation. So, the configuration is now [2, 1], which evolves to [2, 1] itself, because you take the two seeds in the first bin and put one in the second bin and one in the third bin, which was empty. In this case we can say that the depth of the original [1, 1, 1] configuration is equal to 1, meaning that in a single step we reach a periodic status."
"The situation can be more complex, for example the configuration [4] evolves to [1, 1, 1, 1], which in turn evolves to [2, 1, 1], which evolves to [2, 2], which evolves to [3, 1], which evolves again to [2, 1, 1], and so on. In this case the depth of the configuration [4] is 2 and the period is 3 steps long."
"The first thing we need to find is exactly the depth of the configuration we have found here."
"Going backwards in the game evolution is not as easy as going forward: for example, we have seen that configurations [1, 1, 1, 1] and [3, 1] evolve both to [2, 1, 1] in one step. On the contrary, the configuration [1, 2] cannot be the evolution of any valid configuration, because we exclude all the so-called not connectedconfigurations, which are those that contain a zero, such as [1, 0, 2]."
"The second thing we need to find, in fact, is the height of the given configuration, which is equal to the maximum number of backwards steps or, in other words, the distance (in terms of number of steps) of the farthest previous configuration. We cannot iterate backwards over the period, so the height is the length of the longest sequence of unique configurations that leads to the current configuration."
Help the archaeologists to solve the mystery by finding the depth D and the height H of given Mancala’h configurations.
Input Format
The input contains a single Mancala’h configuration. A Mancala’h configuration is defined by the sequence [N1, N2, N3, ..., NL] of L (1 ≤ L ≤ 100) integers separated by a blank-space. Each integer Ni (1 ≤ Ni ≤ 500) represents the number of seeds in the ith bin.
Constraints
The size of the Mancala'h board is large enough that you will never have a board in which all of the bins are filled. In other words, there are always more bins than seeds.
The number of unique configurations reachable from any input (either forwards or backwards) is at most 5 * 106.
Output Format
The output is a single line containing two integers D and H, separated by a blank-space. The first integer D is the depth of the Mancala’h configuration specified in the input. The second integer H is the height of the Mancala’h configuration specified in the input.
Sample Input
2 1 1
Sample Output
0 3
Explanation
The depth of the Mancala’h configuration [2, 1, 1] is 0 because the period includes the given configuration:
[2, 1, 1] → [2, 2] → [3, 1] → [2, 1, 1]
The height is 3 because there are three backwards evolutions, two of which contain 3 steps:
[2, 1, 1] ← [1, 1, 1, 1] ← [4] ← [1, 3]
[2, 1, 1] ← [3, 1] ← [2, 2] ← [1, 1, 2]
[2, 1, 1] ← [3, 1] ← [1, 2, 1]
Note that for the following is not a valid backwards evolution because it repeats the configuration [2, 1, 1]:
[2, 1, 1] ← [3, 1] ← [2, 2] ← [2, 1, 1]
As an additional example, suppose that the input was:
3 1 2 3 2
For this Mancala’h, the depth is 6:
[3, 1, 2, 3, 2] → [2, 3, 4, 2] →[4, 5, 2] →
[6, 3, 1, 1] → [4, 2, 2, 1, 1, 1] → [3, 3, 2, 2, 1] →
[4, 3, 3, 1] → [4, 4, 2, 1] → [5, 3, 2, 1] →
[4, 3, 2, 1, 1] → [4, 3, 2, 2] → [4, 3, 3, 1]
The height is 1 because the only previous configuration possible is:
[3, 1, 2, 3, 2] ← [1, 2, 1, 2, 3, 2]
题目解析
状态从前往后推只有一种可能性,但是可能存在循环,如何判断是否存在循环是一个关键的问题。事实上,只需要保存之前访问过的状态,线性遍历一遍,并不需要在常数时间内判断是否位于循环节内。
状态从后往前推可能有多种可能性,同样可能存在循环,判断是否存在循环的方法与从前往后推相同。存在多种可能性,那么状态会构成一棵树,需要进行深度优先搜索。
另一个关键问题是如何存储一个状态。状态从前往后推时,会去除第一个元素,需要访问中间的元素有可能在后面添加元素;状态从后往前推时,会添加一个元素,需要访问中间的元素,有可能需要删除末位的元素。
双端队列deque能够很好的满足这些要求,能够在常数时间在首尾添加或者删除元素,同时能在常数时间访问每一个元素。
程序
C++
#include <vector>
#include <iostream>
#include <algorithm>
#include <deque> using namespace std; // return: >=0 found, -1 not found
int find(const vector<deque<int> > &states, const deque<int> &state) {
for(int i=; i<states.size(); i++) {
if(states[i] == state) {
return i;
}
}
return -;
} int depth(deque<int> s) {
int index = ;
vector<deque<int> > states;
while(find(states, s) == -) {
states.push_back(s);
// get next state
int front = s.front();
s.pop_front();
int i, q_size = s.size();
for(i=; i<min(front, q_size); i++) {
s[i]++;
}
for(;i<front; i++) {
s.push_back();
}
} return find(states, s);
} // depth first search
int height(deque<int> &state, vector<deque<int> > &statesBefore) {
int max_depth = ;
for(int l=state.size()-; l>=; l--) {
deque<int> s(state); for(int i=l; i>=; i--) {
s[i]--;
} bool bad_state = false; // true: contain a zero
for(int i=; i<s.size()-; i++) {
if(s[i]== && s[i+]!=) {
bad_state = true;
break;
}
} if(!bad_state) {
s.push_front(l+);
// remove trailing zero
while(s.back() == ) {
s.pop_back();
}
if(find(statesBefore, s) != - ) break;
statesBefore.push_back(s);
max_depth = max(max_depth, height(s, statesBefore)+) ;
statesBefore.pop_back();
}
} return max_depth;
} int main() {
deque<int> state; int m;
while(cin >> m) {
state.push_back(m);
} vector<deque<int> > statesBefore;
statesBefore.push_back(state); cout << depth(state) << ' ' << height(state, statesBefore); return ;
}
博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址
IEEEXtreme 10.0 - Mancala'h的更多相关文章
- IEEEXtreme 10.0 - Counting Molecules
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Full Adder
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...
- IEEEXtreme 10.0 - Mysterious Maze
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...
- IEEEXtreme 10.0 - Inti Sets
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...
- IEEEXtreme 10.0 - Painter's Dilemma
这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...
- IEEEXtreme 10.0 - Ellipse Art
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...
- IEEEXtreme 10.0 - Checkers Challenge
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...
- IEEEXtreme 10.0 - Game of Stones
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...
- IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...
随机推荐
- HDU 4584 splay
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- poj2549 Sumsets
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11886 Accepted: 3273 Descript ...
- laravel调试神器tinker
一直以来,想调试框架中的某些东西,如想知道 Elpquent 的 create 方法返回值是个什么东西, 以前的话,应该就是在 create 方法调用之后,使用 dd 或者 var_dump 之类的函 ...
- ICPC 2018 南京网络赛 J Magical Girl Haze(多层图最短路)
传送门:https://nanti.jisuanke.com/t/A1958 题意:n个点m条边的路,你有k次机会将某条路上的边权变为0,问你最短路径长度 题解:最短路变形,我们需要在常规的最短路上多 ...
- IO多路复用之epoll(二)
前一篇介绍了epoll的LT模式,LT模式注意epollout事件在数据全部写成功后需要取消关注, 或者更改为EPOLLIN. 而这次epoll的ET模式,要注意的是在读和写的过程中要在循环中写完或者 ...
- 「Linux」centos7安装mysql
1.yum仓库下载MySQL:sudo yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch. ...
- 题解 P1682 【过家家】
P1682 过家家 题目描述 有2n个小学生来玩过家家游戏,其中有n个男生,编号为1到n,另外n个女生,编号也是1到n.每一个女生可以先选择一个和她不吵嘴的男生来玩,除此之外,如果编号为X的女生的朋友 ...
- 2015/10/9 Python基础(21):可调用和可执行对象
在Python中有多种运行外部程序的方法,比如,运行操作系统命令或另外的Python脚本,或执行一个磁盘上的文件,或通过网络来运行文件.这完全取决于想要干什么.特定的环境包括: 在当前脚本继续运行 创 ...
- Linux命令学习-图形化界面命令开关闭
su root password 1, 关闭图形界面: init 3 关闭图形界面(XServer服务也会关闭) 开启图形界面: init 5 或 startx 开机时,不进入 X Window: v ...
- ASP.Net Web 服务 – 如何使用会话状态
在上次博客帖子中,我们讨论了客户端对web服务的使用.在这篇文章中我们将复习一下如何使用web服务的会话状态. 这是上一篇文章的延续.因此请迅速的回顾之前的文章以便有一个清晰的概念. 在web服务中要 ...