hdu 2757 Ocean Currents

来源:互联网 发布:java 1.8.0 openjdk 编辑:程序博客网 时间:2024/06/13 23:56

Ocean Currents

                                                              Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                   Total Submission(s): 1896    Accepted Submission(s): 666


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 5041250335564734723770206234 2 4 24 5 1 45 3 3 45 5041250335564734723770206234 2 4 24 5 1 45 3 3 4
 

Sample Output
021021
 题目大意:
给出一个矩阵,矩阵中的每个数字代表风向,其中顺风时间行走花费为零,否则花费为1,分别给出多组数据,多个起点和终点,求出对应的最小花费。
解题思路:
dfs加优先队列,由于第一次写优先队列的题,感觉不会用的东西写着有点吃力,虽然自己理解,花费不少时间,终于算是ac了,不过时间花费太大了,看了网上有些代码,还是能简化的。优先队列这种题应该注意,不是步数最小时间花费就一定最小,因此应该每次把花费最小的排到队列的前面,优先考虑,依次类推到终点。
代码:
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>using namespace std;struct point{int x,y;int s;  friend bool operator < (const point a,const point b)  {return a.s>b.s;   }};int a[1010][1010];int b[1010][1010];int m,n,x0,y0,x,y;int d[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};void bfs(point w){priority_queue<point> Q;int i;Q.push(w);point hd;while(!Q.empty()){   hd=Q.top();   Q.pop();   if(hd.x==x&&hd.y==y)   {    return;   }   for(i=0;i<8;i++)   {    int nx=hd.x+d[i][0];    int ny=hd.y+d[i][1];    if(nx<=0||ny<=0||nx>m||ny>n)      continue;      point t;      t.x=nx;t.y=ny;if(i!=a[hd.x][hd.y])      t.s=hd.s+1;      else      t.s=hd.s;      if(b[t.x][t.y]>t.s)        { b[t.x][t.y]=t.s;           Q.push(t);    }       }}return ;}int main(){int i,j,k,t;char s0;while(scanf("%d%d",&m,&n)!=EOF){getchar();for(i=1;i<=m;i++) {   for(j=1;j<=n;j++)   {    scanf("%c",&s0);a[i][j]=s0-'0';    }   getchar();     }     scanf("%d",&t);     while(t--)     {  scanf("%d%d%d%d",&x0,&y0,&x,&y);  if(x0==x&&y0==y)   {   printf("0\n");   continue;   }  for(i=1;i<=m;i++)   for(j=1;j<=n;j++)     b[i][j]=2222;  point v;  v.x=x0;  v.y=y0;  v.s=0;  b[x0][y0]=0;  bfs(v);  printf("%d\n",b[x][y]);     }}return 0; } 
原创粉丝点击