现在的位置: 首页 > 综合 > 正文

hdu2262——Where is the canteen

2019年02月17日 ⁄ 综合 ⁄ 共 4090字 ⁄ 字号 评论关闭

Where is the canteen

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1143    Accepted Submission(s): 327

Problem Description
After a long drastic struggle with himself, LL decide to go for some snack at last. But when steping out of the dormitory, he found a serious problem : he can't remember where is the canteen... Even worse is the campus is very dark
at night. So, each time he move, he check front, back, left and right to see which of those four adjacent squares are free, and randomly walk to one of the free squares until landing on a canteen.
 

Input
Each case begin with two integers n and m ( n<=15,m<=15 ), which indicate the size of the campus. Then n line follow, each contain m characters to describe the map. There are 4 different type of area in the map:

'@' is the start location. There is exactly one in each case.
'#' is an impassible square.
'$' is a canteen. There may be more than one in the campus.
'.' is a free square.

 

Output
Output the expected number of moves required to reach a canteen, which accurate to 6 fractional digits. If it is impossible , output -1.
 

Sample Input
1 2 @22@.. 1 3 @#$
 

Sample Output
1.000000 4.000000 -1
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2264 2259 2373 2257 2247 
 

概率dp 首先设 dp[i] 表示在位置i时,到达食堂需要的步数的期望值
dp[i] = (dp[x1] + ... + dp[xn]) / n + 1;  x1, x2, ...xn是i可以到达的点
 整理得
 dp[x1] + dp[x2] + .... + dp[xn] - n * dp[i] = -n;
于是对于所有的点都可以建立这样一个方程,而且存在环, 所以考虑用高斯消元
首先bfs下,然后去掉到不了的点,接着高消就行了

/*************************************************************************
    > File Name: hdu2262.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2014年12月27日 星期六 14时22分14秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const double eps = 1e-12;

int n, m;
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
bool flag[20][20];
double mat[300][300];
double x[300];
char str[20][20];
bool free_x[300];

struct node
{
	int x, y;
};

bool is_inmat(int x, int y)
{
	if (x < 0 || y < 0 || x >= n || y >= m)
	{
		return false;
	}
	return true;
}

int sgn(double x)
{
	return (x > eps) - (x < -eps);
}

int gauss()
{
	int  k, max_r, col;
	int free_num = 0, free_index;
	int equ = n * m, var = n * m;
	memset (free_x, true, sizeof(free_x));
	for (k = col = 0; k < equ && col < var; ++k, ++col)
	{
		max_r = k;
		for (int i = k + 1; i < equ; ++i)
		{
			if (sgn(fabs(mat[i][col]) - fabs(mat[max_r][col])) > 0)
			{
				max_r = i;
			}
		}
		if (max_r != k)
		{
			for (int i = k; i < var + 1; ++i)
			{
				swap(mat[max_r][i], mat[k][i]);
			}
		}
		if (sgn(mat[k][col]) == 0)
		{
			--k;
			continue;
		}
		for (int i = k + 1; i < equ; ++i)
		{
			if (sgn(mat[i][col]) != 0)
			{
				double tx = (mat[i][col] / mat[k][col]);
				for (int j = col; j < var + 1; ++j)
				{
					mat[i][j] = mat[i][j] - tx * mat[k][j];
				}
			}
		}
	}
	for (int i = k; i < equ; ++i)
	{
		if (sgn(mat[i][col]) != 0)
		{
			return 0;
		}
	}
	if (k < var)
	{
		for (int i = k - 1; i >= 0; --i)
		{
			free_num = 0;
			for (int j = 0; j < var; ++j)
			{
				if (sgn(mat[i][j]) != 0 && free_x[j])
				{
					free_num++;
					free_index = j;
				}
			}
			if (free_num > 1)
			{
				continue;
			}
			double tmp = mat[i][var];
			for (int j = 0; j < var; ++j)
			{
				if (sgn(mat[i][j]) != 0 && j != free_index)
				{
					tmp -= mat[i][j] * x[j];
				}
			}
			x[free_index] = tmp / mat[i][free_index];
			free_x[free_index] = 0;
		}
		return var - k;
	}
	for (int i = var - 1; i >= 0; --i) //唯一解,回代
	{
		double tmp = mat[i][var];
		for (int j = i + 1; j < var; ++j)
		{
			if (sgn(mat[i][j]) != 0)
			{
				tmp -= mat[i][j] * x[j];
			}
		}
		x[i] = tmp / mat[i][i];
	}
	return 1;
}

void bfs()
{
	queue <node> qu;
	node tmp1, tmp2;
	while (!qu.empty())
	{
		qu.pop();
	}
	memset (flag, false, sizeof(flag));
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			if (str[i][j] == '$')
			{
				tmp1.x = i;
				tmp1.y = j;
				qu.push(tmp1);
				flag[i][j] = 1;
			}
		}
	}
	while (!qu.empty())
	{
		tmp1 = qu.front();
		qu.pop();
		for (int i = 0; i < 4; ++i)
		{
			tmp2.x = tmp1.x + dir[i][0];
			tmp2.y = tmp1.y + dir[i][1];
			if (!is_inmat(tmp2.x, tmp2.y))
			{
				continue;
			}
			if (str[tmp2.x][tmp2.y] == '#')
			{
				continue;
			}
			if (!flag[tmp2.x][tmp2.y])
			{
				flag[tmp2.x][tmp2.y] = 1;
				qu.push(tmp2);
			}
		}
	}
}

int main()
{
	int sx, sy;
	while (~scanf("%d%d", &n, &m))
	{
		for (int i = 0; i < n; ++i)
		{
			scanf("%s", str[i]);
			for (int j = 0; j < m; ++j)
			{
				if (str[i][j] == '@')
				{
					sx = i;
					sy = j;
					break;
				}
			}
		}
		memset (mat, 0, sizeof(mat));
		bfs();
		if (!flag[sx][sy])
		{
			printf("-1\n");
			continue;
		}
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < m; ++j)
			{
				int cnt = 0;
				if (str[i][j] == '#')
				{
					continue;
				}
				if (str[i][j] == '$')
				{
					mat[i * m + j][i * m + j] = 1;
					mat[i * m + j][n * m] = 0;
				}
				else
				{
					for (int k = 0; k < 4; ++k)
					{
						int xx = i + dir[k][0];
						int yy = j + dir[k][1];
						if (!is_inmat(xx, yy) || !flag[xx][yy])
						{
							continue;
						}
						mat[i * m + j][xx * m + yy] = 1;
						cnt++;
					}
					mat[i * m + j][i * m + j] = -1.0 * cnt;
					mat[i * m + j][n * m] = -1.0 * cnt;
				}
			}
		}
		if (gauss())
		{
			printf("%.6f\n", x[sx * m + sy]);
			continue;
		}
		printf("-1\n");
	}
}

抱歉!评论已关闭.