UVA1331 Minimax Triangulation (区间DP)

UVA1331 Minimax Triangulation (区间DP)

本题属于最优三角剖分问题,用dp(i,j)表示在编号为i的顶点和编号为j的顶点间的最大三角面积的最小值。

这样我们就可以得到状态转移方程
dp(i,j)=min(max(dp(i,k),dp(k,j),S(i,k,j))),其中i<k<j

知道三角形的三点坐标,可以用向量叉乘来求面积;
知道三角形的三边长,可以用海伦公式求面积;

本题需要注意的地方是,需要判断i-j是否是多边形的对角线(除了i=1且j=n的情况),我们可以通过面积判断在i,k,j点组成的三角形内是否有别的顶点。如果有则说明该划分是非法的,原因是:
1、划分的线段可能不是对角线
2、划分的线段与别的对角线相交

#include<bits/stdc++.h>
#define esp 1e-6
using namespace std;
struct Node{
	int x;
	int y;
}node[55];
int n;
double dp[55][55];
double S(int l,int k,int r)
{
	return fabs((node[l].x-node[k].x)*(node[r].y-node[k].y)-(node[l].y-node[k].y)*(node[r].x-node[k].x))*0.5;
}
bool judge(int l,int k,int r)//判断是否以l.k.r为顶点的三角形是否包含其他点 
{
	for(int i=1;i<=n;i++)
	{
		if(i==l||i==r||i==k)continue;
		double s=S(i,l,r)+S(i,l,k)+S(i,k,r);
		if(s-S(l,k,r)<esp)return true;
	}
	return false;
}
void solve(void)
{	
	memset(dp,0,sizeof(dp));
	for(int s=2;s<=n;s++)
	for(int l=1;l<=n;l++)
	{
		int r=l+s;
		if(r>n)continue;
		dp[l][r]=0x3f3f3f3f;
		for(int k=l+1;k<r;k++)
		{
			if(judge(l,k,r))continue;
			double MAX=max(dp[l][k],dp[k][r]);
			MAX=max(MAX,S(l,k,r));
//			 ncout<<S(l,k,r)<<" "<<dp[l][k]<<" "<<dp[k][r]<<" "<<dp[l][r]<<endl;
			dp[l][r]=min(dp[l][r],MAX);
//			cout<<l<<" "<<r<<" "<<dp[l][r]<<endl;
		}
	}
	printf("%.1f\n",dp[1][n]);
}
int main(void)
{
//	freopen("out.txt","w",stdout);
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		scanf("%d%d",&node[i].x,&node[i].y);
		solve();
	}
//	fclose(stdout);
}
//1
//6
//7 0
//6 2
//9 5
//3 5
//0 3
//1 1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Minimax is a popular algorithm used in game theory and artificial intelligence to determine the optimal move for a player in a game with perfect information. It is often used in games such as chess, tic-tac-toe, and Connect Four. In C#, you can implement the Minimax algorithm by representing the game state and creating a recursive function to search through all possible moves and evaluate their outcomes. Here's a simplified example of Minimax in C#: ```csharp public int MiniMax(int[] board, int depth, bool isMaximizingPlayer) { // Base case: check if the game is over or the maximum depth is reached if (IsGameOver(board) || depth == 0) { return Evaluate(board); } if (isMaximizingPlayer) { int bestScore = int.MinValue; foreach (int move in GetPossibleMoves(board)) { int[] newBoard = MakeMove(board, move); int score = MiniMax(newBoard, depth - 1, false); bestScore = Math.Max(bestScore, score); } return bestScore; } else { int bestScore = int.MaxValue; foreach (int move in GetPossibleMoves(board)) { int[] newBoard = MakeMove(board, move); int score = MiniMax(newBoard, depth - 1, true); bestScore = Math.Min(bestScore, score); } return bestScore; } } // Example usage: int[] board = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int bestMove = -1; int bestScore = int.MinValue; foreach (int move in GetPossibleMoves(board)) { int[] newBoard = MakeMove(board, move); int score = MiniMax(newBoard, depth, false); if (score > bestScore) { bestScore = score; bestMove = move; } } Console.WriteLine("Best move: " + bestMove); ``` This is a simplified example, and you would need to implement the `IsGameOver()`, `Evaluate()`, `GetPossibleMoves()`, and `MakeMove()` functions according to the rules of your specific game. The `depth` parameter controls the depth of the search tree, determining how far ahead the algorithm looks. Adjusting this parameter can affect the algorithm's performance and the quality of the decisions it makes.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值