CodeForces877D【BFS】

思路:
BFS本身已经决定先搜到的位置肯定小于等于后搜到的位置层数(步数)。
那就直接break呗~
然后就 一直 wa49…
期间还去搜了搜题解(难道思路错了???)。。。好像没有啊。。。然后有人标记方向。。。真是。。。完全不关方向什么事啊,怎么可能重复方向。。然后还有人写了好几个标记,难以直视就继续改自己的破代码吧。。。完全不知道发生了什么,突然改改就过了。。
想了想应该是队列相同状态进多次了。。然后跑了46ms,返回时间差不多花了5分钟,心都碎了!!!碎觉碎觉。。

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define lson num<<1, Left, Mid
#define rson num<<1|1, Mid+1, Right
#define mem(a, b) memset(a, b, sizeof(a))
typedef pair<int,int> PII;

const double pi = acos(-1.0);
const double eps = 1e-6;

const int Maxn = 1e3 + 7;
struct asd{
    int x, y;
};

int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int step[Maxn][Maxn];
char ma[Maxn][Maxn];
int n, m, k;
int sx, sy, ex, ey;

queue<asd>q;
void solve(){
    int x, y;
    memset(step, -1, sizeof(step));
    asd now, nex;
    now.x = sx;
    now.y = sy;
    step[sx][sy] = 0;
    q.push(now);
    while(!q.empty()){
        now = q.front(); q.pop();
        if(now.x == ex && now.y == ey){
            printf("%d\n", step[ex][ey]);
            return;
        }
        for(int i=0;i<4;i++){
            int j=1;
            while(j<=k){
                x = now.x + dx[i]*j;
                y = now.y + dy[i]*j;
                j++;
                if(x<0||y<0||x>=n||y>=m) break;
                if(ma[x][y] == '#') break;
                if((step[x][y] != -1) && (step[x][y] < step[now.x][now.y]+1)) break;
                if(step[x][y] == -1){
                    step[x][y] = step[now.x][now.y] + 1;
                    nex.x = x, nex.y = y;
                    q.push(nex);
                }
            }
        }
    }
    printf("-1");
}

int main(){
    scanf("%d%d%d", &n, &m, &k);
    for(int i=0;i<n;i++)
        scanf("%s", ma[i]);
    scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
    sx--;
    sy--;
    ex--;
    ey--;

    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/keyboarderqq/article/details/78671493