2014 ACM/ICPC Asia Regional Beijing online HDU 5040 instrusive priority queue

Source: Internet
Author: User

Wa is a dead question.

A person goes from the starting point to the target point. There is a camera in this process. The camera's illumination range is two units of length, including the camera's own position. There are two options to avoid being exposed.

Wait 1 s in one location, or sit in the box (3 S) and take one step to spend 1 s. The camera turns clockwise every second.


1.4s has a loop, so vis [r] [C] [SEC] is the optimal solution in four situations.

2. No need to display the diagram. Each camera records four situations.

3. Use the priority queue to determine

Re, TLE, WA, AC .....

At the beginning, when each camera rotates, it does not determine whether the camera is in the range. It has been Re. At first, it does not take into account the current situation of the camera. There can also be three steps or waiting. At first, I thought that the camera location was not accessible, but it was actually possible .. Wa to death.

The path selected by BFS must be optimal (continuous expansion)


Notice: Determine whether the element of a state is in the range.


# Include <iostream> # include <stdio. h> # include <algorithm> # include <math. h> # include <string. h> # include <memory. h ># include <queue> # define max (a, B) (a)> (B )? (A) :( B) # define min (a, B) (a) <(B )? (A) :( B) # define PI ACOs (-1) # define INF 100000 using namespace STD; typedef _ int64 ll; const ll maxn = 10 + 500; using namespace STD; struct P {int row, Col; int step; // direct} St, Ed; struct CMP {bool operator () (struct p a, struct p B) {return. step> B. step ;}}; int n, m, t, q, Pos; int ans, TOT; int vis [maxn] [maxn] [4]; int done [maxn] [maxn] [4]; int map1 [maxn] [maxn]; int maps [4] [maxn] [maxn]; int turn [4] [2] = {0,-1}, {1 }, {},{-}}; Struct P monitor [maxn * maxn]; int monnum; int in (int x, int y) {If (x <0 | x> = n | Y <0 | Y> = N) return 0; return 1;} void change (INT sec, INT (* map1) [maxn]) {int I, K; struct p t; for (I = 0; I <monnum; I ++) {T = monitor [I]; k = (T. step + Sec) % 4; T. row + = turn [k] [1]; T. col + = turn [k] [0]; If (in (T. row, T. COL) & map1 [T. row] [T. col] = 0) map1 [T. row] [T. col] = 2;} // print (map1);} void BFS () {int I, j, k; int XX, YY; memset (VIS, 0x3f, siz EOF (VIS); // store the Shortest Path memset (done, 0, sizeof (VIS); // whether the file has been accessed, the shority_queue obtained from the priority queue must be the shororority_queue <struct P, vector <struct P>, CMP> q; struct p t, TT; q. push (ST); // vis [st. row] [st. col] [0] =-1; while (! Q. empty () {T = Q. top (); q. pop (); int sec = (T. step) % 4; If (done [T. row] [T. col] [SEC]) continue; done [T. row] [T. col] [SEC] = 1; for (I = 0; I <4; I ++) {xx = T. col + turn [I] [0]; YY = T. row + turn [I] [1]; if (XX <0 | YY <0 | XX> = n | YY> = n | maps [SEC] [YY] [XX] = 1) continue; TT. row = YY; TT. col = xx; int W = 1; if (maps [SEC] [YY] [XX] = 2 | maps [SEC] [T. row] [T. col] = 2) W = 3; if (vis [YY] [XX] [(SEC + W) % 4]> T. step + W) {TT. step = T. step + W; vis [YY] [XX] [(SEC + W) % 4] = T. step + W; q. push (TT) ;}} if (vis [T. row] [T. col] [(SEC + 1) % 4]> T. step + 1) {T. step ++; vis [T. row] [T. col] [(SEC + 1) % 4] = T. step; q. push (t); T. step -- ;}} ans = inf; for (I = 0; I <4; I ++) if (vis [ed. row] [ed. col] [I]) ans = min (ANS, vis [ed. row] [ed. col] [I]);} int main (INT argc, char * argv []) {# ifndef online_judgefreopen ("in.txt", "r", stdin); # endifint I, j, Len, CNT = 0; char STR [1, 1000]; scanf ("% d", & T); While (t --) {scanf ("% d ", & N); getchar (); monnum = 0; memset (map1, 0, sizeof (map1); for (I = 0; I <n; I ++) {gets (STR); Len = strlen (STR); For (j = 0; j <Len; j ++) {If (STR [J] = '#') map1 [I] [J] = 1; else if (STR [J] = 'M') {St. col = J; ST. row = I; ST. step = 0;} else if (STR [J] = 'T') {ed. col = J; ed. row = I; ed. step = 0;} else if (STR [J] = 'n') {map1 [I] [J] = 2; monitor [monnum]. col = J; monitor [monnum]. row = I; monitor [monnum]. step = 0; monnum ++;} else if (STR [J] = 'E') {map1 [I] [J] = 2; monitor [monnum]. col = J; monitor [monnum]. row = I; monitor [monnum]. step = 1; monnum ++;} else if (STR [J] = 's') {map1 [I] [J] = 2; monitor [monnum]. col = J; monitor [monnum]. row = I; monitor [monnum]. step = 2; monnum ++;} else if (STR [J] = 'W') {map1 [I] [J] = 2; monitor [monnum]. col = J; monitor [monnum]. row = I; monitor [monnum]. step = 3; monnum ++ ;}}for (I = 0; I <4; I ++) // display the diagram, very time spent {memcpy (maps [I], map1, sizeof (map1); change (I, maps [I]);} BFS (); if (ANS = inf) ans =-1; printf ("case # % d: % d \ n", ++ CNT, ANS);} return 0 ;}

Paste the god code of Sun Yat-sen University

#include <stdio.h>#include <string.h>#include <queue>using namespace std;const int MAXN = 510;int n;char s[MAXN][MAXN];int x0, y0;int d0[MAXN][MAXN];struct state {    int x, y, t, k;    state(int x, int y, int t, int k): x(x), y(y), t(t), k(k) {}};bool v[MAXN][MAXN][3];const int dx[] = {-1, 0, 1, 0};const int dy[] = {0, 1, 0, -1};void init(){    scanf("%d", &n);    for (int i = 0; i < n; ++i) {        scanf("%s", s[i]);        for (int j = 0; j < n; ++j) {            switch (s[i][j]) {                case 'N': d0[i][j] = 0; break;                case 'E': d0[i][j] = 1; break;                case 'S': d0[i][j] = 2; break;                case 'W': d0[i][j] = 3; break;                default: d0[i][j] = -1; break;            }            if (s[i][j] == 'M') x0 = i, y0 = j;        }    }}inline bool inrange(int x, int y){    return x >= 0 && x < n && y >= 0 && y < n;}bool capture(int x, int y, int t){    if (d0[x][y] >= 0) return true;    for (int i = 0; i < 4; ++i) {        int nx = x - dx[i], ny = y - dy[i];        if (inrange(nx, ny) && d0[nx][ny] >= 0) {            if ((d0[nx][ny] + t) % 4 == i) return true;        }    }    return false;}void push(queue<state> &q, int x, int y, int t, int k){    if (!v[x][y][k]) {        q.push(state(x, y, t, k));        v[x][y][k] = true;    }}int bfs(int x, int y){    memset(v, 0, sizeof(v));    queue<state> q;    push(q, x, y, 0, 0);    while (!q.empty()) {        int x = q.front().x;        int y = q.front().y;        int t = q.front().t;        int k = q.front().k;        // printf("%d %d %d %d\n", x, y, t, k);        q.pop();        if (k) {            if (k == 1 && s[x][y] == 'T') return t + 1;            push(q, x, y, t + 1, k - 1);            continue;        }        bool f = capture(x, y, t);        for (int i = 0; i < 4; ++i) {            int nx = x + dx[i], ny = y + dy[i];            if (!inrange(nx, ny) || s[nx][ny] == '#') continue;            if (f || capture(nx, ny, t)) {                for (int j = 1; j <= 2; ++j) {                    if (j == 2 || (!capture(x, y, t + j) && !capture(nx, ny, t + j))) {                        push(q, nx, ny, t + 1, j);                        break;                    }                }            } else {                if (s[nx][ny] == 'T') return t + 1;                push(q, nx, ny, t + 1, 0);            }        }    }    return -1;}int main(){    int dat;    scanf("%d", &dat);    for (int cas = 1; cas <= dat; ++cas) {        init();        printf("Case #%d: %d\n", cas, bfs(x0, y0));    }}


2014 ACM/ICPC Asia Regional Beijing online HDU 5040 instrusive priority queue

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.