Language: C++

// AOJ 1022
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#define value first
#define p second
using namespace std;
typedef pair<int,int> result;
 
const int OutOfIntExc = 1,
          NegIntExc = 2,
          NotEquationExc = 3,
          NotNumExc = 4,
          LackNumExc = 5,
          IterExceedExc = 6;
 
result equation(const string &s, int p = 0);
result expression(const string &s, int p = 0);
result factor(const string &s, int p = 0);
result term(const string &s, int p = 0);
 
result equation(const string &s, int p) { // {{{
  if (s[p] == '=') { throw NotEquationExc; }
  result r = expression(s, p);
  if (s[r.p] != '=') { throw NotEquationExc; }
   
  r.p++;
  result t = expression(s, r.p);
  t.value = (int)(r.value == t.value);
   
  return t;
} // }}} 

result expression(const string &s, int p) { // {{{
  result r = factor(s, p);
  while (s[r.p] == '+' or s[r.p] == '-') {
    result r_ = factor(s, r.p+1);
    r.value += (s[r.p] == '+' ? r_.value : - r_.value);
    if (r.value < 0) { throw NegIntExc; }
    r.p = r_.p;
  }
  return r;
} // }}}

result factor(const string &s, int p) { // {{{
  result r = term(s, p);
  while (s[r.p] == '*' or s[r.p] == '/') {
    result r_ = term(s, r.p+1);
    if (s[r.p] == '*') {
      r.value *= r_.value;
    } else {
      if (r_.value == 0 or r.value % r_.value != 0) {
        throw OutOfIntExc;
      }
      r.value /= r_.value;
    }
    r.p = r_.p;
  }
  return r;
} // }}}
 
result term(const string &s, int p) { // {{{
  if (not isdigit(s[p])) { throw LackNumExc; }
  int value = 0;
  bool flag = false;
 
  while (isdigit(s[p])) {
    if (flag and value == 0) { throw NotNumExc; }
    flag = true;
    value = value * 10 + (s[p++] - '0');
  }
  return result(value, p);
} // }}}
 
#define REP(i,n) for(int i=0;i<(int)(n);i++)
int H, W, n;
const int HMAX = 200, WMAX = 200, NMAX = 200;
char TABLE[HMAX][WMAX];
bool PRINTED[HMAX][WMAX], HBEGIN[HMAX][WMAX], VBEGIN[HMAX][WMAX];
char BLOCK[NMAX];
pair<int,int> BLANKS[NMAX];
 
bool legal(int x, int y) {
  return 0 <= x and x < W and 0 <= y and y < H; 
}
 
string getv(int x, int y) { // {{{
  string str;
  while (legal(x, y) and TABLE[x][y] != '#') {
    str.push_back(TABLE[x][y]);
    y++;
  }
  return str;
} // }}}
 
string geth(int x, int y) { // {{{
  string str;
  while (legal(x, y) and TABLE[x][y] != '#') {
    str.push_back(TABLE[x][y]);
    x++;
  }
  return str;
} // }}}
 
bool is_operand(char c) { // {{{
  return c == '=' or c == '+' or c == '-' or c == '*' or c == '/';
} // }}}
 
bool is_illegal_operand() { // {{{
  REP(y, H) REP(x, W) {
    if (PRINTED[x][y]) continue;
    if (not is_operand(TABLE[x][y])) continue;
    const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
    REP(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (legal(nx, ny) and is_operand(TABLE[nx][ny])) {
        return true;
      }
    }
  }
 
  REP(y, H) REP(x,W) {
    if ((VBEGIN[x][y] or HBEGIN[x][y]) and is_operand(TABLE[x][y])) {
      return true;
    }
  }
  return false;
}
 
int hbadx, hbady, vbadx, vbady;
bool hbadflag, vbadflag;
string hbadeq, vbadeq;
 
bool solved() {
  if (hbadflag and geth(hbadx, hbady) == hbadeq) return false;
  if (vbadflag and geth(vbadx, vbady) == vbadeq) return false;
  REP(y, H) REP(x, W) {
    if (not HBEGIN[x][y]) continue;
    string eq = geth(x, y);
    try {
      result e = equation(eq);
      if (e.value == 0) {
        hbadflag = true;
        hbadx = x, hbady = y;
        hbadeq = eq;
        return false;
      }
    } catch (int ex) {
      hbadflag = true;
      hbadx = x, hbady = y;
      hbadeq = eq;
      return false;
    }
  }
 
  REP(y, H) REP(x, W) {
    if (not VBEGIN[x][y]) continue;
    string eq = getv(x, y);
    try {
      result e = equation(eq);
      if (e.value == 0) {
        vbadflag = true;
        vbadx = x, vbady = y;
        vbadeq = eq;
        return false;
      }
    } catch(int ex) {
      vbadflag = true;
      vbadx = x, vbady = y;
      vbadeq = eq;
      return false;
    }
  }
  return true;
} // }}}

bool solve() { // {{{
  sort(BLOCK, BLOCK + n);
  do {
    try {
      for (int i = 0; i < n; i++) {
        TABLE[BLANKS[i].first][BLANKS[i].second] = BLOCK[i];
        if (VSOLVABLE[i] and incorrect
        }
      }
    } catch (int ex) {
      continue;
    }
  } while (next_permutation(BLOCK, BLOCK + n));
  return false;
} // }}}
 
void init() { // {{{
  REP(y, H) REP(x, W) {
    hbadflag = vbadflag = false;
    PRINTED[x][y] = false;
    HBEGIN[x][y] = false;
    VBEGIN[x][y] = false;
  }
} // }}}
 
int main() {
  while (cin >> H >> W and H + W) {
    init();
 
    REP(y, H) {
      string row;
      cin >> row;
      REP(x, W) {
        TABLE[x][y] = row[x];
        PRINTED[x][y] = row[x] != '.';
      }
    }
 
    int idx = 0;
    REP(y, H) REP(x, W) {
      if (TABLE[x][y]!='#' and legal(x+1,y) and TABLE[x+1][y]!='#'
          and (!legal(x-1,y) or TABLE[x-1][y] == '#')) {
        HBEGIN[x][y] = true;
      }
      if (TABLE[x][y]!='#' and legal(x,y+1) and TABLE[x][y+1]!='#'
          and (!legal(x,y-1) or TABLE[x][y-1] == '#')) {
        VBEGIN[x][y] = true;
      }
      if (not PRINTED[x][y]) {
        BLANKS[idx] = make_pair(x, y);
        idx++;
      }
    }

 
    cin >> n;
    REP(i, n) { cin >> BLOCK[i]; }
 
    cout << (solve() ? "Yes" : "No") << endl;
  }
}


// vim: set foldmethod=marker :

Compiler output:
a.cc: In function 'bool solve()':
a.cc:185:13: error: 'VSOLVABLE' was not declared in this scope
  185 |         if (VSOLVABLE[i] and incorrect
      |             ^~~~~~~~~
a.cc:185:30: error: 'incorrect' was not declared in this scope
  185 |         if (VSOLVABLE[i] and incorrect
      |                              ^~~~~~~~~
a.cc:185:39: error: expected ')' before '}' token
  185 |         if (VSOLVABLE[i] and incorrect
      |            ~                          ^
      |                                       )
  186 |         }
      |         ~                              
a.cc:186:9: error: expected primary-expression before '}' token
  186 |         }
      |         ^
a.cc:188:5: error: expected 'catch' before '}' token
  188 |     } catch (int ex) {
      |     ^
a.cc:188:5: error: expected '(' before '}' token
  188 |     } catch (int ex) {
      |     ^
      |     (
a.cc:188:5: error: expected type-specifier before '}' token
a.cc:187:8: error: expected ')' before '}' token
  187 |       }
      |        ^
      |        )
  188 |     } catch (int ex) {
      |     ~   
a.cc:188:5: error: expected '{' before '}' token
  188 |     } catch (int ex) {
      |     ^
a.cc:188:13: error: expected 'while' before '(' token
  188 |     } catch (int ex) {
      |             ^
a.cc:188:14: error: expected primary-expression before 'int'
  188 |     } catch (int ex) {
      |              ^~~
a.cc:188:14: error: expected ')' before 'int'
  188 |     } catch (int ex) {
      |             ~^~~
      |              )
a.cc:188:14: error: expected ';' before 'int'
  188 |     } catch (int ex) {
      |              ^~~
      |              ;
a.cc:188:20: error: expected initializer before ')' token
  188 |     } catch (int ex) {
      |                    ^
a.cc:191:3: warning: no return statement in function returning non-void [-Wreturn-type]
  191 |   } while (next_permutation(BLOCK, BLOCK + n));
      |   ^
a.cc: At global scope:
a.cc:191:5: error: expected unqualified-id before 'while'
  191 |   } while (next_permutation(BLOCK, BLOCK + n));
      |     ^~~~~
a.cc:192:3: error: expected unqualified-id before 'return'
  192 |   return false;
      |   ^~~~~~
a.cc:193:1: error: expected declaration before '}' token
  193 | } // }}}
      | ^