HDU 2757 Ocean 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
code:
View Code
#include<stdio.h>
#include<string.h>
#include<queue>
#define inf 8000
#define clr(x)memset(x,inf,sizeof(x))
using namespace std;
int f[16]={-1,-1,0,1,1,1,0,-1,0,1,1,1,0,-1,-1,-1};
struct node
{
int x,y,step;
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
}pp,st,en,tmp;
char s[1001];
int a[1001][1001];
int step[1001][1001];
int main()
{
int ca,i,j,n,m,front,rear;
while(scanf("%d%d",&n,&m)!=EOF)
{
priority_queue<node>que;
for(i=1;i<=n;i++)
{
scanf("%s",s);
for(j=0;j<m;j++)
a[i][j+1]=s[j]-'0';
}
scanf("%d",&ca);
while(ca--)
{
scanf("%d%d%d%d",&st.x,&st.y,&en.x,&en.y);
clr(step);
step[st.x][st.y]=0;
st.step=0;
que.push(st);
while(!que.empty())
{
pp=que.top();
if(pp.x==en.x&&pp.y==en.y)
{printf("%d\n",step[pp.x][pp.y]);break;}
for(i=0;i<8;i++)
{
tmp.x=pp.x+f[i]; tmp.y=pp.y+f[i+8];
if(tmp.x>=1&&tmp.x<=n&&tmp.y>=1&&tmp.y<=m&&step[tmp.x][tmp.y]>step[pp.x][pp.y]+(i!=a[pp.x][pp.y]))
{
step[tmp.x][tmp.y]=step[pp.x][pp.y]+(i!=a[pp.x][pp.y]);
tmp.step=step[tmp.x][tmp.y];
que.push(tmp);
}
}
que.pop();
}
}
}
return 0;
}
posted @ 2012-03-23 13:09  'wind  阅读(233)  评论(0编辑  收藏  举报