计算机学院大学生程序设计竞赛(2015’12)Pick Game

Pick Game

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 119    Accepted Submission(s): 20


Problem Description
This is a pick game.
On a n*m matrix, each gird has a value. The player could only choose the gird that is adjacent to at least two empty girds (A grid outside the matrix also regard as empty). Adjacent means two girds share a common edge. If one play chooses one gird, he will get the value and the gird will be empty. They play in turn.

One day, WKC plays this game with ZJS. 
Both of them are clever students, so they will choose the best strategy. 
WKC plays first, and he wants to know the maximal value he could get.
 

Input
There is a positive integer T(1<=T<=50) in the first line, which specifying the number of test cases to follow.
Each test case begins with two numbers n and m ( 2 <= n, m <= 5 ).
Then n lines follow and each lines with m numbers V ij (0< V ij <=1000).
 

Output
Output the maximal value WKC could get.
 

Sample Input
  
  
1 2 2 9 8 7 6
 

Sample Output
  
  
16
 

总是望着曾经的空间发呆,那些说好不分开的朋友不在了,转身,陌路。 熟悉的,安静了, 安静的,离开了, 离开的,陌生了, 陌生的,消失了, 消失的,陌路了。 快哭了

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define mod 10007
#define N 10010
#include<vector>
using namespace std;
vector<int> v[N],dp[N];
int n,m,cnt,Map[6][6];
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
bool ok(int x,int y)
{
    if(x<n&&x>=0&&y<m&&y>=0)
        return true;
    return false;
}
int Gets(int st)
{
    int ret=0;
    for(int i=0; i<cnt; i++)
    {
        if(((1<<i)&st)==0)
        {
            ret+=Map[i/m][i%m];
        }
    }
    return ret;
}
int cal(int st)
{
    int ret=0,i;
    for(i=0; i<cnt; i++)
        if((1<<i)&st)
            ret++;
    return ret;
}
int dfs(int st)
{
    int next,ans,i,j,tmp,x,y,xx,yy;
    x=st%mod;
    for(y=v[x].size()-1; y>=0; y--)
    {
        if(v[x][y]==st)
            return dp[x][y];
    }
    if(cal(st)==cnt-1)
        return Gets(st);
    ans=-20006;
    for(i=0; i<cnt; i++)
    {
        if(((1<<i)&st)==0)
        {
            x=i/m;
            y=i%m;
            tmp=0;
            for(j=0; j<4; j++)
            {
                xx=x+dir[j][0];
                yy=y+dir[j][1];
                if(!ok(xx,yy)||(ok(xx,yy)&&(((1<<(xx*m+yy))&st))))
                {
                    tmp++;
                }
            }
            if(tmp>=2)
            {
                next=(1<<i) | st;
                ans=max(ans,Map[x][y]-dfs(next));
            }
        }
    }
    v[st%mod].push_back(st);
    dp[st%mod].push_back(ans);
    return ans;
}
int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--)
    {
        for(i=0; i<mod; i++)
        {
            dp[i].clear();
            v[i].clear();
        }
        scanf("%d%d",&n,&m);
        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
                scanf("%d",&Map[i][j]);
        cnt=n*m;
        printf("%d\n",(dfs(0)+Gets(0))/2);
    }
    return 0;
}

@执念  "@☆但求“❤”安★ 下次我们做的一定会更好。。。。吐舌头

为什么这次的题目是英文的。。。。QAQ... 哭

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是基于 Python 的哲学家进餐模拟程序设计。 ```python import threading import time class Philosopher(threading.Thread): def __init__(self, philosopher_id, left_fork, right_fork): threading.Thread.__init__(self) self.philosopher_id = philosopher_id self.left_fork = left_fork self.right_fork = right_fork def run(self): while True: self.left_fork.pick_up() time.sleep(1) if not self.right_fork.pick_up(): self.left_fork.put_down() time.sleep(1) continue print("Philosopher ", self.philosopher_id, " is eating.") time.sleep(1) self.right_fork.put_down() self.left_fork.put_down() print("Philosopher ", self.philosopher_id, " is thinking.") time.sleep(1) if __name__ == '__main__': n_philosophers = 5 forks = [threading.Lock() for _ in range(n_philosophers)] philosophers = [Philosopher(i, forks[i], forks[(i+1)%n_philosophers]) for i in range(n_philosophers)] for philosopher in philosophers: philosopher.start() ``` 这个程序中,我们首先定义了 `Philosopher` 类,它继承自 `threading.Thread` 类,每个哲学家通过 `left_fork` 和 `right_fork` 两个锁来控制自己的左右两个 fork。在 `run` 方法中,每个哲学家都会不断地尝试拿起左边的 fork,如果左边的 fork 没有被拿走,就等待一段时间,然后再次尝试;如果左边的 fork 被拿走了,就接着尝试拿起右边的 fork,如果右边的 fork 没有被拿走,就先放下左边的 fork,等待一段时间后再次尝试;如果右边的 fork 被拿走了,就开始进餐,并输出哲学家的 ID。在进餐完成后,哲学家会先放下右边的 fork,再放下左边的 fork,然后开始思考。 在主程序中,我们首先创建了 `n_philosophers` 个锁对象 `forks`,每个哲学家的左边的 fork 为 `forks[i]`,右边的 fork 为 `forks[(i+1)%n_philosophers]`。然后,我们创建了 `n_philosophers` 个 `Philosopher` 对象,并将它们全部启动。 这个程序中,我们使用了 `time.sleep` 方法来模拟哲学家的行为,让它们在某些情况下等待一段时间再继续执行。同时,我们也使用了 `threading.Lock` 类来实现锁的功能,避免哲学家之间互相干扰。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值