D题
生成树
https://ac.nowcoder.com/acm/contest/6607/A
**bfs加优先队列 优先队列自动从小到大 这样找到出口,自然是最小值** #pragma GCC optimze(2); #include"bits/stdc++.h" using namespace std; int n,m; int a[50][50]; int visit[50][50]; int p[]={0,0,-1,1}; int q[]={-1,1,0,0}; int bx,by,ex,ey; struct node{ int x; int y; int sum; friend bool operator < (const node &a,const node b){ return a.sum>b.sum; } }; priority_queue<node>v; int main(){ std::ios::sync_with_stdio(false); cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ char c; cin>>c; if(c>='1'&&c<='9') a[i][j]=c-'0'; if(c=='A'||c=='B'||c=='C') a[i][j]=100; if(c=='S'){ bx=i; by=j; } if(c=='E'){ ex=i; ey=j; } } } v.push(node{bx,by,0}); while(!v.empty()){ node top=v.top(); v.pop(); if(top.x==ex&&top.y==ey){ cout<<top.sum<<endl; return 0; } for(int i=0;i<4;i++){ int dx=top.x+p[i]; int dy=top.y+q[i]; if(dx>=1&&dx<=n&&dy>=1&&dy<=m&&!visit[dx][dy]){ visit[top.x][top.y]=1; v.push(node{dx,dy,top.sum+a[dx][dy]}); } } } return 0; }