[UVa1331]Minimax Triangulation

Problem Description

这里写图片描述

My Problem Report

过题以后又把代码读了两遍,上Udebug对拍了几发。简直不敢想象它是怎么A的。

My Source Code

//  Created by Chlerry in 2015.
//  Copyright (c) 2015 Chlerry. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define Size 100000
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array, x) memset(array,x,sizeof(array))
#define cp(x1,y1,x2,y2) x1*y2-x2*y1  //cross_product
#define S(i,j,k) abs(cp((x[k]-x[i]),(y[k]-y[i]),(x[k]-x[j]),(y[k]-y[j])))
typedef pair<int,int> P;
int T,n;
ll x[60],y[60],f[60][60];

bool Check(int i,int j,int k)
{
    i%=n;j%=n;k%=n;
    for(int p=0;p<n;p++)
        if(p==i || p==j || p==k)
            continue;
        else if(S(i,j,k)==S(i,j,p)+S(i,k,p)+S(j,k,p))
            return 0;
    return 1;
}
int main()
{
    freopen("in.txt","r",stdin);
    cin>>T;
while(T--)
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>x[i]>>y[i];
    x[n]=x[0],y[n]=y[0];
    for(int i=0;i+2<=n;i++)
    {
        ll vx1=x[i]-x[i+1],vy1=y[i]-y[i+1];
        ll vx2=x[i+2]-x[i+1],vy2=y[i+2]-y[i+1];
        f[i][i+2]=-cp(vx1,vy1,vx2,vy2);
        if(f[i][i+2]<0)
            f[i][i+2]=INT_MAX;
    }
    for(int i=n-3;i>=0;i--)
        for(int j=i+2;j<=n;j++)
        {
            f[i][j]=INT_MAX;
            for(int k=i+1;k<j;k++)
                if(Check(i,j,k))
                    f[i][j]=min( f[i][j],max( S(i,j,k),max(f[i][k],f[k][j]) ) );
        }
    printf("%.1f\n",(double)f[0][n]/2.0);
}
    return 0;
}
  • 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、付费专栏及课程。

余额充值