其他分享
首页 > 其他分享> > 1021 Ocean Currents bfs+优先队列

1021 Ocean Currents bfs+优先队列

作者:互联网

For a boat on a large body of water, strong currents can be dangerous, but with careful planning, they
can be harnessed to help the boat reach its destination. Your job is to help in that planning.
At each location, the current flows in some direction. The captain can choose to either go with the
flow of the current, using no energy, or to move one square in any other direction, at the cost of one
energy unit. The boat always moves in one of the following eight directions: north, south, east, west,
northeast, northwest, southeast, southwest. The boat cannot leave the boundary of the lake.
You are to help him devise a strategy to reach the destination with the minimum energy consump-
tion.
Input
The lake is represented as a rectangular grid. The first line of input contains two integers r and c, the
number of rows and columns in the grid. The grid has no more than 1000 rows and no more than 1000
columns. Each of the following r lines contains exactly c characters, each a digit from 0 to 7 inclusive.
The character ‘0’ means the current flows north (i.e. up in the grid, in the direction of decreasing
row number), ‘1’ means it flows northeast, ‘2’ means east (i.e. in the direction of increasing column
number), ‘3’ means southeast, and so on in a clockwise manner:
7 0 1
\|/
6-*-2
/|\
5 4 3
The line after the grid contains a single integer n, the number of trips to be made, which is at most
50. Each of the following n lines describes a trip using four integers rs, cs, rd, cd, giving the row and
column of the starting point and the destination of the trip. Rows and columns are numbered starting
with 1.
Output
For each trip, output a line containing a single integer, the minimum number of energy units needed
to get from the starting point to the destination.
Sample Input
5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4
Sample Output
0
2
1

题目大意

地图上存的是水流的方向,0上,1右上,2右。。。这样,顺着水流不消耗能量,否则消耗一点能量。问从起点到终点消耗的最小能量。

分析

bfs + 优先队列。优先队列存消耗能量少的。

vis有点没想清楚。一开始想的是:再开一维,表示有无能量走到这里。

看了题解,题解是直接存一个dis,然后比之前近的就存进去。

可是我寻思bfs就是最短路,是不是连再开一维都没必要,因为第一次走到这个位置就是最近的一次。

但是判题器有问题判不了题。。

易错点

dx是上下,dy是左右。

-----------------------------------------------------------------------#include<bits/stdc++.h>

int dx[] = {-1,-1,0,1,1,1,0,-1}, dy[] = {0,1,1,1,0,-1,-1,-1};
// 上,下,左,右
// 在这里上下是dx变化,左右是dy变换。。。

/*文档区


*/

//-------------------------代码----------------------------

//#define int LL
const int N = 1010;
struct Q {
    int x,y,e;
    bool operator<(const Q & s) const {
        return e > s.e;
    }
};
int n,m;
PII st,ed;
int mp[N][N],vis[N][N];
priority_queue<Q> q;

int bfs() {
    while(q.size()) {
        auto t = q.top();q.pop();
        int x = t.x,y = t.y,e = t.e;
        if(vis[x][y]) continue;
        vis[x][y] = true;
        if(x == ed.x && y == ed.y)return e;
        for(int i = 0;i<8;i++) {
            int xx = x + dx[i],yy = y + dy[i];
            if(xx < 1 || yy < 1 || xx > n || yy > m) continue;
            if(i == mp[x][y]) {
                if(vis[xx][yy])continue;
                q.push({xx,yy,e});
            } else {
                if(vis[xx][yy])continue;
                q.push({xx,yy,e + 1});
            }
        }
    }
    return 0;
}

void solve()
{
     cin>>n>>m;
//     V<V<int>>mp(n+1,V<int>(m+1));
     fo(i,1,n) {
         fo(j,1,m) {
             char c;cin>>c;
             mp[i][j] = c - '0';
         }
     }
     
     fo(i,1,n) {
         fo(j,1,m) {
             cout<<mp[i][j]<<' ';
         } cout<<endl;
     }
     
    int t;cin>>t;
    while( t -- ) {
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        st = {a,b};ed = {c,d};
        memset(vis,0,sizeof vis);
        while(q.size())q.pop();
        q.push({a,b,0});
        cout<<bfs()<<endl;;
    }
}

signed main(){
    clapping();TLE;
    
//    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区
5 5
04125
03355
64734
72377
02062
1
5 3 3 4

5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4

*/

//------------------------------------------------------------

标签:yy,1021,int,cin,Ocean,bfs,vis,xx,grid
来源: https://www.cnblogs.com/er007/p/16437994.html