swustoj(Escape(1023))(多状态BFS)

BH is in a maze,the maze is a matrix,he wants to escape!

Description

The input consists of multiple test cases.


For each case,the first line contains 2 integers N,M( 1 <= N, M <= 100 ).


Each of the following N lines contain M characters. Each character means a cell of the map.


Here is the definition for chracter.


 


For a character in the map: 


'S':BH's start place,only one in the map.


'E':the goal cell,only one in the map.


'.':empty cell.


'#':obstacle cell.


'A':accelerated rune.


 


BH can move to 4 directions(up,down,left,right) in each step.It cost 2 seconds without accelerated rune.When he get accelerated rune,moving one step only cost 1 second.The buff lasts 5 seconds,and the time doesn't stack when you get another accelerated rune.(that means in anytime BH gets an accelerated rune,the buff time become 5 seconds).

Input

The minimum time BH get to the goal cell,if he can't,print "Please help BH!".

Output
1
2
3
4
5
6
7
8
9
10
11
12
13
5 5
....E
.....
.....
##...
S#...
5 8
........
........
..A....A
A######.
S......E
Sample Input
1
2
Please help BH!
12
Sample Output

由于OJ上传数据的BUG,换行请使用"\r\n",非常抱歉


题目大意:一个迷宫逃离问题,只是有了加速符A,正常情况下通过一个格子2s,有了加速符只要1s,并且加速符持续5s,‘S'代表起点

     'E'代表终点,'#'代表障碍,'.'空格子,能够逃离输出最少用时,否则输出"Please help BH!"

解题思路:BFS,用一个3维dp数组存贮,每一点在不同加速状态下的值,然后筛选dp数组终点的最小值即可



#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;

char mp[105][105];
int n, m;
int ans = 0;
int dp[105][105][6];

struct node
{
	int x, y;
	int jiasu;
	int sj;
}sd,ed;
int vis[105][105];
int flag;
int dir[4][2] = { 1,0,0,1,-1,0,0,-1 };

void BFS()
{
	queue<node>q;
	node ss, tt;
	flag = 0;
	memset(vis, 0, sizeof(vis));
	ss = sd;
	q.push(ss);
	dp[ss.x][ss.y][0] = 0;
	vis[ss.x][ss.y] = 1;
	while (!q.empty())
	{
		ss = q.front();
		q.pop();

		/*if (ss.x == ed.x&&ss.y == ed.y)
		{
			cout << ss.sj;
			printf("\r\n");
			flag = 1;
			return;
		}*/
		for (int i = 0; i < 4; i++)
		{
			tt.x = ss.x + dir[i][0];
			tt.y = ss.y + dir[i][1];
			if (tt.x < 0 || tt.x >= n || tt.y < 0 || tt.y >= m  || mp[tt.x][tt.y] == '#')
			{
				continue;
			}
			/*if (mp[tt.x][tt.y] == 'E')
			{
				cout << ss.sj;
				printf("\r\n");
				flag = 1;
				return;
			}*/
			if (ss.jiasu != 0)
			{
				tt.sj = ss.sj + 1;
				tt.jiasu = ss.jiasu - 1;
			}
			else
			{
				tt.sj = ss.sj + 2;
				tt.jiasu = 0;
			}
			//vis[tt.x][tt.y] = 1;
			if (mp[tt.x][tt.y] == 'A')
			{
				tt.jiasu = 5;
			}
			if (tt.sj < dp[tt.x][tt.y][tt.jiasu])
			{
				dp[tt.x][tt.y][tt.jiasu] = tt.sj;
				q.push(tt);
			}
		}

	}

	ans = 9999999;
	for (int i = 4; i >= 0; i--)
	{
		ans = min(ans, dp[ed.x][ed.y][i]);
	}
	if (ans >= 9999999)
	{
		cout << "Please help BH!\r\n";
	}
	else
	{
		cout << ans << "\r\n";
	}


}



int main()
{
	while (cin >> n >> m)
	{
	//getcher();
		memset(dp, inf, sizeof(dp));
		for (int i = 0; i < n; i++)
		{
			cin >> mp[i];//不知道为什么用scanf("%c",&mp[i][j]);会错
			for (int j = 0; j < m; j++)
			{
				
				if (mp[i][j] == 'S')
				{
					sd.x = i;
					sd.y = j;
					sd.jiasu = 0;
					sd.sj = 0;
				}
				if (mp[i][j] == 'E')
				{
					ed.x = i;
					ed.y = j;
					ed.jiasu = 0;
					sd.sj = 0;
				}
			}
		}

		BFS();

	}

	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值