首页 > 其他分享 >旅途

旅途

时间:2022-10-04 19:23:53浏览次数:25  
标签:cmp1 struct point int ans 旅途 op

题目:

 

 

 

 这一题是典型的BFS模板题只有几点需要注意:

1.要靠小根堆顶替栈这样才能最优

2.要想到BFS,乍一看给人DFS的感觉但DFS会超,所以用BFS

3.因为这里有x,y值,所以要开struct 小根堆+结构体?总结如下:

struct point{
    int x,y,a;
};
struct cmp1{
    int operator() (point x,point y) const
    {
        return x.a>y.a;
    }
};
priority_queue<point,vector<point>,cmp1> q;

4.最后要注意什么初始化之类的

程序:

#include<bits/stdc++.h>
using namespace std;
const int N=530;
int n,m,a[N][N]={0},vis[N][N]={0},d[10][10]={{1,0},{0,1},{-1,0},{0,-1}},b[N][N]={0},ans=0;
struct point{
    int x,y,a;
};
struct cmp1{
    int operator() (point x,point y) const
    {
        return x.a>y.a;
    }
};
priority_queue<point,vector<point>,cmp1> q;
void bfs()
{
    while(q.size())
    {
        point op=q.top();
        ans=max(ans,a[op.x][op.y]);
        q.pop();
        if(op.x==n&&op.y==m)
        {
            return;
        }
        for(int i=0;i<4;i++)
        {
            point np;
            np.x=op.x+d[i][0];
            np.y=op.y+d[i][1];
            np.a=a[np.x][np.y];
            if(np.x<=n&&np.x>=1&&np.y>=1&&np.y<=m&&vis[np.x][np.y]==0)
            {
                vis[np.x][np.y]=1;
                
//                a[np.x][np.y]+=a[op.x][op.y];
                q.push(np);
                
            }
            
        }
//        cout<<"a: "<<op.a<<" x: "<<op.x<<" y: "<<op.y<<endl;
    }
}
int main()
{
     freopen( "travel.in", "r", stdin );
     freopen( "travel.out", "w", stdout );
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&a[i][j]);
            b[i][j]=a[i][j];
        }
    }
    vis[1][1]=1;
    point t;
    t.x=1;
    t.y=1;
    t.a=a[1][1];
    ans=a[1][1];
    q.push(t);
    bfs();
//    cout<<a[n][m];
//    for(int i=1;i<=n;i++)
//    {
//        for(int j=1;j<=m;j++) cout<<a[i][j]<<" ";
//        cout<<endl;
//    } 
    cout<<ans;
    return 0;
}

 

标签:cmp1,struct,point,int,ans,旅途,op
From: https://www.cnblogs.com/wjk53233/p/16754260.html

相关文章