Codeforces 877 D Olya and Energy Drinks

来源:互联网 发布:投影幕布 白墙 知乎 编辑:程序博客网 时间:2024/05/17 23:13

题目地址
题意:给你一个地图,‘.’是可以走的路。‘#’是不能走的地方,然后她每分钟能走1~k个格子,告诉你起点和终点,问你要多少分钟从起点到终点。
思路:就是简单的广搜,每次都要遍历下这个方向的1~k个格子,然后因为是广搜,之后到这个点的耗时要不是等于这个时间要不就更大。然后如果遇到了墙或者出去了就直接找过一个方向。

#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//双倍#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|1using 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;}
原创粉丝点击