[codeforces] 877D. Olya and Energy Drinks (BFS)

[codeforces] 877D. Olya and Energy Drinks (BFS)


题目链接: D. Olya and Energy Drinks
题目大意: 给一个N * M的矩阵,给定起点和终点坐标,k代表沿同一个方向1s可以走1~k步, 问从起点走到终点最少需要多少秒,不能到达输出-1。 ‘#’表示墙, ‘.’表示路。

数据范围:
(1n,m,k1000) 。 起点终点合法并且位置可能重合。
解题思路:
大体思路是BFS+优化吧, 或者说就是裸BFS,T不T就看怎么写了。 首先BFS的时候同一个方向能入队就入队, 遇到#就终止当前方向。 我刚开始T了好几发, 最后把判终点在枚举的循环里面也写了一遍就过了 1800ms, 有些险。

AC代码:

 /**********************************************
 *Author*        :ZZZZone
 *Created Time*  : 2017/11/13 14:24:49
 *Ended  Time*  : 2017/11/13 15:31:06
*********************************************/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

const int MaxN = 1e3;
const int xx[] = {0, 0, 0, -1, 1};
const int yy[] = {0, 1, -1, 0, 0};

struct Point{ int x, y; };

struct NODE{
    Point a; int tim;
};

NODE q[MaxN * MaxN + 5];
int n, m, k, ans;
char a[MaxN + 5][MaxN + 5];
bool vis[MaxN + 5][MaxN + 5];
int dep[MaxN + 5][MaxN + 5];
Point sta, End;

bool check(Point p){
    if(p.x < 1 || p.y < 1 || p.x > n || p.y > m) return false;
    if(a[p.x][p.y] == '#') return false;
    return true;
}


void Bfs(){
    int head = 1, tail = 0;
    NODE temp;
    temp.a = sta;
    temp.tim = 0;
    q[++tail] = temp;
    vis[temp.a.x][temp.a.y] = true;
    dep[temp.a.x][temp.a.y] = 0;
    while(head <= tail && ans == -1){
        NODE now = q[head++];
        //printf("%d %d\n", now.a.x, now.a.y);
        if(now.a.x == End.x && now.a.y == End.y) {
            ans = now.tim;
            break;
        }
        for(int i = 1; i <= 4 && ans == -1; i++){
            NODE nxt;
            nxt.a.x = now.a.x, nxt.a.y = now.a.y;
            nxt.tim = now.tim + 1;
            int cnt = 0;
            while(true){
                cnt++;
                nxt.a.x += xx[i], nxt.a.y += yy[i];
                //printf("%d %d\n", nxt.a.x, nxt.a.y);
                if(cnt > k) break;
                if(!check(nxt.a)) break;
                if(nxt.a.x == End.x && nxt.a.y == End.y) {
                    ans = nxt.tim;
                    break; //之前T就是因为这个循环里没有判断到达终点。
                }
                if(nxt.tim < dep[nxt.a.x][nxt.a.y]){
                    dep[nxt.a.x][nxt.a.y] = nxt.tim;
                    if(!vis[nxt.a.x][nxt.a.y]){
                        vis[nxt.a.x][nxt.a.y] = true;
                        q[++tail] = nxt;
                    }
                }
            }
        }
        vis[now.a.x][now.a.y] = false;
    }
}

int main()
{
    while(~scanf("%d %d %d", &n, &m, &k)){
        for(int i = 1; i <= n; i++) scanf("%s", a[i] + 1);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++) 
                dep[i][j] = 1 << 30;
        scanf("%d %d", &sta.x, &sta.y);
        scanf("%d %d", &End.x, &End.y);
        ans = -1;
        Bfs();
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zzzzone/article/details/78525958