1470. The Game Of Take Numbers

本文介绍了一种基于数组的游戏算法,两人轮流从数组两端取数,目标是使所取数值之和最大化。通过记忆化搜索实现最优解策略,确保先手玩家得分优势。

这道题给我们一个数组,让两个人轮流从数组的两边拿数,最后谁有的数的和最多谁就赢,如果先拿的那个大于等于第二个拿个返回1,否则返回2。


  解答:

      这道题属于博弈类问题,一般用记忆化搜索来做,递归定义是剩余[l,r]的时候先手最多能拿多少。

     1 如过先手拿arr[l],那么先手之后拿的是Math.min(search(arr, l + 2, r, map), search(arr,l + 1, r - 1, map));因为第二步的时候,后手一定会让先手拿小的。

     2 如过先手拿arr[r],那么先手之后拿的是 Math.min(search(arr, l + l, r - 1, map), search(arr,l, r - 2, map));因为第二步的时候,后手一定会让先手拿小的。

     3 那最后先手肯定取最大的

    public int theGameOfTakeNumbers(int[] arr) {
        if(arr.length % 2 == 0) return 1;
        int sum = 0;
        for  (int i : arr) {
            sum += i;
        }
        Map<String, Integer> map = new HashMap<>();  
        return search(arr, 0, arr.length - 1, map) * 2 >= sum ? 1 : 2;
    }
    private int search(int[] arr, int l, int r,  Map<String, Integer> map) {
        if (l > r) {
            return 0;
        }
        if (l == r) return arr[l];
        String str = l + ":" + r;
        if (map.containsKey(str)) {
            return map.get(str);
        }
        int max = 0;
        if (l + 1 == r){
            max = Math.max(arr[l], arr[r]);
        } else {
            int left = Math.min(search(arr, l + 2, r, map), search(arr,l + 1, r - 1, map)) + arr[l];
            int right = Math.min(search(arr, l + l, r - 1, map), search(arr,l, r - 2, map)) + arr[r];
            max = Math.max(left, right);
        }
        return max;
    }

c++题解,代码没有注释,可从上网查询,输出必须符合样例,本题有8个测试点,请得满分。 T-2 Breaking Through 分数 35 作者 陈越 单位 浙江大学 In a war game, you are given a map consists of n×n square blocks. Starting from your current block, your task is to conquer a destination block as fast as you can. The difficulties are not only that some of the blocks are occupied by enemies, but also that they are shooting in some directions. When one is shooting in some direction, the whole row/column in that direction will be covered by fire, unless there is another enemy blocking the way. You must make sure that you are not shot on the way to your destination. However, it is very much likely, at the very beginning, that this task is impossible. So you must follow the following instructions: Step 1: find the shortest path from the starting block to the destination, avoiding all the enemy blocks. The path length is the number of blocks on the way, not including the starting block. If this path is not unique, select the smallest index sequence – that is, all the blocks are indexed from 1 to n 2 , starting from the upper-left corner, and row by row till the lower-right corner. An index sequence is an ordered sequence of the block indices from the starting block to the destination. One sequence { s,u 1 ​ ,⋯,u k ​ ,d } is said to be smaller than { s,v 1 ​ ,⋯,v k ​ ,d }, if there exists 1≤p≤k such that u i ​ =v i ​ for i<p and u p ​ <v p ​ . Step 2: if some of the blocks along the selected shortest path is covered by fire, conquer and clear the nearest reachable enemy block that are firing at the path (in case that such a block is not unique, take the one with the smallest index). Then take that block as the starting position, goto step 1. Keep in mind that you must always make sure that you are not shot on the way. If step 2 is impossible, then the game is over. Otherwise, keep going until you finally conquer the destination. Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer n (3≤n≤100), which is the size of the map. Then n lines follow, each gives n numbers. Each number describes the status of the corresponding block: 0 means the block is clear; 1, 2, 3, or 4 means the block is occupied by an enemy, who will shoot toward up, down, left, and right, respectively; 5 means the block is your starting position; 6 means the block is your destination. All the numbers in a line are separated by a space. Note: You can never cross any of the boundaries of the map. When an enemy is shooting from block A in direction B, every block starting from A in direction B will be covered by fire, untill the boundary is reached, or another enemy block is encountered. The enemies will never kill each other. Only you will get killed if you step into a block that is covered by fire. It is guaranteed that there is no more than n enemy blocks, and your starting position is not on fire. Output Specification: Print in a line the path length for you to reach the last block from your starting position, and the block number. The two numbers must be separated by 1 space. In the next line, print Win if the destination is reached, or Lose if not. Sample Input 1: 6 6 2 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 1 0 0 0 0 3 5 0 4 0 0 0 0 0 Sample Output 1: 10 1 Win Hint: The movements are shown by the following figures 14. Figure 1 shows how the blocks are numbered. Figure 2 shows the initial status of the map, where the player’s position is green, enemy blocks are black, and red blocks are covered by fire. At the very beginning the shortest path was 29->30->24->18->12->11->10->9->8->7->1, but the path was covered by fire shooting from blocks 23, 14 and 2. Since the enemy block 23 is the nearest reachable one, it was cleared with path length 1. Now starting from block 23, the shortest path was 23->17->11->10->9->8->7->1, but the path was covered by fire shooting from blocks 14 and 2. Since the enemy block 14 is covered by fire from block 2, taking block 2 now is the only option. So next, block 2 is cleared with path length 8 (crossing the destination). Finally we can get to the destination from block 2 to 1. Sample Input 2: 4 5 0 2 0 0 0 0 0 0 1 0 0 4 0 6 3 Sample Output 2: 6 3 Lose Hint: The movements are shown by the following figures 5~8. Figure 5 shows how the blocks are numbered. Figure 6 shows the initial status of the map. At the very beginning the shortest path was 1->2->6->7->11->15, but the path was covered by fire shooting from blocks 3, 10, 13 and 16. Since the enemy block 10 is the nearest reachable one, it was cleared with path length 3. Now starting from block 10, the shortest path was 10->11->15, but the path was covered by fire shooting from blocks 3, 13 and 16. Since the enemy blocks 13 and 16 are covered by fire from each other, taking block 3 now is the only option. So next, block 3 is cleared with path length 3. Now it is clear that there is no way to conquer the destination, since block 15 is fired by 13 and 16, yet we cannot clear any of them without getting fired. Hence the last block we can reach is 3. steps2.png 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB
最新发布
08-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值