码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2757 Ocean Currents【广度优先搜索】

时间:2015-01-20 22:22:48      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

Ocean Currents

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


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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#define maxh 1000+100
#define inf 10000
using namespace std;
typedef struct node
{
    int x,y,step;
    friend bool operator < (node n1,node n2)
    {
        return n2.step<n1.step;
    }
}node;
int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
int map[maxh][maxh];
bool dis[maxh][maxh][8];
int n,m;
char s[maxh][maxh];
int judge(int x,int y,int i)
{
    if(x<1||x>n||y<1||y>m||dis[x][y][i])
    return 1;
    return 0;
}   
int bfs(int xi,int yi,int xj,int yj)
{   
    for(int i=0;i<=n;i++)
    for(int j=0;j<=m;j++)
    for(int l=0;l<8;l++)
    {
        dis[i][j][l]=0;
    }
    node fir,nex;
    priority_queue<node>Q;
    fir.x=xi,fir.y=yi,fir.step=0;
    Q.push(fir);
    while(!Q.empty())
    {   
        fir=Q.top();
        Q.pop();
        if(fir.x==xj&&fir.y==yj)
        {
            return fir.step;
        }
        for(int i=0;i<8;i++)
        {   
            nex.x=fir.x+dir[i][0];
            nex.y=fir.y+dir[i][1];
            if(judge(nex.x,nex.y,i))
            continue;
            if(i==map[fir.x][fir.y])
            {   
                nex.step=fir.step;
            }
            else
            {
                nex.step=fir.step+1;
            }
            
            dis[nex.x][nex.y][i]=1;
            Q.push(nex);
        }
    }
    return -1;
}
int main()
{
    int f;
    int a,b,c,d;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s[i]+1);
            for(int j=1;j<=m;j++)
            {
                map[i][j]=s[i][j]-‘0‘;
            }
        }
        scanf("%d",&f);
        for(int i=0;i<f;i++)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            printf("%d\n",bfs(a,b,c,d));
        }
    }
    return 0;
}
    

hdu 2757 Ocean Currents【广度优先搜索】

标签:

原文地址:http://blog.csdn.net/letterwuyu/article/details/42922503

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!