动态规划三维 hdu2579

Problem Description
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.
 

Input
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.
 

Output
For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".
 

Sample Input
  
  
1 6 6 2 ...Y.. ...#.. .#.... ...#.. ...#.. ..#G#.
 

Sample Output
  
  
7
 

Source
 

Recommend
lcy
 
题意:
  从 Y到G  问最短的路程是什么  其中 .为可走路径 #为石头 其中石头在时间%k==0的时候是可以消失的 即使可以走的     如果不等于0 就会又出现

 


这道题有两个难点。第一个是状态的表示,因为不同时间的状态不同,所以要用三维。(其实我觉得只有石头的地方用三维就够了,但是反正复杂度没要求,随便开)

第二个就是数据的读入。有同学用字符串,有同学用c++的读入流,都非常好。这里我用的傻逼方法是在每个scanf前面都加一个scanf("\n");.

下面是代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
#include<queue>
using namespace std;
struct myk{
	int x;
	int y;
	int t;
};
const int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
char a[110][110],b[110][110][11];
int visit[110][110][11],step[110][110][11];
int n,m,xx,yy,tt,temp_x,temp_y,temp_t,k,start_x,start_y,end_x,end_y;
myk temp;
queue <myk> q;
void duru(void)
{
    scanf("%d%d%d",&n,&m,&k);
    scanf("\n");
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf("\n");
            scanf("%c",&a[i][j]);
            if(a[i][j]=='Y')
            {
                start_x=i;
                start_y=j;
            }
            if(a[i][j]=='G')
            {
                end_x=i;
                end_y=j;
            }
        }

    }
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {
        if(a[i][j]=='#')
            b[i][j][0]='.';
        for(int jishu=1;jishu<k;jishu++)
            b[i][j][jishu]=a[i][j];
    }
}
bool panduan(int aa,int bb)
{
	if((aa>=1)&&(aa<=n)&&(bb>=1)&&(bb<=m))
		return true;
	else
		return false;
}
void initialization(void)
{
	while(!q.empty())
		q.pop();
	memset(step,-1,sizeof(step));
	memset(visit,0,sizeof(visit));
	temp.x=start_x;
	temp.y=start_y;
	temp.t=0;
	q.push(temp);
	visit[temp.x][temp.y][0]=1;
	step[temp.x][temp.y][0]=0;
}
void push(int xx,int yy,int tt)
{
	temp.x=xx;
	temp.y=yy;
	temp.t=tt;
	q.push(temp);
	step[xx][yy][tt]=1+step[temp_x][temp_y][temp_t];
	visit[xx][yy][tt]=1;
}
void bfs(void)
{
	while(!q.empty())
	{
		temp=q.front();
		temp_x=temp.x;
		temp_y=temp.y;
		temp_t=temp.t;
		q.pop();
		for(int i=0;i<4;i++)
		{
			xx=temp_x+dx[i];
			yy=temp_y+dy[i];
			tt=(temp_t+1)%k;
			if((panduan(xx,yy))&&(!visit[xx][yy][tt])&&(b[xx][yy][tt]!='#'))
				push(xx,yy,tt);
		}
	}
}
int main(void)
{
	int status,minn,t;
	scanf("%d",&t);
	while(t--)
	{
		memset(b,-1,sizeof(b));
		duru();
/*
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
                printf("%c",a[i][j]);
            printf("\n");
        }
*/
		initialization();
		bfs();
		status=0;
		minn=INT_MAX;
		for(int i=0;i<k;i++)
			if(step[end_x][end_y][i]>0)
			{
				status=1;
				minn=min(minn,step[end_x][end_y][i]);
			}
		if(status==0)
			printf("Please give me another chance!\n");
		else
			printf("%d\n",minn);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值