题解:AT_zone2021_e 潜入

· · 题解

题目理解:

AC 代码:

#include<bits/stdc++.h>
using namespace std;
long long d[1010][1010];
long long a[1010][1010],b[1010][1010];
int wx[]={-1,1,0,0};
int wy[]={0,0,-1,1};
int r,c;
const int inf=1e9;
void bfs()
{
    for(int i=0;i<=r;i++)for(int j=0;j<=c;j++) d[i][j]=inf;
    d[1][1]=0;
    priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>q;
    q.push(make_pair(0,make_pair(1,1)));
    while(!q.empty())
    {
        auto f=q.top();q.pop();
        int v=f.first;
        int x=(f.second).first;
        int y=(f.second).second;
            //cout<<d[3][3]<<endl;
        if(v>d[x][y]) continue;
        //printf("d[%d][%d]=%d\n",x,y,v);
        for(int i=0;i<4;i++)
        {
            if(i==0)
            {
                for(int j=x-1;j>=1;j--)
                {
                    if(v+(x-j+1)<d[j][y])
                    { 
                        d[j][y]=v+(x-j+1); 
                        q.push(make_pair(v+(x-j+1),make_pair(j,y)));
                    }               
                }
            }
            else
            {
                int fx=x+wx[i];
                int fy=y+wy[i];
                if(fx>r || fx<1 || fy>c || fy<1)
                {
                    continue;
                }
                int new_v=v;
                if(i==1)
                {
                    //cout<<b[x][y]<<endl;
                    new_v+=b[x][y]; 
                }
                if(i==2)
                {
                    new_v+=a[fx][fy];
                }
                if(i==3)
                {
                    new_v+= a[x][y];
                }
                //cout<<new_v<<endl;
                if(new_v<d[fx][fy])
                {
                    d[fx][fy]=new_v;
                    q.push(make_pair(new_v,make_pair(fx,fy)));
                }
            }
        } 
    }
    cout<<d[r][c];
}   
int main()
{
    cin>>r>>c;
    for(int i=1;i<=r;i++)
    {
        for(int j=1;j<c;j++)
        {
            cin>>a[i][j];
        } 
    }
    for(int i=1;i<r;i++)
    {
        for(int j=1;j<=c;j++)
        {
            cin>>b[i][j];
        } 
    }
    bfs(); 
}

AC 记录