HDU 2579 Dating with girls (2) BFS

Source: Internet
Author: User
Tags printf
/*---------------------------------------------time:14:45-15:25 2012.2.13 stratege:bfs (keypoint: Defines a three-dimensional array, nu
                M[X][Y][Z]; X, y is used to store the current maze coordinates, Z is used to store the time traveled and the modulus of k in the topic.     Because x, y can go back and forth, so we need to open one-dimensional array, update the state Acmer:johnsonddu problem:2579 (Dating with Girls (2)) Judge status:accepted runid:5111908 language:c++ author:a312745658------------------------------------- ----------*/#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #incl

Ude <memory.h> #include <queue> #include <algorithm> using namespace std;
    #define INF 0xfffffff struct Node {int x, y;
int step;
}n, M;
int direct[4][2] = {1, 0, 0,-1,-1, 0, 0, 1};
Char map[105][105];
BOOL Flag;
int num[105][105][11];
int R, C, K;
int SX, SY, DX, dy;

int tcase;
    int main () {int I, j, K;
    Cin >> Tcase; while (Tcase-) {cin >> R &Gt;> C >> K; 
                for (i = 0; i < R; i + +) for (j = 0; J < C; J + +) {cin >> map[i][j];
                if (map[i][j] = = ' Y ') sx = i, sy = j;
            if (map[i][j] = = ' G ') dx = i, dy = j; 
                    } for (i = 0; i < R; i + +) for (j = 0; J < C; J + +) for (k = 0; k < one; k + +)     Num[i][j][k] = INF;

        The number of steps to initialize is maximum to update the status flag = FALSE;
        n.x = SX;
        N.Y = sy;

        N.step = 0;
        Queue <Node> Q;

        Q.push (n); while (!
            Q.empty ()) {m = Q.front ();

            Q.pop ();
                if (m.x = = DX && m.y = = dy) {flag = true;
            break;
            }//cout << "(" << m.x << "," << m.y << ")" << Endl;
   for (i = 0; i < 4; i + +)         {n.x = m.x + direct[i][0];
                N.Y = M.y + direct[i][1];

                N.step = M.step + 1;
                    if (n.x >= 0 && n.y >= 0 && n.x < R && N.Y < C) {
                    if (map[n.x][n.y] = = ' # ' && n.step% k! = 0)//if n.step% K = = 0, the stone will disappear, can walk continue;
                        if (Num[n.x][n.y][n.step%k] <= n.step)//Update step number.
                    Continue;
                    Num[n.x][n.y][n.step%k] = N.step;
                Q.push (n);
        }}} if (flag) printf ("%d\n", m.step);

    else printf ("Please give me another chance!\n");
} 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.