Codeforces 877 problem D Olya and energy drinks BFS pruning

Source: Internet
Author: User

Transmission Door
Test instructions: Is given a starting point and an end point, each can go to four directions up to the K-step, each walk spent a second, asked to go to the end of the minimum number of seconds required.
Idea: Direct BFS will definitely t, we need to add a few pruning, one is when we go to some point, the point has been reached, and at this time than from the current point of time to go to this point is shorter, then do not go, because the next point from this point to a better point. See the code for specifics.

AC Code

const int MAXN = 1E3+5;
int Cas=1;
Char MAPP[MAXN][MAXN];
int VIS[MAXN][MAXN],DIS[MAXN][MAXN];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
    void Solve () {int n,m,k;
    int Sx,sy,ex,ey;
        while (~SCANF ("%d%d%d", &n,&m,&k)) {for (int i=1;i<=n;i++) {scanf ("%s", mapp[i]+1);
        } scanf ("%d%d%d%d", &sx,&sy,&ex,&ey); Fill (vis,0);
        Fill (Dis,inf);
        queue<pair<int,int>>q;
        Q.push (Make_pair (Sx,sy));
        Vis[sx][sy] = 1;
        Dis[sx][sy] = 0;
            while (!q.empty ()) {pair<int,int>tmp = Q.front ();
            Q.pop ();
                    for (int i=0;i<4;i++) {for (int j=1;j<=k;j++) {int xx = tmp.fi+dx[i]*j;
                    int yy = Tmp.se+dy[i]*j;
                    if (xx<1 | | xx>n | | yy<1 | | yy>m) break;
                    if (mapp[xx][yy] = = ' # ') break; if (Dis[xx][yy]!=inf && Dis[xx][yy] < DIs[tmp.fi][tmp.se] + 1) break;
                    if (Vis[xx][yy]) continue;
                    VIS[XX][YY] = 1;
                    DIS[XX][YY] = dis[tmp.fi][tmp.se] + 1;
                Q.push (Make_pair (XX,YY));
        }}} if (Dis[ex][ey]!=inf) cout << Dis[ex][ey] << Endl;
    else cout << "-1" << Endl; }
}

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.

Tags Index: