Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

hdu2757之BFS

$
0
0

Ocean Currents

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


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<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=1000+10;
int n,m,t,sx,sy,ex,ey;
char Map[MAX][MAX];
int mark[MAX][MAX];
int dir[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};

struct Node{
	int x,y,time;
	Node(){}
	Node(int X,int Y,int Time):x(X),y(Y),time(Time){}
	bool operator<(const Node &a)const{
		return time>a.time; 
	}
};

int BFS(){
	memset(mark,-1,sizeof mark);
	priority_queue<Node>q;
	Node next,oq;
	q.push(Node(sx,sy,0));
	mark[sx][sy]=0;
	while(!q.empty()){
		oq=q.top();
		q.pop();
		if(oq.x == ex && oq.y == ey)return mark[ex][ey];
		for(int i=0;i<8;++i){
			next=Node(oq.x+dir[i][0],oq.y+dir[i][1],oq.time);
			if(next.x<0 || next.y<0 || next.x>=n || next.y>=m)continue;
			if(Map[oq.x][oq.y] != i+'0')++next.time;
			if(mark[next.x][next.y] == -1 || mark[next.x][next.y] > next.time){
				mark[next.x][next.y]=next.time,q.push(next);
			} 
		}
	}
	return -1;
}

int main(){
	while(~scanf("%d%d",&n,&m)){
		for(int i=0;i<n;++i)scanf("%s",Map[i]);
		scanf("%d",&t);
		while(t--){
			scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
			--sx,--sy,--ex,--ey;
			printf("%d\n",BFS());
		}
	}
	return 0;
}

作者:xingyeyongheng 发表于2013-9-3 21:13:24 原文链接
阅读:50 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles