1. 程式人生 > >(HDOJ3309)Roll The Cube-BFS

(HDOJ3309)Roll The Cube-BFS

Roll The Cube

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 777    Accepted Submission(s): 326  

Problem Description

This is a simple game.The goal of the game is to roll two balls to two holes each. 'B' -- ball 'H' -- hole '.' -- land '*' -- wall Remember when a ball rolls into a hole, they(the ball and the hole) disappeared, that is , 'H' + 'B' = '.'. Now you are controlling two balls at the same time.Up, down , left , right --- once one of these keys is pressed, balls exist roll to that direction, for example , you pressed up , two balls both roll up. A ball will stay where it is if its next point is a wall, and balls can't be overlap. Your code should give the minimun times you press the keys to achieve the goal.

Input

First there's an integer T(T<=100) indicating the case number. Then T blocks , each block has two integers n , m (n , m <= 22) indicating size of the map. Then n lines each with m characters. There'll always be two balls(B) and two holes(H) in a map. The boundary of the map is always walls(*).

Output

The minimum times you press to achieve the goal. Tell me "Sorry , sir , my poor program fails to get an answer." if you can never achieve the goal.

Sample Input

4 6 3 *** *B* *B* *H* *H* *** 4 4 **** *BB* *HH* **** 4 4 **** *BH* *HB* **** 5 6 ****** *.BB** *.H*H* *..*.* ******

Sample Output

3 1 2 Sorry , sir , my poor program fails to get an answer.

題目大意:

兩個球,可以向四周滾動,問最少的時間將兩個球滾到兩個洞裡去。

題目分析:我遇到的搞不清的點就是怎麼判斷球前是否有球,後來發現這道題和HDOJ-2821 Pusher這道題有相同的處理,就是先走然後判斷,滿足條件則做後續的處理否則跳過,不做處理。還有一個點就是queue中的Node放什麼,這裡放兩小球和倆洞的座標,還有倆小球和洞的狀態(至於為啥,我不太清楚)。這裡用一個四維的vis陣列做標記,是因為一個小球訪問當前位置,另一個小球可能在除了當前位置的任何位置,用兩個二維陣列分別標誌兩個小球是不可以的。

程式碼:

#include<iostream>
#include<queue>
#define maxn 25
using namespace std;
int n, m, ans;
int sx[2], sy[2];
int dx[] = { -1,1,0,0 };
int dy[] = {0,0,-1,1};
char mp[maxn][maxn];
char s[maxn];
bool vis[maxn][maxn][maxn][maxn];

struct Node
{
	int x[2], y[2], step;
	int b[2], h[2];
}cur,now;
queue<Node> q;

bool bfs() {
	int i, j, t, flag;
	memset(vis, 0, sizeof(vis));
	while (!q.empty())q.pop();
	cur.x[0] = sx[0], cur.y[0] = sy[0];
	cur.x[1] = sx[1], cur.y[1] = sy[1];
	cur.h[0] = cur.h[1] = 0;
	cur.b[0] = cur.b[1] = 0;
	cur.step = 0;
	vis[sx[0]][sy[0]][sx[1]][sy[1]] = 1;
	q.push(cur);
	while (!q.empty())
	{
		now = q.front();
		q.pop();
		for (i = 0; i < 4; i++)
		{
			cur = now;
			//兩個球
			for (j = 0; j < 2; j++)
			{
				//如果進洞,則跳過
				if (cur.b[j])continue;
				cur.x[j] += dx[i];
				cur.y[j] += dy[i];
				if (mp[cur.x[j]][cur.y[j]] == '*')
				{
					cur.x[j] -= dx[i];
					cur.y[j] -= dy[i];
				}
			}
			//如果當前點走過或當前位置有小球就跳過
			if (vis[cur.x[0]][cur.y[0]][cur.x[1]][cur.y[1]] || cur.x[0] == cur.x[1] && cur.y[0] == cur.y[1] && cur.b[0] + cur.b[1] == 0)continue;
			cur.step++;
			vis[cur.x[0]][cur.y[0]][cur.x[1]][cur.y[1]] = 1;
			flag = 1;
			for (j = 0; j < 2; j++) {
				t = mp[cur.x[j]][cur.y[j]];
				//t<2說明當前為洞,!cur.h[t]說明當前的洞沒有被填上
				if (t < 2 && !cur.h[t])cur.b[j] = 1, cur.h[t] = 1;
				//只要有一個洞沒被添上flag就等於0
				if (!cur.b[j])flag = 0;
			}
			//兩個洞都被添上
			if (flag)
			{
				ans = cur.step;
				return true;
			}
			q.push(cur);
		}
	}
	return false;
}

int main() {
	int i, j, t, cnt1, cnt2;
	cin >> t;
	while (t--)
	{
		cin >> n >> m;
		cnt1 = cnt2 = 0;
		for (i = 1; i <= n; i++)
		{
			cin >> s;
			for (j = 1; j <= m; j++)
			{
				mp[i][j] = s[j - 1];
				//洞用小球代替
				if (mp[i][j] == 'H')mp[i][j] = cnt1++;
				else if (mp[i][j] == 'B')sx[cnt2] = i, sy[cnt2] = j, cnt2++;
			}
		}
		if (bfs())cout << ans << endl;
		else cout << "Sorry , sir , my poor program fails to get an answer." << endl;
	}
	return 0;
}