HDU 2757 Ocean Currents "breadth First search"

Source: Internet
Author: User

Ocean CurrentsTime limit:6000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1561 Accepted Submission (s): 516


Problem descriptionfor a boat on a large body of water, strong currents can is dangerous, but with careful planning, they Can is harnessed to help the boat reach its destination. Your job is-to-help in this 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 dire Ction, at the cost of one energy unit. The boat 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. Him devise a strategy to reach the destination and the minimum energy consumption.
Inputthe Lake is represented as a rectangular grid. The first line of each test chunk contains-integers r and C, the number of rows and columns in the grid. The grid has no more than, and no more than 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 cloc kwise manner:

7 0 1
\|/
6-*-2
/|\
5 4 3

The line after the grid contains a single integer n and the number of trips to be made, which are 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 Starti Ng Point and the destination of the trip. Rows and columns is numbered starting with 1.

Please process to the end of the data file.

Outputfor, 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
Analysis: The subject can be done with the priority queue, but and the general wide search is the difference between the operation of the tag array, because it is the minimum energy consumption, so the state of the mark is the current position and the energy consumed at this location, so you can use the tag array to store energy, if the current position state consumes the total energy is less than the location Update tag Array, code example #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&AMP;&AMP;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 "breadth First search"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.