Problem p00698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int H, W;
string field[1010][1010];
string diff[1010][1010];
int totals[2][1010];
int total;

int toInt(string s) {
  istringstream iss(s);
  int n;
  iss >> n;
  return n;
}

string toString(int n) {
  ostringstream oss;
  oss << n;
  return oss.str();
}

void Wcalc(int h, int x) {
  int sum = 0;
  for (int j = 0; j < W; j++) {
    if (field[h][j] == "?")
      continue;
    else
      sum += toInt(field[h][j]);
  }
  /*
  cout << "x = " << x << ", y = " << h << endl;
  cout << "sum = " << sum << endl;
  cout << totals[0][h] << endl;
  */
  field[h][x] = toString(totals[0][h] - sum);
}

void Hcalc(int w, int y) {
  int sum = 0;
  for (int i = 0; i < H; i++) {
    if (field[i][w] == "?")
      continue;
    else
      sum += toInt(field[i][w]);
  }
  /*
  cout << "x = " << w << ", y = " << y << endl;
  cout << totals[1][w] << endl;
  */
  field[y][w] = toString(totals[1][w] - sum);
}

int main() {
  bool flag = false;
  while (cin >> H) {
    if (H == 0)
      break;
    if (flag)
      cout << endl;
    if (!flag)
      flag = true;

    cin >> W;

    int q = 0;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> field[i][j];
        diff[i][j] = field[i][j];
        if (field[i][j] == "?")
          q++;
      }
      cin >> totals[0][i];
    }

    for (int i = 0; i < W; i++) {
      cin >> totals[1][i];
    }
    cin >> total;

    bool cansolve = true;
    while (1) {
      int tmp = q;
      for (int i = 0; i < H; i++) {
        int qcnt = 0;
        int w;
        for (int j = 0; j < W; j++) {
          if (field[i][j] == "?")
            w = j, qcnt++;
        }
        if (qcnt == 1) {
          Wcalc(i, w);
          q--;
        }
      }

      for (int j = 0; j < W; j++) {
        int qcnt = 0;
        int h;
        for (int i = 0; i < H; i++) {
          if (field[i][j] == "?")
            h = i, qcnt++;
        }
        if (qcnt == 1) {
          Hcalc(j, h);
          q--;
        }
      }

      if (q == 0)
        break;

      if (q == tmp) {
        cansolve = false;
        break;
      }
    }

    if (!cansolve) {
      cout << "NO" << endl;
      continue;
    }

    vector<string> ans;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (diff[i][j] == "?") {
          ans.push_back(field[i][j]);
        }
      }
    }

    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] << endl;
    }
    /*
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
        cout << field[i][j] <<  ' ';
      }
      cout << endl;
    }
    cout << endl;
    */
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int H, W;
string field[1010][1010];
string diff[1010][1010];
int totals[2][1010];
int total;

int toInt(string s) {
  istringstream iss(s);
  int n;
  iss >> n;
  return n;
}

string toString(int n) {
  ostringstream oss;
  oss << n;
  return oss.str();
}

void Wcalc(int h, int x) {
  int sum = 0;
  for (int j = 0; j < W; j++) {
    if (field[h][j] == "?")
      continue;
    else
      sum += toInt(field[h][j]);
  }
  /*
  cout << "x = " << x << ", y = " << h << endl;
  cout << "sum = " << sum << endl;
  cout << totals[0][h] << endl;
  */
  field[h][x] = toString(totals[0][h] - sum);
}

void Hcalc(int w, int y) {
  int sum = 0;
  for (int i = 0; i < H; i++) {
    if (field[i][w] == "?")
      continue;
    else
      sum += toInt(field[i][w]);
  }
  /*
  cout << "x = " << w << ", y = " << y << endl;
  cout << totals[1][w] << endl;
  */
  field[y][w] = toString(totals[1][w] - sum);
}

int main() {
  bool flag = false;
  while (cin >> H) {
    if (H == 0)
      break;
    if (flag)
      cout << endl;
    if (!flag)
      flag = true;

    cin >> W;

    int q = 0;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> field[i][j];
        diff[i][j] = field[i][j];
        if (field[i][j] == "?")
          q++;
      }
      cin >> totals[0][i];
    }

    for (int i = 0; i < W; i++) {
      cin >> totals[1][i];
    }
    cin >> total;

    int tmp = q;

    bool cansolve = true;
    while (1) {
      for (int i = 0; i < H; i++) {
        int qcnt = 0;
        int w;
        for (int j = 0; j < W; j++) {
          if (field[i][j] == "?")
            w = j, qcnt++;
        }
        if (qcnt == 1) {
          Wcalc(i, w);
          tmp--;
        }
      }

      for (int j = 0; j < W; j++) {
        int qcnt = 0;
        int h;
        for (int i = 0; i < H; i++) {
          if (field[i][j] == "?")
            h = i, qcnt++;
        }
        if (qcnt == 1) {
          Hcalc(j, h);
          tmp--;
        }
      }

      if (tmp == 0)
        break;

      if (q == tmp) {
        cansolve = false;
        break;
      }
    }

    if (!cansolve) {
      cout << "NO" << endl;
      continue;
    }

    vector<string> ans;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (diff[i][j] == "?") {
          ans.push_back(field[i][j]);
        }
      }
    }

    for (int i = 0; i < q; i++) {
      cout << ans[i] << endl;
    }
    /*
    for(int i = 0 ; i < H ; i++){
      for(int j = 0 ; j < W ; j++){
        cout << field[i][j] <<  ' ';
      }
      cout << endl;
    }
    cout << endl;
    */
  }
  return 0;
}