社区讨论

MLE?

P1746离开中山路参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lywe9xbl
此快照首次捕获于
2024/07/22 10:55
2 年前
此快照最后确认于
2024/07/22 11:46
2 年前
查看原帖
#2过了,其他的都是MLE
CPP
#include <bits/stdc++.h>
#define mian main
using namespace std;

int n,xx,yy,xxx,yyy;
int a[2005][2005];
int vis[2005][2005];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

struct Node {
    int x,y,step;
};

void read() {
    cin >> n;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
        	char c;
        	cin >> c;
            a[i][j] = c - '0';
        }
    }
    cin >> xx >> yy >> xxx >> yyy;
}

void bfs() {
    queue<Node> q;
    int x1 = xx,y1 = yy,x2 = xxx,y2 = yyy;
    q.push({x1,y1,0});
    int ans = 1e9;
    while(!q.empty()) {
        Node nw = q.front();
        q.pop();
        int x = nw.x,y = nw.y,step = nw.step;
        vis[x][y] = 1;
        if(x == x2 && y == y2) ans = min(ans,step);
        for(int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx < 1 || nx > n || ny < 1 || ny > n || vis[nx][ny] == 1 || a[nx][ny] == 1) continue;
            q.push({nx,ny,step+1});
        }
    }
    cout << ans;
}

int mian() {
    read();
    bfs();
    return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...