Problem:


Problem D: Indian Puzzle

In the Indian Puzzle, one is intended to fill out blanks with numbers and operators in a n by n grid in order to make equalities in the grid true (See Figure 1).

Figure 1

A blank cell should be filled out with a number (from 0 to 9 inclusive) or an operator (+, -, &times;, &divide;, =), and black cells split equalities. Initially, some cells are already filled with numbers or operators.

The objective of this puzzle is to fill out all the blank cells with numbers (from 0 to 9 inclusive) and operators (+, -, &times;, &divide;) in a given list. All the numbers and operators in the list must be used to fill out the blank cells.

A equality is organized by more than 2 consecutive left-to-right or top-to-bottom white cells. You can assume that a equality contains exactly one cell with '=' which connects two expressions.

The expressions conform to the order of operations and semantics of conventional four arithmetic operations. First, do all multiplication and division, starting from the left (top). Then, do all addition and subtraction, starting from the left (top). In addition, this puzzle has the following rules:

Division by zero and division leaving a remainder, are not allowed.

Leading zeros are not allowed.

Unary operators like 3&times;-8=-24 are not allowed.

In a formal description, the equality must conform the syntax in the following BNF:

<Eq> ::= <Ex> = <Ex>
<Ex> ::= <N0> | <Ex> <Op> <N0> 
<N0> ::= <N> | 0
<N>  ::= <D> | <N&gt <D> | <N> 0
<Op> ::= + | - | &times; | &divide;
<D>  ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Of course, puzzle creators must make solvable puzzle.
Your task is to write a program which verifies whether given puzzle is solvable or not.

Input

Input consists of several datasets. Each dataset consists of:

H W
H &times; W characters
n
n characters

The integers H and W are the numbers of rows and columns of the grid, respectively. H &times; W characters denote the grid which contains '.' representing a blank cell, '#' representing a black cell, numbers (from 0 to 9 inclusive), operators('+','-', '*', '/', '=')(&times; is represented by '*' and &divide; is represented by '/').

The integer n is the number of characters in the list. The last line of a dataset contains n characters indicating numbers and operators for the blank cells.

The end of input is indicated by a line containing two zeros separated by a space.

Output

For each dataset, print "Yes" if the given puzzle is solvable, "No" if not.

Constraints

Judge data includes at most 100 data sets.

H, W ≤ 10

n ≤ 10

Sample Input

5 5
4=..2
+#=#+
.-2=.
=#*#=
.-.=3
6
7 3 1 4 / 8
1 6
8..3=2
2
2 +
0 0

Output for the Sample Input

Yes
No



Sample input:
5 5
4=..2
+#=#+
.-2=.
=#*#=
.-.=3
6
7 3 1 4 / 8
1 6
8..3=2
2
2 +
0 0


Sample output:
Yes
No

Submission status: Time Limit Exceeded

Solution code:
import itertools
def myeval(st):
    if(not (str(st[0]).isdigit())):
        throw()
    ok=True
    for i in range(len(st)-1):
        if((not(str(st[0]).isdigit()))and (not (str(st[i+1]).isdigit()))):
            ok=False
    if ok:
        return eval(st)
    else:
        throw()


    
def formula(st):
    if(st==""):return True
    if(len(st)==1):return True
    sts=st.split("=")
    if len(sts)==2:
        try:
            a=myeval(sts[0])
            b=myeval(sts[1])
            return a==b
        except:
            return False
    else :
        return False

def check(field,H,W):
    for y in range(H):
        row=""
        for x in range(W):
            row+=str(field[y][x])
        sts=row.split("#")
        for st in sts:
            if (not formula(st)):
                return False
            else:
                a=2

    for i in range(W):
        col=""
        for y in range(H):
            col+=str(field[y][x])
        sts=col.split("#")
        for st in sts:
            if (not formula(st)):return False
    return True


while True:

    H,W=map(int,input().split())
    if ( H==0):
        break
    field=[]
    for i in range(H):
        st=input()
        aa=[]
        for x in range(W):
            aa.append(st[x])

        field.append(aa)
    N=int(input())
    nums=list(input().split())
    unknowns=[]
    for y in range(H):
        for x in range(W):
            if(field[y][x]=='.'):
                aa=[x,y]
                unknowns.append([x,y])
    ok=False
    for x in itertools.permutations(nums):
        nowfield=field[:]
        for i in range(N):
            nowfield[unknowns[i][1]][unknowns[i][0]]=x[i]
        if check(nowfield,H,W):
            ok=True
    if ok:
        print("Yes")
    else:
        print("No")