您的位置:首页 > 其它

POJ-1392 Ouroboros Snake

2014-09-12 14:07 423 查看
Ouroboros Snake

Time Limit: 1000MS Memory Limit: 65536K
   
Description

Ouroboros is a mythical snake from ancient Egypt. It has its tail in its mouth and continously devours itself. 

The Ouroboros numbers are binary numbers of 2^n bits that have the property of "generating" the whole set of numbers from 0 to 2^n - 1. The generation works as follows: given an Ouroboros number, we place its 2^n bits wrapped in a circle. Then, we can take
2^n groups of n bits starting each time with the next bit in the circle. Such circles are called Ouroboros circles for the number n. We will work only with the smallest 

Ouroboros number for each n. 

Example: for n = 2, there are only four Ouroboros numbers. These are 0011;0110;1100; and 1001. In this case, the smallest one is 0011. Here is the Ouroboros circle for 0011: 



The table describes the function o(n;k) which calculates the k-th number in the Ouroboros circle of the smallest Ouroboros number of size n. This function is what your program should compute. 

Input

The input consists of several test cases. For each test case, there will be a line containing two integers n and k (1<=n<=15; 0<=k<2^n). The end of the input file is indicated by a line containing two zeros. Don抰 process that line.
Output

For each test case, output o(n;k) on a line by itself.
Sample Input
2 0
2 1
2 2
2 3
0 0

Sample Output
0
1
3
2

————————————————————受伤的分割线————————————————————
前言:什么Fleury算法。。。无视!dfs解决一切。
思路:其实和POJ-1780 Code那道题思想一致。只不过这次是二进制数而已。而且不要求输出路径,要求输出路径上第k个数。边权搞定。依然是按照显式栈DFS解决,独立完成1A,小激动。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int M = 40000, N = 20000;
int n, k;
int tot, head
, path[M];
struct Node {
int v, w, next;
}edge[M];
int sta[M][2], pi;

void add(int u, int v, int w)
{
edge[tot].v = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
}

void Euler()
{
int top = -1;
sta[0][0] = 0; sta[0][1] = -1;
top++;
while(top != -1) {
int u = sta[top][0], w = sta[top][1], i;
for(i = head[u]; i != -1; i = edge[i].next) {
if(edge[i].v != -1) {
int v = edge[i].v;
edge[i].v = -1;
top++;
sta[top][0] = v;
sta[top][1] = edge[i].w;
break;
}
}
if(i == -1) {
if(w != -1) path[pi++] = w;
top--;
}
}
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
while(scanf("%d%d", &n, &k), n||k) {
tot = 0;
memset(head, -1, sizeof(head));
int m = 1<<n, base = 1<<(n-1);
for(int i = m-1; i >= 0; i--) {
int u = i >> 1, v = (~base) & i;
add(u, v, i);
}
pi = 0;
Euler();
printf("%d\n", path[pi-k-1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: