HDU 2579/bfs/dating with girls (2)

Source: Internet
Author: User

Topic links

/* Test instructions is a traditional maze plus a condition that the walls will disappear at the full multiples of k, so the shortest time to reach the exit is obtained.
The key point is that a point is taken up to K times, labeled Vis[x][y][time%k]. */#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <
Algorithm> using namespace std;
const int maxn=100+5;
int vis[maxn][maxn][15];
int dir[4][2]={0,1,1,0,0,-1,-1,0};
Char MAZE[MAXN][MAXN];
int Sx,sy,ex,ey;
int n,m,k;
    void Init () {memset (vis,0,sizeof (Vis));} struct node {int x;
    int y;
int time;
};
    int BFS () {node now;
    NOW.X=SX;
    Now.y=sy;
    now.time=0;
    queue<node>que;
    Que.push (now);
        while (!que.empty ()) {Now=que.front ();
        Que.pop ();
        if (Now.x==ex&&now.y==ey) return now.time;
            for (int i=0;i<4;i++) {int x=now.x+dir[i][0];
            int y=now.y+dir[i][1];
            int t=now.time+1;
            int mod=t%k; if (x>=1&&x<=n&&y>=1&&y<=m) {if (maze[x]
                    [y]!= ' # ' &&vis[x][y][mod]==0) {vis[x][y][mod]=t;
                    Node next= (node) {x,y,t};
                Que.push (next);
                    } else if (maze[x][y]== ' # ' &&mod==0&&vis[x][y][mod]==0) {
                    vis[x][y][mod]=t;
                    Node next= (node) {x,y,t};
                Que.push (next);
}}}} return-1;
    } int main () {int t;scanf ("%d", &t);
        while (t--) {scanf ("%d%d%d", &n,&m,&k);
        Init ();
            for (int i=1;i<=n;i++) {scanf ("%s", maze[i]+1);
                for (int j=1;j<=m;j++) if (maze[i][j]== ' Y ') sx=i,sy=j;
        else if (maze[i][j]== ' G ') ex=i,ey=j;
        } int Ans=bfs ();
        if (ans==-1) printf ("Please give me another chance!\n"); else PrinTF ("%d\n", ans);
} 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.