logo AlgoBeat OnlineJudge
登录 注册

#213561. [GCJ 2009 #1B] Square Math

内存限制:1024 MiB 时间限制:12000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Say we have a square that has cells on each side and, therefore, cells total. Let's go further and fill each cell with one of the following:

  • A digit from 0 to 9;
  • The addition sign (+);
  • The subtraction sign (-).

If, finally, we add a constraint that no 2 digits are horizontally or vertically adjacent and no 2 operators (+ or -) are horizontally or vertically adjacent, then our square can be called an "arithmetic square".

Square Math is the name of a puzzle where, given an arithmetic square, we start from any numeric cell and move either horizontally or vertically a cell at a time, finally ending in a numerical cell. The mathematical expression we get from the traversal is evaluated to get a single value. For example:

2+3
+4-
1+0

The above is a valid arithmetic square of size . If we start from "2", move horizontally right, then vertically down, we'll get "2+4", which gives a value of "6". If we further move horizontally right, then vertically up, we'll get "2+4-3", which is equal to "3".

In Square Math, there is no limit to how many times you can use a particular cell. It is perfectly legal to move from a cell to its neighbor, then back to the original cell. Given an arithmetic square and a list of queries, your task is to find a Square Math expression which evaluates to each query.

输入格式

The first line of input contains a single integer, . test cases follow. The first line of each test case contains 2 integers, and . lines follow, each containing characters, representing the arithmetic square. Don't worry, all arithmetic squares in the input are well-formed. The following line contains a space separated list of integers, representing the values which need to be computed by using Square Math (the queries). You can assume that all given values will have at least one possible Square Math solution.

输出格式

For each test case, begin output with "Case #:" on a line by itself, where is the test case number, starting from 1. Then, for each query within the test case, print the Square Math expression which evaluates to the query on a line by itself.

In the case where there are multiple possible Square Math expressions, print the one that is shortest. If there is still a tie, print the lexicographically smallest expression. Remember that '+' is lexicographically smaller than '-'.

样例

样例输入 1

2
5 3
2+1-2
+3-4+
5+2+1
-4-0-
9+5+1
20 30 40
3 2
2+1
+4+
5+1
2 20

样例输出 1

Case #1:
1+5+5+9
3+4+5+9+9
4+9+9+9+9
Case #2:
2
5+5+5+5

数据范围与提示

Limits

Small dataset

  • Time limit: 30 3 seconds.

Large dataset

  • Time limit: 60 12 seconds.