BFS,70求助

P1434 [SHOI2002] 滑雪

KANO07 @ 2023-11-24 08:03:41

#include<bits/stdc++.h>
using namespace std;
int len = 0;
int row,col;
int mp[101][101];
int dirx[4] = {0,0,1,-1};
int diry[4] = {1,-1,0,0};

void bfs(int x, int y){
    int start = mp[x][y];
    int end = 0;
    queue<pair<int,int>>que;
    que.push({x,y});
    while(!que.empty()){
        pair<int,int>tmp = que.front();
        que.pop();
        x = tmp.first;
        y = tmp.second;
        for(int i = 0; i < 4; i++){
            int nx = x + dirx[i];
            int ny = y + diry[i];
            // 越界 
            if(nx<0||ny<0||nx>=row||ny>=col) continue;
            //遇到更高的点 
            if(mp[nx][ny] > end){
                que.push({nx,ny});
                end = mp[nx][ny];
            }
        }
    }
    len = max(len,end-start+1);
}

int main(){
    //初始化 
    cin>>row>>col;
    for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++) cin>>mp[i][j];
    }
    //遍历起点 
    for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++){
            //起点高比最长路径小时 
            if(len>mp[i][j]) continue;
            bfs(i,j);
        }
    }
    cout<<len;
    return 0;
} 

by meowjiao @ 2024-01-13 15:06:43

本题目前好像没有bfs解法


|