HDU 1429 胜利大逃亡(续)

请不要随便指点别人该怎么做、每个人的人生都应该自己掌握、你给不了别人一切、你也不懂别人的忧伤、

                                                                                          微笑不代表快乐、哭泣不一定悲伤

               不努力怎么让关心你的人幸福、不努力怎么让看不起你的人绝望、

                                                                                                                                    

                                                                                                                                                              我用生命在奋斗——lx_Zz

—————————————————————————————————————————————————————————————

—————————————————————————    华丽的分割线    ————————————————————————————

—————————————————————————————————————————————————————————————

107752942014-05-20 19:59:13Accepted1429437MS2336K2372 BG++给我再去相....
水题、标记多状态就行、

虽然是1A、、但是手速弱的一B、、、

/* 题目: HDU 1429    */
/* 作者: lx_Zz       */
/* 时间: 2014.5.20   */
#include<cstdio>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff
#define LL __int64

int dir[4][2]={0,1,1,0,-1,0,0,-1};
int vis[21][21][1<<10];
char mpt[21][21];
int n,m,t;
struct node
{
	int x,y;
	int state;
	int step;
};

int BFS(int sx,int sy,int ex,int ey)
{
	int ans=-1;
	memset(vis,0,sizeof(vis));
	vis[sx][sy][0]=1;
	queue<node>q;
	node now,next;
	now.x=sx;now.y=sy;
	now.state=0;now.step=0;
	q.push(now);
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		if(now.x==ex&&now.y==ey)
		{
			ans=now.step;break;
		}

		for(int i=0;i<4;i++)
		{
			int tx=now.x+dir[i][0];
			int ty=now.y+dir[i][1];
			if(tx<1||ty<1||tx>n||ty>m)continue;
			if(mpt[tx][ty]=='*')continue;
			else if(vis[tx][ty][now.state]==0&&(mpt[tx][ty]=='.'||mpt[tx][ty]=='^'||mpt[tx][ty]=='@'))
			{
				next.x=tx;
				next.y=ty;
				next.state=now.state;
				next.step=now.step+1;
				vis[tx][ty][now.state]=1;
				q.push(next);
			}
			else if(vis[tx][ty][now.state]==0&&(mpt[tx][ty]>='a'&&mpt[tx][ty]<='z'))
			{

				vis[tx][ty][now.state]=1;
				int temp=1<<(mpt[tx][ty]-'a');
				next.x=tx;
				next.y=ty;
				next.state=now.state|temp;
				next.step=now.step+1;
				q.push(next);
			}
			else if(vis[tx][ty][now.state]==0&&(mpt[tx][ty]>='A'&&mpt[tx][ty]<='Z'))
			{
				vis[tx][ty][now.state]=1;
				int temp=1<<(mpt[tx][ty]-'A');
				if(now.state&temp)
				{
					next.x=tx;
					next.y=ty;
					next.state=now.state;
					next.step=now.step+1;
					q.push(next);
				}
			}
		}
	}
	return ans;
}


int main()
{
    //freopen("C:\\Users\\终将我要华丽的逆袭\\Desktop\\lx_Zz_in.txt","r",stdin);
    //freopen("C:\\Users\\终将我要华丽的逆袭\\Desktop\\lx_Zz_out.txt","w",stdout);

	while(scanf("%d%d%d",&n,&m,&t)!=EOF)
	{
		int sx,sy;
		int ex,ey;
		for(int i=1;i<=n;i++)
		{
			scanf("%s",mpt[i]+1);
			for(int j=1;j<=m;j++)
			{
				if(mpt[i][j]=='@')
				{
					sx=i;sy=j;
				}
				if(mpt[i][j]=='^')
				{
					ex=i;ey=j;
				}
			}
		}
		int ans=BFS(sx,sy,ex,ey);
		if(ans<t)
			printf("%d\n",ans);
		else printf("-1\n");
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

N诺计算机考研

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值