back to the menu

Flag Counter TopCoder SRM 558 - 1000: SurroundingGame

SRM 558 1 1000 SurroundingGame


Problem Statement

题目简述

一个人在一个n * m棋盘上玩游戏,想要占领一个格子有两个方法:

在这个格子放一个棋子。

这个格子周围(四联通)的格子中**都有棋子**。

在(i, j)中放棋子需要花费cost[i][j],占领(i, j)能获得benefit[i][j]。求一种放置棋子的方法,使得总收益(收益 - 花费)最大。

2<=n,m<=20

Surrounding Game is a single-player game played on a rectangular grid of cells. Cells are considered adjacent if they share a common side. (Hence, each cell has at most four adjacent cells. The cells on the sides and in the corners of the grid have fewer adjacent cells than the ones inside the grid.)

The game is played by placing stones into some of the cells. Each cell may only contain at most one stone. A cell is called dominated if at least one of the following two conditions holds:
  • The cell contains a stone.
  • All cells adjacent to the cell contain stones.

Each cell of the grid contains two numbers, each from 0 to 61, inclusive: the cost of placing a stone into the cell, and the benefit from dominating the cell. At the end of the game, the overall score of the player is the sum of all benefits minus the sum of all costs.

You are given the String[]s cost and benefit. The characters cost[i][j] and benefit[i][j] represent the two numbers written in the cell (i,j), using the following encoding:
  • Characters '0'-'9' represent numbers 0-9.
  • Characters 'a'-'z' represent numbers 10-35.
  • Characters 'A'-'Z' represent numbers 36-61.

For example, if character 7 of element 4 of cost is 'B', the cost of placing a stone into the cell (4,7) is 37.

Calculate and return the maximal possible score for the given grid.

Definition

(be sure your method is public)

Limits

Constraints

Test cases

  1. input
    • cost {"21",
       "12"}
    • benefit {"21",
       "12"}
    output
    Returns 4
    note
    The optimal solution is to put stones into (0,1) and (1,0). All the cells will be dominated and the overall score will be 6 - 2 = 4.
  2. input
    • cost {"ZZ",
       "ZZ"}
    • benefit {"11",
       "11"}
    output
    Returns 0
    note
    The optimal solution is to put no stones. It is impossible to get a positive score.
  3. input
    • cost {"XXX",
       "XXX",
       "XXX"}
    • benefit {"aaa",
       "aZa",
       "aaa"}
    output
    Returns 2
    note
    The optimal solution is to put a stone into (1,1).
  4. input
    • cost {"asam",
       "atik"}
    • benefit {"123A",
       "45BC"}
    output
    Returns 71
  5. input
    • cost {"IIIIIIII",
       "IIWWWWII",
       "IIWIIIII",
       "IIWIIIII",
       "IIWWWWII",
       "IIIIIWII",
       "IIIIIWII",
       "IIWWWWII",
       "IIIIIIII"}
    • benefit {"IIIIIIII",
       "II0000II",
       "II0II0II",
       "II0II0II",
       "II0000II",
       "II0II0II",
       "II0II0II",
       "II0000II",
       "IIIIIIII"}
    output
    Returns 606

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.