(HDU2128) Tempter of the Bone II-BFS

Tempter of the Bone II

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 98304/32768 K (Java/Others)
Total Submission(s): 2573    Accepted Submission(s): 728


 

Problem Description

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze was changed and the way he came in was lost.He realized that the bone was a trap, and he tried desperately to get out of this maze.


The maze was a rectangle with the sizes of N by M. The maze is made up of a door,many walls and many explosives. Doggie need to reach the door to escape from the tempter. In every second, he could move one block to one of the upper, lower, left or right neighboring blocks. And if the destination is a wall, Doggie need another second explode and a explosive to explode it if he had some explosives. Once he entered a block with explosives,he can take away all of the explosives. Can the poor doggie survive? Please help him.

 

 

Input

The input consists of multiple test cases. The first line of each test case contains two integers N, M,(2 <= N, M <= 8). which denote the sizes of the maze.The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall;
'S': the start point of the doggie;
'D': the Door;
'.': an empty block;
'1'--'9':explosives in that block.

Note,initially he had no explosives.

The input is terminated with two 0's. This test case is not to be processed.

 

 

Output

For each test case, print the minimum time the doggie need to escape if the doggie can survive, or -1 otherwise.

 

 

Sample Input


 

4 4 SX.. XX.. .... 1..D 4 4 S.X1 .... ..XX ..XD 0 0

 

 

Sample Output


 

-1 9

題目分析:https://blog.csdn.net/acm_cxlove/article/details/7635497

代碼:

#include<iostream>
#include<queue>
#include<string.h>
#include<vector>
#define LL unsigned long long
using namespace std;
int n, m;
int sx, sy;
char mapp[10][10];
int p[4][2] = { {1,0}, {-1,0}, {0,1}, {0,-1} };
int wallcnt, wallmap[10][10];
int bombcnt, bombmap[10][10];

struct Node {
	int x, y, step, bomb;
	LL vis;
	LL wall;
	bool check() {
		if (x >= 0 && y >= 0 && x < n&&y < m)
			return true;
		return false;
	}
	bool operator<(const Node n1)const {
		return step > n1.step;
	}
}s,e,u,v;
vector<Node> flag[8][8][8 * 8 * 9 + 1];
bool check(Node tmp)
{
	for (int i = 0; i < flag[tmp.x][tmp.y][tmp.bomb].size(); i++)
		if (tmp.wall == flag[tmp.x][tmp.y][tmp.bomb][i].wall&&tmp.vis == flag[tmp.x][tmp.y][tmp.bomb][i].vis)
			return false;
	return true;
}

int bfs() {
	priority_queue<Node>q;
	q.push(s);
	while (!q.empty())
	{
		u = q.top();
		q.pop();
		for (int i = 0; i < 4; i++) {
			v = u;
			v.step++;
			v.x = u.x + p[i][0];
			v.y = u.y + p[i][1];
			if (v.check())
			{
				if (mapp[v.x][v.y] == 'D')
					return v.step;
				if (mapp[v.x][v.y] == 'X' && ((1LL << wallmap[v.x][v.y])&v.wall))
				{
					if (v.bomb > 0)
					{
						v.bomb--;
						v.step++;
						v.wall ^= (1LL << wallmap[v.x][v.y]);
						q.push(v);
						flag[v.x][v.y][v.bomb].push_back(v);
					}
				}
				else if (mapp[v.x][v.y] >= '1'&&mapp[v.x][v.y] <= '9' && (v.vis&(1LL << bombmap[v.x][v.y])) == 0)
				{
					v.bomb += mapp[v.x][v.y] - '0';
					v.vis |= (1LL << bombmap[v.x][v.y]);
					q.push(v);
					flag[v.x][v.y][v.bomb].push_back(v);
				}
				else
				{
					if (flag[v.x][v.y][v.bomb].empty() || check(v))
					{
						flag[v.x][v.y][v.bomb].push_back(v);
						q.push(v);
					}
				}

			}
		}
	}
	return -1;
}

int main() {
	while (cin >> n >> m&&n&&m)
	{
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				for (int k = 0; k <= (n*m * 9); k++)
				{
					if (!flag[i][j][k].empty()) {
						flag[i][j][k].clear();
					}
				}
			}
		}
		bombcnt = wallcnt = 0;
		memset(wallmap, -1, sizeof(wallmap));
		memset(bombmap,-1,sizeof(bombmap));
		for (int i = 0; i < n; i++)
		{
			cin >> mapp[i];
			for (int j = 0; j < m; j++)
			{
				if (mapp[i][j] == 'S')
				{
					s.x = i;
					s.y = j;
					s.step = 0;
					s.bomb = 0;
					s.vis = 0;
				}
				else if (mapp[i][j] == 'X')
					wallmap[i][j] = wallcnt++;
				else if (mapp[i][j] >= '1'&&mapp[i][j] <= '9')
					bombmap[i][j] = bombcnt++;
			}
		}
		s.wall = (1LL << wallcnt) - 1;
		cout << bfs() << endl;
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章