Andrew Stankevich Contest 4 -- Driving Straight, stankevichcontest

Source: Internet
Author: User

Andrew Stankevich Contest 4 -- Driving Straight, stankevichcontest

Question connection

  • Question:
    Enter m and n, followed by M-1 rows and 2 * n-1 columns. ''Indicates that it cannot pass, '+' can pass, '-', '|' only allows one direction to pass (in the same direction as the symbol ), calculate the shortest path from the lower left to the upper right corner and output the result: when outputting the result, try to maintain the previous motion direction. At a plus point, the current motion operation is output: R, right turn; L, left turn; F, straight. Output at the beginning: N, go north; E go east
  • Analysis:
    The simplest and most short-circuited path is a little effort in output path. Start bfs to find the shortest path in the upper right corner, and start to find the path in the lower left corner.
const int INF = 0x3f3f3f3f;const double EPS = 1e-9;const int MOD = 1000000007;const double PI = acos(-1.0);const int MAXN = 900;char ipt[MAXN][MAXN];int dp[MAXN][MAXN];int n, m;int dx[] = {-1, 0, 1, 0};int dy[] = {0, 1, 0, -1};char judge[] = {'|', '-', '|', '-'};struct Node{    int x, y;    Node(int x = 0, int y = 0) : x(x), y(y) {}} t;bool check(int r, int c, int dir){    return ipt[r][c] == '+' || ipt[r][c] == judge[dir];}void bfs(int sx, int sy){    queue<Node> q;    q.push(Node(sx, sy));    dp[sx][sy] = 0;    while (!q.empty())    {        t = q.front(); q.pop();        REP(i, 4)        {            if (check(t.x, t.y, i))            {                int tx = t.x + dx[i];                int ty = t.y + dy[i];                if (dp[tx][ty] == INF && ipt[tx][ty] != ' ' && ipt[tx][ty] != 0 && check(tx, ty, i))                {                    dp[tx][ty] = dp[t.x][t.y] + 1;                    q.push(Node(tx, ty));                }            }        }    }}void display(int x, int y, int dir){    if (x == 1 && y == m)    {        return;    }    int ldir = (dir + 3) % 4, rdir = (dir + 1) % 4;    int tx, ty;    tx = x + dx[dir]; ty = y + dy[dir];    if (ipt[tx][ty] != 0 && dp[tx][ty] == dp[x][y] - 1)    {        if (ipt[x][y] == '+')            putchar('F');        display(tx, ty, dir);        return;    }    tx = x + dx[ldir]; ty = y + dy[ldir];    if (ipt[tx][ty] != 0 && dp[tx][ty] == dp[x][y] - 1)    {        if (ipt[x][y] == '+')            putchar('L');        display(tx, ty, ldir);        return;    }    tx = x + dx[rdir]; ty = y + dy[rdir];    if (ipt[tx][ty] != 0 && dp[tx][ty] == dp[x][y] - 1)    {        if (ipt[x][y] == '+')            putchar('R');        display(tx, ty, rdir);        return;    }}int main(){    freopen("straight.in", "r", stdin);    freopen("straight.out", "w", stdout);    while (~RII(n, m))    {        n = n * 2 - 1; m = m * 2 - 1;        getchar();        CLR(dp, INF);        CLR(ipt, 0);        FE(i, 1, n)            gets(ipt[i] + 1);        bfs(1, m);        REP(i, 2)        {            int tx = n + dx[i];            int ty = 1 + dy[i];            if (ipt[tx][ty] != 0 && check(n, 1, i) && check(tx, ty, i) && dp[tx][ty] == dp[n][1] - 1)            {                puts(i ? "E" : "N");                display(tx, ty, i);                break;            }        }        puts("");    }    return 0;}

Test data:
2 2+--|||+++2 2+--|+2 2+--+-+--+2 2+++++++++2 2+++-+++++4 4+-+ +-+| |   |+ +-+-+|   -+-+-+-+|     |+-+-+-+






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.