Codeforces beta round #96 (Div. 2) (simulation)

Source: Internet
Author: User
Tags integer numbers
D. piettime limit per test

2 seconds

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

Piet is one of the most known visual esoteric programming languages. the programs in Piet are constructed from colorful blocks of pixels and interpreted using pretty complicated rules. in this problem we will use a subset of Piet language with simplified rules.

The program will be a rectangular image consisting of colored and black pixels. the color of each pixel will be given by an integer number between 0 and 9, aggressive, with 0 denoting black. A block of pixels is defined as a rectangle of pixels of the same color
(Not black). It is guaranteed that all connected groups of colored pixels of the same color will form rectangular blocks. Groups of black pixels can form arbitrary shapes.

The program is interpreted using movement of instruction pointer (IP) which consists of three parts:

<Ul = "">

  • Current block pointer (BP); note that there is no concept of current pixel within the block;

  • Direction pointer (DP) which can point left, right, up or down;

  • Block chooser (CP) which can point to the left or to the right from the ction given by DP; in absolute values CP can differ from DP by 90 degrees counterclockwise or clockwise, respectively.

    <P = "">

    Initially BP points to the block which contains the top-left corner of the program, DP points to the right, and CP points to the left (see the Orange square on the image below ).

    One step of program interpretation changes the state of IP in a following way. the interpreter finds the furthest edge of the current color block in the direction of the DP. from all pixels that form this edge, the interpreter selects the furthest one in
    Direction of CP. after this, BP attempts to move from this Pixel into the next one in the direction of DP. if the next pixel belongs to a colored block, this block becomes the current one, and
    Two other parts of IP stay the same. it the next pixel is black or outside of the program, BP stays the same but two other parts of IP change. if CP was pointing to the left, now it points to the right, and DP stays the same. if CP was pointing to the right,
    Now it points to the left, and DP is rotated 90 degrees clockwise.

    This way BP will never point to a black block (it is guaranteed that top-left pixel of the program will not be black ).

    You are given a Piet program. You have to figure out which block of the program will be current afterNSteps.

  • <P = ""> <p = "">

    Input

    <P = "">

    The first line of the input contains two integer numbersM(1 digit ≤ DigitMLimit ≤ limit 50)
    AndN(1 digit ≤ DigitNLimit ≤ limit 5 · 107 ).
    NextMLines contain the rows of the program. All the lines have the same length between 1 and 50 pixels, and consist of characters 0-9.
    The first character of the first line will not be equal to 0.

    <P = ""> <p = "">

    Output

    <P = "">

    Output the color of the block which will be current afterNSteps of program interpretation.

    <P = ""> <p = "">

    Sample test (s)

    <P = ""> <p = "">

    Input
    2 101243
    Output
    1
    Input
    3 12142366246625
    Output
    6
    Input
    5 91034523456345674567856789
    Output
    5

    <P = ""> <p = "">

    Note

    <P = "">

    In the first example IP changes in the following way. after step 1 block 2 becomes current one and stays it after two more steps. after step 4 BP moves to block 3, after Step 7-to block 4, and finally after Step 10 bp returns to block 1.

    The sequence of states of IP is shown on the image: The arrows are traversed clockwise, the main arrow shows ction of DP, the side one-the direction of CP.

    #include<cstring>#include<iostream>using namespace std;#define N 55int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};int vis[N][N][5][5];int map[N][N];int m, n, l;struct Item{    int dp,cp,r,c,tr,tc;    void next()    {        while(1)        {            tr = r + dir[dp][0];            tc = c + dir[dp][1];            if ( check() ) break;            r = tr; c = tc;            }            int t = dp;            dp = cp == 0 ? (dp + 3)%4 : (dp+1)%4;            while(1)            {                tr = r + dir[dp][0];                tc = c + dir[dp][1];                if ( check() ) break;                r = tr; c = tc;            }            dp = t;            tr = r + dir[dp][0];            tc = c + dir[dp][1];            if(tr < 0 || tr >= m || tc < 0 || tc >= l || map[tr][tc] == 0 )            {                if( cp == 0 ) cp = 1;                else { cp = 0; dp = ( dp + 1 ) % 4; }            }            else { r = tr; c = tc; }        }    bool check ()    {        return (tr < 0 || tr >= m || tc < 0 || tc >= l || map[tr][tc] != map[r][c]);    }    int visId ()    {        return vis[r][c][dp][cp];    }    void setVis( int id )    {        vis[r][c][dp][cp] = id;    }};int main(){    char tmp[N];    cin >> m >> n;    for ( int i = 0; i < m; i++ )    {        cin >> tmp;        if (i==0) l = strlen(tmp);        for ( int j = 0; j < l; j++ )            map[i][j] = tmp[j] - '0';    }    Item step; int id;    step.cp = step.dp = step.r = step.c = id = 0;    memset(vis,-1,sizeof(vis));    step.setVis(id++);    while ( n-- )    {        step.next();        if( step.visId() == -1 ) { step.setVis(id++); continue; }        n = n % ( id - step.visId() );  break;    }    n = n < 0 ? 0 : n;    while ( n-- ) step.next();    cout << map[step.r][step.c] << endl;    return 0;}

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.