Codeforces 877 D Olya and Energy Drinks

Title address
Title: Give you a map,'...'is the way to go. The # is the place where you can't walk, and then she can walk 1 to K grids per minute, telling you the starting point and the end point, and asking how many minutes it takes you to get from the beginning to the end point.
Thought: Simple extensive search, each time to traverse the direction of 1~k lattices, and then because it is extensive search, the time to reach this point is not equal to this time or greater. Then if you encounter a wall or go out, you will find a direction directly.

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#define N 1010
#define M 2000010//double
#define LL __int64
#define inf 0x3f3f3f3f3f3f3f3f
#define lson l,mid,ans<<1
#define rson mid+1,r,ans<<1|1
#define getMid (l+r)>>1
#define movel ans<<1
#define mover ans<<1|1
using namespace std;
const LL mod = 1000000007;
const double eps = 0.001;
char mapp[N][N];
bool vis[N][N];
int dir[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
int n, m, k;
int sx, sy, ex, ey;
struct node {
    int x, y;
    int len;
}now;
int bfs() {
    if (sx == ex&&sy == ey) {
        return 0;
    }
    queue<node> q;
    now.x = sx;
    now.y = sy;
    now.len = 0;
    q.push(now);
    memset(vis, 0, sizeof(vis));
    vis[sx][sy] = 1;
    while (!q.empty()) {
        now = q.front();
        q.pop();
        int x, y, xx, yy, len;
        x = now.x;
        y = now.y;
        len = now.len;
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= k; j++) {
                xx = x + dir[i][0] * j;
                yy = y + dir[i][1] * j;
                if (xx == ex&&yy == ey) {
                    return len + 1;
                }
                if (xx < 1 || xx > n || yy < 1 || yy > m || mapp[xx][yy] == '#') {
                    break;
                }
                if (vis[xx][yy] == 0) {
                    vis[xx][yy] = 1;
                    now.x = xx;
                    now.y = yy;
                    now.len = len + 1;
                    q.push(now);
                }
            }
        }
    }
    return -1;
}
int main() {
    cin.sync_with_stdio(false);
    while (cin >> n >> m >> k) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                cin >> mapp[i][j];
            }
        }
        cin >> sx >> sy >> ex >> ey;
        cout << bfs() << endl;
    }
    return 0;
}

Posted by plezops on Thu, 07 Feb 2019 13:42:19 -0800