首页 > 搜索 > BFS搜索 > HDU 2757-HOJ-Ocean Currents-优先队列-[解题报告]C++
2014
02-14

HDU 2757-HOJ-Ocean Currents-优先队列-[解题报告]C++

Ocean Currents

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

0
2
1
0
2
1

跑的好慢啊,看到别人的几百ms,就知道这道题目不能AC了就算数

先记在这

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int M =1010;
int map[M][M];
int vis[M][M];
int dir[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};
int r,c;
int sx,sy,ex,ey;
struct node{
    int x,y,ti;
    bool  operator < (const node &a) const
    {
        return a.ti<ti;
    };
};
bool is_ok(node a)
{
    if(a.ti >= vis[a.x][a.y]||a.x<1||a.x>r||a.y<1||a.y>c)
        return false;
    return true;
}
void bfs()
{
    priority_queue<node> Q;
    memset(vis,127,sizeof(vis));
    node s;s.x=sx;s.y=sy;
    s.ti=0;
    vis[sx][sy]=0;
    Q.push(s);
    while(!Q.empty())
    {
        node h=Q.top();
        Q.pop();
        if(h.x==ex&&h.y==ey) return ;
        for(int i=0;i<8;i++)
        {
            node t;
            t.x=h.x+dir[i][0];
            t.y=h.y+dir[i][1];
            t.ti=h.ti;
            if(map[h.x][h.y]!=i)
                t.ti+=1;
            if(is_ok(t))
            {
                vis[t.x][t.y]=t.ti;
                Q.push(t);
            }
        }
    }
}
int main()
{
    int m;
    while(scanf("%d%d",&r,&c)!=EOF)
    {
        for(int i=1;i<=r;i++)
            for(int j=1;j<=c;j++)
                scanf("%1d",&map[i][j]);
            scanf("%d",&m);
            while(m--)
            {
                scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
                if(sx==ex&&sy==ey) 
                {
                    printf("0\n");
                    continue;
                }
                bfs();
                printf("%d\n",vis[ex][ey]);
            }
    }
    return 0;
}

解题参考:http://www.cnblogs.com/wuyiqi/archive/2012/01/14/2322194.html


  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮