#1答案对,但是wa,关o2后全re

P1746 离开中山路

ridbor001 @ 2024-03-18 22:49:13

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<algorithm>
#include<cstring>
#include<queue>

using namespace std;

typedef pair<int, int> PII;
queue<PII>q;

int n;
int st[1010][1010];
int arr[1010][1010];
int start[2];
int end1[2];

int bfs(int x1, int y1) {
    q.push({ x1,y1 });
    int x[] = { 0,1,0,-1 };
    int y[] = { 1,0,-1,0 };
    while (!q.empty()) {
        PII head = q.front();
        for (int i = 0; i < 4; i++) {
            int x_now = head.first + x[i];
            int y_now = head.second + y[i];
            if (x_now < 1 || y_now < 1 || x_now > n || y_now > n)
                continue;
            if (arr[x_now][y_now])
                continue;
            if (st[x_now][y_now])
                continue;
            q.push({ x_now,y_now });
            st[x_now][y_now] = st[head.first][head.second] + 1;
            if (x_now == end1[0] && y_now == end1[1]) {
                return st[end1[0]][end1[1]];
            }
        }
        q.pop();
    }
    return st[end1[0]][end1[1]];
}

int main() {
    cin >> n;
    cin.ignore(); // 忽略换行符

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            char temp;
            cin.get(temp);
            arr[i][j] = temp - '0';
        }
        cin.ignore(); // 忽略换行符
    }
    scanf("%d%d%d%d", start, start + 1, end1, end1 + 1);
    st[start[0]][start[1]] = 0;
    int ans = bfs(start[0], start[1]);

    printf("%d", ans);

}

为啥啊..


by caican @ 2024-03-19 13:09:46

re,输入方式换一下, wa,记忆化


by caican @ 2024-03-19 13:12:00

cin>>n; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { char c; cin>>c; arr[i][j]=c-'0'; }

}

by ridbor001 @ 2024-03-19 21:04:05

@caican 我测,大佬!换了就对了..为什么啊这是..


|