现在的位置: 首页 > 综合 > 正文

hdu2579

2013年01月08日 ⁄ 综合 ⁄ 共 1179字 ⁄ 字号 评论关闭

/*
分析:
    BFS。
    再开一个维象,记录是以余数为几的情况推出这个next
情况的。

                                                       2012-11-12
*/

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"math.h"
#include"queue"
using namespace std;
int n,m,k;
int x_s,y_s;
int x_e,y_e;
char map[111][111];
int step[10][111][111];
int dir[4][2]={0,1, 1,0, 0,-1, -1,0};
struct node
{
	int f,x,y;
};
int BFS()
{
	queue<node>q;
	node now,next;
	int i;
	memset(step,-1,sizeof(step));
	now.f=0;
	now.x=x_s;
	now.y=y_s;
	q.push(now);
	step[now.f][now.x][now.y]=0;
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		if(now.x==x_e && now.y==y_e)	return step[now.f][now.x][now.y];
		for(i=0;i<4;i++)
		{
			next.x=now.x+dir[i][0];
			next.y=now.y+dir[i][1];
			next.f=(now.f+1)%k;
			if(next.x<0 || next.x>=n || next.y<0 || next.y>=m)	continue;
			if(map[next.x][next.y]=='#' && next.f)				continue;
			if(step[next.f][next.x][next.y]!=-1)				continue;
			step[next.f][next.x][next.y]=step[now.f][now.x][now.y]+1;
			q.push(next);
		}
	}
	return -1;
}
int main()
{
	int T;
	int i,l;
	int ans;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&m,&k);
		for(i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(l=0;map[i][l];l++)
			{
				if(map[i][l]=='Y')	{x_s=i;y_s=l;map[i][l]='.';}
				if(map[i][l]=='G')	{x_e=i;y_e=l;map[i][l]='.';}
			}
		}


		ans=BFS();
		if(ans==-1)	printf("Please give me another chance!\n");
		else		printf("%d\n",ans);
	}
	return 0;
}

抱歉!评论已关闭.