码迷,mamicode.com
首页 > 其他好文 > 详细

upc 9315 Philosopher’s Walk

时间:2018-10-04 22:05:21      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:col   show   scan   counter   making   algo   src   inpu   isp   

Philosopher’s Walk

时间限制: 1 Sec  内存限制: 128 MB
提交: 89  解决: 45
[提交] [状态] [讨论版] [命题人:admin]

题目描述

In Programming Land, there are several pathways called Philosopher’s Walks for philosophers to have a rest. A Philosopher’s Walk is a pathway in a square-shaped region with plenty of woods. The woods are helpful for philosophers to think, but they planted so densely like a maze that they lost their ways in the maze of woods of a Philosopher’s Walk.

Fortunately, the structures of all Philosopher’s Walks are similar; the structure of a Philosopher’s Walk is designed and constructed according to the same rule in a 2k meter square. The rule for designing the pathway is to take a right-turn in 90 degrees after every 1-meter step when k is 1, and the bigger one for which the integer k is greater than 1 is built up using four sub-pathways with k - 1 in a fractal style. Figure F.1 shows three Philosopher’s Walks for which k is 1, 2, and 3. The Philosopher’s Walk W2 consists of four W1 structures with the lower-left and the lower-right ones are 90 degree rotated clockwise and counter-clockwise, respectively; the upper ones have the same structure with W1. The same is true for any Wk for which the integer k is greater than 1. This rule has been devised by a mathematical philosopher David Hilbert (1862 – 1943), and the resulting pathway is usually called a HILBERT CURVE named after him. He once talked about a space filling method using this kind of curve to fill up a square with 2k sides, and every Philosophers’ Walk is designed according to this method.

技术分享图片

Tae-Cheon is in charge of the rescue of the philosophers lost in Philosopher’s Walks using a hot air balloon. Fortunately, every lost philosopher can report Tae-Cheon the number of meter steps he has taken, and Tae-Cheon knows the length of a side of the square of the Philosopher’s Walk. He has to identify the location of the lost philosopher, the (x,y) coordinates assuming that the Philosopher’s Walk is placed in the 1st quadrant of a Cartesian plain with one meter unit length. Assume that the coordinate of the lower-left corner block is (1,1). The entrance of a Philosopher’s Walk is always at (1,1) and the exit is always (n,1) where n is the length of a side. Also assume that the philosopher have walked one meter step when he is in the entrance, and that he always go forward to the exit without back steps.

For example, if the number of meter-steps taken by a lost philosopher in the Philosopher’s Walk in W2 in Figure F.1(b) is 10, your program should report (3,4).

Your mission is to write a program to help Tae-Cheon by making a program reporting the location of the lost philosopher given the number of meter-steps he has taken and the length of a side of the square of the Philosopher’s Walk. Hurry! A philosopher urgently needs your help.

 

输入

Your program is to read from standard input. The input consists of a single line containing two positive integers, n and m, representing the length of a side of the square of the Philosopher’s Walk and the number of meter-steps taken by the lost philosopher, respectively, where n = 2k and m ≤ 22k for an integer k satisfying 0 < k ≤ 15.

 

输出

Your program is to write to standard output. The single output line should contain two integers, x and y, separated by a space, where (x,y) is the location of the lost philosopher in the given Philosopher’s Walk.

 

样例输入

4 10

 

样例输出

3 4

题意

直接看三个图就可以了,每个图中每个点从左下角按顺序编号,三个图分别为1、2、3级,对于每一级求第n个编号的点的坐标。

分析

分形图,用递归实现就行。每个图都可以分成四个部分,左上和右上是前一级的复制,左下是顺时针转90度,右下是逆时针转90度。定义编号0只有一个点就是(0,0)点。每一级的(0,0)点,下一级就变成了(y,x)点。每一次升级,长度都会扩展2^(n-1)。

技术分享图片
///  author:Kissheart  ///
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c==-) f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-0;return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
pair<ll,ll>Fractal(ll n,ll m)
{
    if(n==0)
        return make_pair(0,0);

    ll len=1<<(n-1),cnt=1<<(2*n-2);

    pair<ll,ll>p=Fractal(n-1,m%cnt);

    ll x=p.first,y=p.second;
    ll indx=m/cnt;
    
    if(indx==0) return make_pair(y,x);
    if(indx==1) return make_pair(x,y+len);
    if(indx==2) return make_pair(x+len,y+len);
    if(indx==3) return make_pair(2*len-y-1,len-x-1);
}
ll f[30];
int main()
{
    ll n,k;
    f[0]=1;
    for(int i=1;i<=30;i++)
        f[i]=f[i-1]*2;

    scanf("%lld%lld",&n,&k)
    
    pair<ll,ll>p;
    ll o;
    
    for(int i=0;i<=30;i++)
    {
        if(f[i]==n)
            o=i;
    }
    p=Fractal(o,k-1);
    printf("%lld %lld\n",p.first+1,p.second+1);
    
    return 0;
}
View Code

 

upc 9315 Philosopher’s Walk

标签:col   show   scan   counter   making   algo   src   inpu   isp   

原文地址:https://www.cnblogs.com/Kissheart/p/9743534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!