CDOJ 1134 男神的约会 状压dp

范围都很小,所以状压就可以,不会T
其实BFS队列也可以做,原理一样的

代码:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 99999999
int pow_2[10];
int grid[10][10];
int dp[1024][10][10];
int main()
{
	//freopen("input.txt", "r", stdin);
	pow_2[0] = 1;
	for (int i = 1; i < 10; ++i)
		pow_2[i] = pow_2[i - 1] << 1;
	for (int i = 0; i < 10; ++i)
		for (int j = 0; j < 10; ++j)
			for (int k = 0; k < 1024; ++k)
				dp[k][i][j] = INF;
	for (int i = 0; i < 10; ++i)
		for (int j = 0; j < 10; ++j)
			scanf("%d", &grid[i][j]);
	dp[pow_2[grid[0][0]]][0][0] = grid[0][0];
	int x = pow_2[grid[0][0]], y = pow_2[grid[0][0]];
	for (int i = 1; i < 10; ++i)
	{
		dp[x | pow_2[grid[i][0]]][i][0] = dp[x][i - 1][0] + grid[i][0];
		x |= pow_2[grid[i][0]];
		dp[y | pow_2[grid[0][i]]][0][i] = dp[y][0][i - 1] + grid[0][i];
		y |= pow_2[grid[0][i]];
	}
	for (int i = 1; i < 10; ++i)
		for (int j = 1; j < 10; ++j)
			for (int k = 0; k < 1024; ++k)
			{
				if (dp[k][i - 1][j]!=INF)
					dp[k | pow_2[grid[i][j]]][i][j] = min(dp[k][i - 1][j] + grid[i][j], dp[k | pow_2[grid[i][j]]][i][j]);
				if (dp[k][i][j - 1] != INF)
					dp[k | pow_2[grid[i][j]]][i][j] = min(dp[k][i][j - 1] + grid[i][j], dp[k | pow_2[grid[i][j]]][i][j]);
			}
	printf("%d\n", dp[1023][9][9]);
	//system("pause");
	//while (1);
	return 0;
}



你可能感兴趣的:(动态规划,CDOJ)