首页 > 搜索 > BFS搜索 > hdu 2579 Dating with girls(2)-BFS-[解题报告]C++
2014
02-10

hdu 2579 Dating with girls(2)-BFS-[解题报告]C++

Dating with girls(2)

问题描述 :

If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and the girl you want to date with is also in the maze.If you can find the girl, then you can date with the girl.Else the girl will date with other boys. What a pity!
The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there.
There are only ‘.’ or ‘#’, ’Y’, ’G’ on the map of the maze. ’.’ indicates the blank which you can move on, ‘#’ indicates stones. ’Y’ indicates the your location. ‘G’ indicates the girl’s location . There is only one ‘Y’ and one ‘G’. Every seconds you can move left, right, up or down.

输入:

The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c (1 <= r , c <= 100), and k(2 <=k <= 10).
The next r line is the map’s description.

输出:

The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c (1 <= r , c <= 100), and k(2 <=k <= 10).
The next r line is the map’s description.

样例输入:

1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.

样例输出:

7

http://acm.hdu.edu.cn/showproblem.php?pid=2579

题目大意:给定 r * c 的迷宫,还有一个整数 k 。迷宫中“.”表示可以走,“#”表示墙,当时间为k的倍数时,这些墙会消失。求从起点“Y”到终点“G”的最短时间。(人不能呆在一点不动)。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<algorithm>
#define N 105
using namespace std;
char map[N][N];
int step[N][N][10];//多加一维,记录(time%k)
int r,c,k,x_s,y_s,x_e,y_e;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
struct node
{
	int x,y,mod;
};
int BFS()
{
	int i;
	queue<node>q;
	node now,next;
	now.x=x_s;
	now.y=y_s;
	now.mod=0;
	memset(step,-1,sizeof(step));
	step[now.x][now.y][now.mod]=0;
	q.push(now);
	while(!q.empty()){
		now=q.front();
		q.pop();
		if(now.x==x_e && now.y==y_e) return step[now.x][now.y][now.mod];
		for(i=0;i<4;i++){
			next.x=now.x+dir[i][0];
			next.y=now.y+dir[i][1];
			next.mod=(now.mod+1)%k;
			if(next.x<0 || next.x>=r || next.y<0 ||next.y>=c) continue;
			if(step[next.x][next.y][next.mod]!=-1)            continue;
			if(map[next.x][next.y]=='#' && next.mod!=0)       continue;
			step[next.x][next.y][next.mod]=step[now.x][now.y][now.mod]+1;
			q.push(next);
		}
	}
	return -1;
}
int main()
{
	int T,i,j,ans;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&r,&c,&k);
		for(i=0;i<r;i++){
			scanf("%s",map[i]);
			for(j=0;map[i][j];j++){
				if(map[i][j]=='Y'){
					x_s=i;y_s=j;map[i][j]='.';
				}
				if(map[i][j]=='G'){
					x_e=i;y_e=j;map[i][j]='.';
				}
			}
		}
		ans=BFS();
		if(ans==-1) printf("Please give me another chance!\n");
		else printf("%d\n",ans);
	}
	return 0;
}

解题转自:http://blog.csdn.net/crazy_xiaohe/article/details/8883608


,
  1. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish