HDU 4753 Fishhead’s Little Game(博弈搜索)

OJ题目:click here~~~

题目分析:九宫格,只利用九宫格的边。开始,边都没有添加。两个人轮流添加边,如果某人添加了某边之后,形成了x个新的1*1的方格,则此人得x分。边全部添加完后,得分高者获胜。已经进行到第n轮,问是先手赢,还是后手赢。


struct Point{
    int x ;
    int y ;
    Point(){}
    Point(int a ,int b):x(a) , y(b){}
};

bool edge_row[5][5] ;
bool edge_col[5][5] ;
bool tom_turn ;
int edge_num ;
int tom_score , jerry_score ;

Point get_point(int x){
    return Point((x - 1)/4 , (x - 1)%4) ;
}

void add_edge(Point p1 , Point p2 , bool value){
    if(p1.x > p2.x || p1.y > p2.y)
        swap(p1 , p2) ;
    if(p1.x == p2.x)
        edge_row[p1.x][p1.y] = value ;
    else
        edge_col[p1.x][p1.y] = value ;
}

int squre(Point a){
    if(a.x < 0 || a.x > 2 || a.y < 0 || a.y > 2) return 0 ;
    bool r = true ;
    r = r&&edge_row[a.x][a.y] ;
    r = r&&edge_row[a.x + 1][a.y] ;
    r = r&&edge_col[a.x][a.y] ;
    r = r&&edge_col[a.x][a.y + 1] ;
    if(r) return 1;
    else return 0 ;
}

int get_score(Point a , Point b){
    if(a.x > b.x || a.y > b.y)
        swap(a , b) ;
    if(a.x == b.x)
        return squre(Point(a.x - 1 , a.y)) + squre(a) ;
    return squre(Point(a.x , a.y - 1)) + squre(a) ;
}


void input(){
    tom_turn = true ;
    tom_score = jerry_score = 0 ;
    int i , a , b ;
    scanf("%d",&edge_num) ;
    memset(edge_row , 0 , sizeof(edge_row)) ;
    memset(edge_col , 0 , sizeof(edge_col)) ;
    for(i = 0;i < edge_num;i++){
        scanf("%d%d",&a ,&b) ;
        Point p1 = get_point(a) ;
        Point p2 = get_point(b) ;
        add_edge(p1 , p2 , true) ;
        if(tom_turn)
            tom_score += get_score(p1 , p2) ;
        else
            jerry_score += get_score(p1 , p2) ;
        tom_turn = !tom_turn ;
    }

}


bool dfs(int now , int pre , int turn){
    bool win = false ;
    bool did = false ;
    for(int i = 0;i < 4;i++){
        for(int j = 0;j < 3;j++){
            if(!edge_row[i][j]){
                did = true ;
                Point a = Point(i , j) ;
                Point b = Point(i , j + 1) ;
                add_edge(a , b , true) ;
                int s = get_score(a , b) ;
                win = !dfs(pre , now + s , !turn) ;
                add_edge(a , b , false) ;
                if(win) return true ;
            }

            if(!edge_col[j][i]){
                did = true ;
                Point a = Point(j , i) ;
                Point b = Point(j + 1 , i) ;
                add_edge(a , b , true) ;
                int s = get_score(a , b) ;
                win = !dfs(pre , now + s , !turn) ;
                add_edge(a , b , false) ;
                if(win) return true ;
            }
        }
    }
    if(!did){
        if(now == pre)
            return !tom_turn ;
        return now > pre ;
    }
    return false ;
}
int main(){
    //freopen("in.txt" , "r" , stdin) ;
    int Case ;
    cin >> Case ;
    for(int i = 1;i <= Case;i++){
        input();
        bool tom_win ;
        if(tom_turn)
            tom_win = dfs(tom_score , jerry_score , tom_turn) ;
        else
            tom_win = !dfs(jerry_score, tom_score , !tom_turn) ;
        printf("Case #%d: ", i);
        if (tom_win)
            printf("Tom200\n");
        else
            printf("Jerry404\n");
    }
    return 0 ;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值