首页 > 其他分享 >HDU 2757 Ocean Currents

HDU 2757 Ocean Currents

时间:2022-11-09 22:33:57浏览次数:37  
标签:HDU point rep d% number 2757 grid mp Currents

Problem Description 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 consumption.  
Input The lake is represented as a rectangular grid. The first line of each test chunk 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. 

Please process to the end of the data file.
 
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 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 2 1

相当于找最短路,加个优先队列优化一下。

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
const int INF=0x7FFFFFFF;
const int sz=1005;
int n,m,t,bx,by,ex,ey;
int mp[sz][sz],f[sz][sz];
char s[sz];
int x[8]={-1,-1,0,1,1,1,0,-1};
int y[8]={0,1,1,1,0,-1,-1,-1};

struct point
{
    int x,y,z;
    point(int x=0,int y=0,int z=0):x(x),y(y),z(z){};
    bool operator<(const point &a) const 
    {
        return z>a.z;
    }
};

int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        memset(mp,-1,sizeof(mp));
        rep(i,1,n) 
        {
            scanf("%s",s+1);
            rep(j,1,m) mp[i][j]=s[j]-'0';
        }
        scanf("%d",&t);
        while (t--)
        {
            scanf("%d%d%d%d",&bx,&by,&ex,&ey);
            rep(i,1,n) rep(j,1,m) f[i][j]=INF;
            priority_queue<point> p;
            p.push(point(bx,by,f[bx][by]=0));
            while (!p.empty())
            {
                point q=p.top(); p.pop();
                if (q.x==ex&&q.y==ey) 
                {
                    printf("%d\n",f[ex][ey]);
                    break;
                }
                rep(i,0,7) 
                {
                    int X=q.x+x[i],Y=q.y+y[i];
                    if (mp[X][Y]==-1) continue;
                    if (f[X][Y]>f[q.x][q.y]+(i!=mp[q.x][q.y]))
                    {
                        f[X][Y]=f[q.x][q.y]+(i!=mp[q.x][q.y]);
                        p.push(point(X,Y,f[X][Y]));
                    }
                }
            }
        }
    }
    return 0;
}


标签:HDU,point,rep,d%,number,2757,grid,mp,Currents
From: https://blog.51cto.com/u_15870896/5838895

相关文章

  • HDU 1252 Hike on a Graph
    ProblemDescription"HikeonaGraph"isagamethatisplayedonaboardonwhichanundirectedgraphisdrawn.Thegraphiscompleteandhasallloops......
  • HDU 3345 War Chess
    ProblemDescriptionWarchessishh'sfavoritegame:Inthisgame,thereisanN*Mbattlemap,andeveryplayerhashisownMovingVal(MV).Ineach......
  • HDU 2660 Accepted Necklace
    ProblemDescriptionIhaveNpreciousstones,andplantouseKofthemtomakeanecklaceformymother,butshewon'tacceptanecklacewhichistooh......
  • HDU 1828 Picture
    ProblemDescriptionAnumberofrectangularposters,photographsandotherpicturesofthesameshapearepastedonawall.Theirsidesareallvertical......
  • HDU 3695 Computer Virus on Planet Pandora
    ProblemDescription    AliensonplanetPandoraalsowritecomputerprogramslikeus.Theirprogramsonlyconsistofcapitalletters(‘A’to‘Z’......
  • HDU 5379 Mahjong tree
    ProblemDescriptionLittlesunisanartist.Todayheisplayingmahjongalone.Hesuddenlyfeelsthatthetreeintheyarddoesn'tlookgood.Sohewant......
  • HDU 3397 Sequence operation
    ProblemDescriptionlxhgwwgotasequencecontainsncharacterswhichareall'0'sor'1's.Wehavefiveoperationshere:Changeoperations:0ab......
  • HDU 1255 覆盖的面积
    ProblemDescription给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试......
  • HDU 2665 Kth number
    ProblemDescriptionGiveyouasequenceandaskyouthekthbignumberofainteval.InputThefirstlineisthenumberofthetestcases. F......
  • HDU 2715 Herd Sums
    DescriptionThecowsinfarmerJohn'sherdarenumberedandbrandedwithconsecutiveintegersfrom1toN(1<=N<=10,000,000).Whenthecowscometot......