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

#include <algorithm>
#include <iostream>

using namespace ::std;

int ans = 0;

void solve(int **osenbei, int **osenbei_extra, int i, const int c, const int r,
           int temp) {

  int bef = 0;
  int aft = 0;
  if (i == c) {

    for (int j = 0; j < c; j++) {
      for (int k = 0; k < r; k++) {
        osenbei_extra[j][k] = osenbei[j][k];
      }
    }

    for (int j = 0; j < r; j++) {

      int bef = 0;
      int aft = 0;
      for (int k = 0; k < c; k++) { // i???????????????
        if (osenbei_extra[k][j] == 1) {
          osenbei_extra[k][j] = 0;
          bef++; // i?????????1?????°????????????
        } else {
          osenbei_extra[k][j] = 1;
          aft++; // i?????????0?????°????????????
        }
      }
      if (bef <= aft) {
        temp = temp - bef + aft;
        ans = max(ans, temp);
      } else {
        for (int k = 0; k < c; k++) { // i?????????????????´???
          if (osenbei_extra[k][j] == 1)
            osenbei_extra[k][j] = 0;
          else
            osenbei_extra[k][j] = 1;
        }
      }
    }

    if (ans < temp) {
      ans = temp;
    }

  } else {

    for (int j = 0; j < r; j++) { // i???????????????
      if (osenbei[i][j] == 1) {
        osenbei[i][j] = 0;
        bef++; // i?????????1?????°????????????
      } else {
        osenbei[i][j] = 1;
        aft++; // i?????????0?????°????????????
      }
    }

    solve(osenbei, osenbei_extra, i + 1, c, r, temp - bef + aft);

    for (int j = 0; j < r; j++) { // i?????????????????´???
      if (osenbei[i][j] == 1)
        osenbei[i][j] = 0;
      else
        osenbei[i][j] = 1;
    }

    solve(osenbei, osenbei_extra, i + 1, c, r, temp);
  }
}

int main() {

  while (1) {

    int temp_c, temp_r;
    cin >> temp_c; //???
    cin >> temp_r; //???

    const int r = temp_r;
    const int c = temp_c;

    if (c == 0 && r == 0)
      break;

    int **osenbei = new int *[c];
    int **osenbei_extra = new int *[c];
    for (int i = 0; i < c; i++) {
      osenbei[i] = new int[r];
      osenbei_extra[i] = new int[r];
    }

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < r; j++) {
        osenbei[i][j] = 0;
        osenbei_extra[i][j] = 0;
      }
    }

    int temp = 0;
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < r; j++) {
        cin >> osenbei[i][j];
        if (osenbei[i][j] != 0 && osenbei[i][j] != 1)
          return 1;
        else if (osenbei[i][j] == 1)
          temp++;
        osenbei_extra[i][j] = osenbei[i][j];
      }
    }
    ans = temp;

    solve(osenbei, osenbei_extra, 0, c, r, temp); //??±???????????¢?´¢+????¬????
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace ::std;

int ans = 0;

void dfs_second(int **osenbei, int i, const int c, const int r, int temp) {

  int bef = 0;
  int aft = 0;
  if (i == r) {
    ans = max(ans, temp);
  } else {

    for (int j = 0; j < c; j++) { // i???????????????
      if (osenbei[j][i] == 1) {
        osenbei[j][i] = 0;
        bef++; // i?????????1?????°?????°??????
      } else {
        osenbei[j][i] = 1;
        aft++; // i?????????0?????°?????°??????
      }
    }

    dfs_second(osenbei, i + 1, c, r, temp - bef + aft);

    for (int j = 0; j < c; j++) { // i?????????????????´???
      if (osenbei[j][i] == 1)
        osenbei[j][i] = 0;
      else
        osenbei[j][i] = 1;
    }

    dfs_second(osenbei, i + 1, c, r, temp);
  }
}

void dfs_first(int **osenbei, int i, const int c, const int r, int temp) {

  int bef = 0;
  int aft = 0;
  if (i == c) {
    dfs_second(osenbei, 0, c, r, temp);
  } else {

    for (int j = 0; j < r; j++) { // i???????????????
      if (osenbei[i][j] == 1) {
        osenbei[i][j] = 0;
        bef++; // i?????????1?????°????????????
      } else {
        osenbei[i][j] = 1;
        aft++; // i?????????0?????°????????????
      }
    }

    dfs_first(osenbei, i + 1, c, r, temp - bef + aft);

    for (int j = 0; j < r; j++) { // i?????????????????´???
      if (osenbei[i][j] == 1)
        osenbei[i][j] = 0;
      else
        osenbei[i][j] = 1;
    }

    dfs_first(osenbei, i + 1, c, r, temp);
  }
}

void solve(int **osenbei, int **osenbei_extra, int i, const int c, const int r,
           int temp) {

  int bef = 0;
  int aft = 0;
  if (i == c) {

    for (int j = 0; j < c; j++) {
      for (int k = 0; k < r; k++) {
        osenbei_extra[j][k] = osenbei[j][k];
      }
    }

    for (int j = 0; j < r; j++) {

      int bef = 0;
      int aft = 0;
      for (int k = 0; k < c; k++) { // i???????????????
        if (osenbei_extra[k][j] == 1) {
          osenbei_extra[k][j] = 0;
          bef++; // i?????????1?????°????????????
        } else {
          osenbei_extra[k][j] = 1;
          aft++; // i?????????0?????°????????????
        }
      }
      if (bef <= aft) {
        temp = temp - bef + aft;
        ans = max(ans, temp);
      } else {
        for (int k = 0; k < c; k++) { // i?????????????????´???
          if (osenbei_extra[k][j] == 1)
            osenbei_extra[k][j] = 0;
          else
            osenbei_extra[k][j] = 1;
        }
      }
    }

    if (ans < temp) {
      ans = temp;
    }

  } else {

    for (int j = 0; j < r; j++) { // i???????????????
      if (osenbei[i][j] == 1) {
        osenbei[i][j] = 0;
        bef++; // i?????????1?????°????????????
      } else {
        osenbei[i][j] = 1;
        aft++; // i?????????0?????°????????????
      }
    }

    dfs_first(osenbei, i + 1, c, r, temp - bef + aft);

    for (int j = 0; j < r; j++) { // i?????????????????´???
      if (osenbei[i][j] == 1)
        osenbei[i][j] = 0;
      else
        osenbei[i][j] = 1;
    }

    dfs_first(osenbei, i + 1, c, r, temp);
  }
}

int main() {

  while (1) {

    int temp_c, temp_r;
    cin >> temp_c; //???
    cin >> temp_r; //???

    const int r = temp_r;
    const int c = temp_c;

    if (c == 0 && r == 0)
      break;

    int **osenbei = new int *[c];
    int **osenbei_extra = new int *[c];
    for (int i = 0; i < c; i++) {
      osenbei[i] = new int[r];
      osenbei_extra[i] = new int[r];
    }

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < r; j++) {
        osenbei[i][j] = 0;
        osenbei_extra[i][j] = 0;
      }
    }

    int temp = 0;
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < r; j++) {
        cin >> osenbei[i][j];
        if (osenbei[i][j] != 0 && osenbei[i][j] != 1)
          return 1;
        else if (osenbei[i][j] == 1)
          temp++;
        osenbei_extra[i][j] = osenbei[i][j];
      }
    }
    ans = temp;

    // dfs_first(osenbei, 0, c, r, temp);//??±???????????¢?´¢??2
    solve(osenbei, osenbei_extra, 0, c, r, temp); //??±???????????¢?´¢+????¬????
    cout << ans << endl;
  }

  return 0;
}