Ocean Currents

For a boat on a large body of water, strong currents can be dangerous, but with careful planning, theycan 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 theflow of the current, using no energy, or to move one square in any other direction, at the cost of oneenergy 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 consumption.

Input
The lake is represented as a rectangular grid. The first line of input contains two integers r and c, thenumber of rows and columns in the grid. The grid has no more than 1000 rows and no more than 1000columns. 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 decreasingrow number), ‘1’ means it flows northeast, ‘2’ means east (i.e. in the direction of increasing columnnumber), ‘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 most50. Each of the following n lines describes a trip using four integers rs, cs, rd, cd, giving the row andcolumn 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 neededto 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

//190ms
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int M = 1024, MOD = 1000000007;
const int go[8][2]={
    
    {
    
    -1,0},{
    
    -1,1},{
    
    0,1},{
    
    1,1},{
    
    1,0},{
    
    1,-1},{
    
    0,-1},{
    
    -1,-1}};
char save[M][M];
int dis[M][M],n,m;
inline int read()
{
    
    
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {
    
    if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){
    
    x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int bfs(int str,int stc,int edr,int edc)
{
    
    
    memset(dis,0x3f,sizeof(dis));
    deque<pair<int,int>> bfs;
    dis[str][stc] = 0;
    bfs.emplace_front(str,stc);
    while(!bfs.empty())
    {
    
    
        int r = bfs.front().first, c = bfs.front().second; bfs.pop_front();
        if(r==edr&&c==edc) return dis[r][c];
        for(int k=0;k<8;++k)
        {
    
    
            int nr = r+go[k][0], nc = c+go[k][1];
            if(nr>=1&&nr<=n&&nc>=1&&nc<=m)
            {
    
    
                int nd = dis[r][c]+(k!=save[r][c]-'0');
                if(dis[nr][nc] > nd)
                {
    
    
                    dis[nr][nc] = nd;
                    if(nd==dis[r][c]) bfs.emplace_front(nr,nc);
                    else bfs.emplace_back(nr,nc);
                }
            }
        }
    }
    return -1;
}
int main()
{
    
    
    n = read(), m=read();
    for(int i=1;i<=n;++i)
        scanf("%s",save[i]+1);
    int q = read();
    while(q--)
    {
    
    
        int r1=read(),c1=read(),r2=read(),c2=read();
        printf("%d\n",bfs(r1,c1,r2,c2));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44826711/article/details/114232376