Sicily 7143. Treasure

7143. Treasure

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

One day, MXWai found a treasure map. The map was
divided into 2^n * 2^n grids. The top left corner was (0, 0) and the down right corner was (2^n-1, 2^n-1). The grids and their coordinates are displayed below.


(0, 0)


(0, 1)


……


(0, 2^n-1)


……


……


……


……


(2^n-2, 0)


(2^n-2,1)


……


(2^n-2, 2^n-1)


(2^n-1, 0)


(2^n-1,1)


……


(2^n-1, 2^n-1)

Moreover, she found a secret sequence of length
n, and a secret article saying that

"Repeat
these operations n times then you will find the treasure.

1、divide
the map into four 2^(n-1) * 2^(n-1) parts: the top left part labeled 0, the top right part labeled 1, the down left part labeled 2 and the down right part labeled 3

2、select
the corresponding part according to the first digit of the sequence

3、erase
the first digit of the sequence

4、decrease
n by one

finally
you will find out where the treasure is"

You know MXWai is just a majia, so you know she
is stupid. Now it is your task to find out the location of the treasure.

Input

An integer T (1<=T<=1000000), indicating the number
of test cases.

For each test case, there is only one line, containing
an integer n (1<=n<=31), and a sequence of length n (the sequence only contains digits ‘0’, ‘1’, ‘2’ and ’3’).

Output

Output two integers, indicating the coordinates
of the treasure.

Sample Input

3
1 3
2 00
2 01

Sample Output

1 1
0 0
0 1

Hint

This problem has huge input data, use scanf() instead
of cin to read data to avoid time limit exceed.

Problem Source

“星海通杯”第四届中山大学ICPC新手赛 by 黄文浩

这道题其实就是无限划分。。

#include <string.h>
#include <vector>
#include <queue>
#include <stdio.h>
#include <stack>
#include <math.h>
using namespace std;

int main() {
    int t, n;
    scanf("%d", &t);
    while (t--) {
        char dir[35] = {0};
        long long i_min = 0, j_min = 0, i_max, j_max;
        scanf("%d %s", &n, dir);
        i_max = (long long)pow(2, n) - 1;
        j_max = (long long)pow(2, n) - 1;
        for (int i = 0; i < n; i++) {
            if (dir[i] == '0') {
                i_max = i_min + (i_max - i_min) / 2;
                j_max = j_min + (j_max - j_min) / 2;
            } else if (dir[i] == '1') {
                i_max = i_min + (i_max - i_min) / 2;
                j_min = j_max - (j_max - j_min) / 2;
            } else if (dir[i] == '2') {
                i_min = i_max - (i_max - i_min) / 2;
                j_max = j_min + (j_max - j_min) / 2;
            } else {
                i_min = i_max - (i_max - i_min) / 2;
                j_min = j_max - (j_max - j_min) / 2;
            }
        }
        printf("%lld %lld\n", i_min, j_min);
    }
    return 0;
}                                 
时间: 2024-11-25 20:15:18

Sicily 7143. Treasure的相关文章

UVa 587 - There&#39;s treasure everywhere!

题目:你开始在坐标原点,想去目的地,给你一系列的指路信息,问目的地的位置和到原点的距离. 分析:模拟,计算几何.直接按照顺序计算即可,利用相对坐标求绝对坐标. 说明:注意输入格式. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespac

POJ2594 Treasure Exploration[DAG的最小可相交路径覆盖]

Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8301   Accepted: 3402 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored

Gym 100971A Treasure Island BFS 思维题

A - Treasure Island Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description standard input/output Announcement Statements Pirate John Silver has found a map depicting exactly one island in a sea. The ma

poj 2594 Treasure Exploration 二分图匹配

点击打开链接题目链接 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7215   Accepted: 2947 Description Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you eve

There&#39;s Treasure Everywhere! poj1473题目解析

题目: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 总时间限制:  1000ms 内存限制:  65536kB 描述 Finding buried treasures is simple: all you need is a map! The pirates in the Caribbean were famous for their enormous buried treasures and their elaborate maps

HDU2061 Treasure the new start, freshmen!【水题】

Treasure the new start, freshmen! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11239    Accepted Submission(s): 3502 Problem Description background: A new semester comes , and the HDU also m

Sicily 1146:Lenny&#39;s Lucky Lotto(dp)

题意:给出N,M,问有多少个长度为N的整数序列,满足所有数都在[1,M]内,并且每一个数至少是前一个数的两倍.例如给出N=4, M=10, 则有4个长度为4的整数序列满足条件: [1, 2, 4, 8], [1, 2, 4, 9], [1, 2, 4, 10], [1, 2, 5, 10] 分析:可用动态规划解题,假设dp[i][j],代表满足以整数i为尾数,长度为j的序列的个数(其中每一个数至少是前一个数的两倍).那么对于整数i,dp[i][j] 等于所有dp[k][j-1]的和,其中k满足:

bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱*

bzoj2101[Usaco2010 Dec]Treasure Chest 藏宝箱 题意: 给个序列,A与B轮流取数,谁取的数总和大谁赢.每次只能取序列两端,问A能取的数总和最大是多少.假设两人都用最优策略.序列大小≤5000 题解: dp.f[i][j][0]=max(f[i+1][j][1]+a[i],f[i][j-1][1]+a[j]),f[i][j][1]=min(f[i+1][j][0],f[i][j-1][0]). 代码: 1 #include <cstdio> 2 #includ

ZOJ 3209 Treasure Map(舞蹈链)

题目链接:[kuangbin带你飞]专题三 Dancing Links B - Treasure Map 题意 给一矩形和k个小矩形,问选取最小数量为多少的小矩形可以对大矩形进行精确覆盖. 思路 仍然是个模版题,把二维的n*m的大矩形看作是一维的n*m的一条线.k个小矩形同理,那么就转化成01矩阵精确覆盖的问题了. 代码 #include <iostream> #include <algorithm> #include <cstring> #include <cs