Language: C++

// AOJ 1022
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#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) {
//  cout << "? " << s << " ";
  if (s[p] == '=') { throw NotEquationExc; }
  result r = expression(s, p);
  if (s[r.p] != '=') { throw NotEquationExc; }
  
  r.p++; // skip '=';
  result t = expression(s, r.p);
  t.value = (int)(r.value == t.value);

//  cout << (t.value ? "T" : "F") << endl;
  
  return t;
}


result expression(const char* &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 char* &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 char* &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];

void init() {
  REP(y, H) REP(x, W) {
    PRINTED[x][y] = false;
    HBEGIN[x][y] = false;
    VBEGIN[x][y] = false;
  }
}

bool legal(int x, int y) {
  return 0 <= x and x < W and 0 <= y and y < H;
}

void fill_blank() {
  int i = 0;
  REP(y, H) REP(x, W) {
    if (i == n) return;
    if (not PRINTED[x][y]) {
      TABLE[x][y] = BLOCK[i++];
    }
  }
}

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;
}

bool solved() {
  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) { return false; }
    } catch (int ex) {
      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) { return false; }
    } catch(int ex) {
      return false;
    }
  }
  return true;
}

bool solve() {
  sort(BLOCK, BLOCK + n);
  do {
    fill_blank();
/*    REP(y, H) {
      REP(x, W) {
        cout << TABLE[x][y];
      }
      cout << endl;
    }*/
    if (is_illegal_operand()) continue;
    if (solved()) { return true; }
  } while (next_permutation(BLOCK, BLOCK + n));
}

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] != '.';
      }
    }

    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;
      }
    }

    cin >> n;
    REP(i, n) { cin >> BLOCK[i]; }

    cout << (solve() ? "Yes" : "No") << endl;
  }
}

Compiler output:
a.cc: In function 'bool solve()':
a.cc:190:1: warning: control reaches end of non-void function [-Wreturn-type]
  190 | }
      | ^
/usr/bin/ld: /tmp/ccmbA8kc.o: in function `equation(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
a.cc:(.text+0x64): undefined reference to `expression(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: a.cc:(.text+0xca): undefined reference to `expression(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /tmp/ccmbA8kc.o: in function `expression(char const*&, int)':
a.cc:(.text+0x139): undefined reference to `factor(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: a.cc:(.text+0x19c): undefined reference to `factor(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: /tmp/ccmbA8kc.o: in function `factor(char const*&, int)':
a.cc:(.text+0x32e): undefined reference to `term(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: a.cc:(.text+0x391): undefined reference to `term(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
collect2: error: ld returned 1 exit status