P1301 魔鬼之城 (bfs

#include <bits/stdc++.h>
//using int_max = 0x3f3f3f3f;
#define long_max 9223372036854775807;
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
using VI = vector<int>;
typedef unsigned long long ull;
const int MAXN = 1e6;
int n,m;
int a[1010][1010];
//y
//-1  0  1
//-1  0  1
//-1  0  1

//x
//-1 -1 -1
// 0  0  0
// 1  1  1

int dx[] = {0,1,1,0,-1,-1,-1,0,1};
int dy[] = {0,0,1,1,1,0,-1,-1,-1};
struct point{
    int x,y;
    int from;//判断是从哪个方向跳到当前这个点
    int time;
};
int vis[2010][2010][8];

int main() {
    cin>>n>>m;
    swap(n,m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
 /*
    for(int i=1;i<=n;i++) {
        for (int j = 1; j <= m; j++) {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }*/
    queue<point>q;
    point b = {1,1,0,0};
    q.push(b);
    while(q.size()){
        auto u = q.front();
        q.pop();
        //if(vis[u.x][u.y][u.from]) continue;
        int x = u.x;
        int y = u.y;
        if(x == n && y == m){
            cout<<u.time;
            return 0;
        }
        int from = u.from;
        int t = u.time;
        for(int i=1;i<=8;i++){
            if(i == from) continue;
            int xx = x + dx[i] * a[x][y];
            int yy = y + dy[i] * a[x][y];
            if(xx >= 1 && xx <= n && yy >= 1 && yy<= m && !vis[xx][yy][i]){
                vis[xx][yy][i] = 1;
                q.push({xx,yy,i,t+1});

            }
        }

    }

    cout<<"NEVER";
    return 0;


}

这题是n列m行,真无语,调半天

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值