Ejemplo n.º 1
0
  // cntl : count of left parentheses
  // cntr : count of right parentheses
  // cntp : count of parentheses paris
  // comb : combination of characters
  void _solve(std::string s, int i, int cntl, int cntr, int cntp,
              std::string comb) {
    for (int j = 0; j < i; ++j)
      printf(" ");
    printf("i: %d, cntl: %d, cntr: %d, cntp: %d, comb: %s\n",
           i, cntl, cntr, cntp, comb.c_str());
    
    // base condition
    if (i == s.size()) {
      if (cntl == 0 && cntr == 0 && cntp == 0)
        m_rslt.insert(comb);
      return;
    }

    // recursion
    if (s[i] != '(' && s[i] != ')') {
      _solve(s, i+1, cntl, cntr, cntp, comb+s[i]);
    } else {
      if (s[i] == '(') {
        if (cntl > 0) {
          _solve(s, i+1, cntl-1, cntr, cntp, comb);
        }
        _solve(s, i+1, cntl, cntr, cntp+1, comb+s[i]);        
      } else if (s[i] == ')') {
        if (cntr > 0) {
          _solve(s, i+1, cntl, cntr-1, cntp, comb);
        }
        if (cntp > 0) {
          _solve(s, i+1, cntl, cntr, cntp-1, comb+s[i]);
        }
      }
      
    }
  }
Ejemplo n.º 2
0
void Board::solve()
{
  iterationsMax = 1;
  for( vector< Cell *>::iterator i = _board.begin(); i != _board.end(); ++i ) {
    set< uint8_t > p = (*i)->getPossibles();
    iterationsMax *= p.size();
  }
  cout << "Iterations MAX (overestimation): " << iterationsMax << endl;
  _optimize();
  double iterationsMax1 = 1.;
  for( vector< Cell *>::iterator i = _board.begin(); i != _board.end(); ++i ) {
    iterationsMax1 *= (*i)->getPossibles().size();
  }
  cout << "Iterations MAX after preoptimization (overestimation): " << iterationsMax1 << endl;
//   m_usingOptiBoard = _buildOptiboard();
  startTime = time(0);
  if( m_usingOptiBoard ) {
    cout << "Using Optimized board" << endl;
    if( _solve( _optiBoard.begin() ) ) {
      cout << "Finish" << endl;
    } else {
      cout << "Failed" << endl;
    }
  } else {
    cout << "Using standard board" << endl;
    if( _solve( _board.begin() ) ) {
      cout << "Finish" << endl;
    } else {
      cout << "Failed" << endl;
    }
  }
  show();
}
Ejemplo n.º 3
0
void _solve(int x, int y, char old_color, char new_color) {
  // base condition
  if (x < 0 || x >= C || y < 0 || y >= R)
    return;

  // recursion
  if (SCR[y][x] == old_color) {
    SCR[y][x] = new_color;
    _solve(x, y + 1, old_color, new_color);
    _solve(x + 1, y, old_color, new_color);
    _solve(x, y - 1, old_color, new_color);
    _solve(x - 1, y, old_color, new_color);
  }
}
Ejemplo n.º 4
0
  int _solve(int y, int x) {
    // base condition
    if (y == m-1 && x == n-1)
      return 1;
    else if (y >= m || x >= n)
      return 0;

    // memoization
    int& r = C[y][x];
    if (r >= 0)
      return r;

    // recursion
    return r = _solve(y+1, x) + _solve(y, x+1);
  }
Ejemplo n.º 5
0
double _solve(int cur_recog_idx, int prev_match_idx) {
  // printf("  ");
  // for (int i = 0; i < cur_recog_idx; ++i)
  //   printf(" ");
  // printf("%d %d\n", cur_recog_idx, prev_match_idx);
  
  // base condition
  if (cur_recog_idx == N)
    return 0;
  double& r = CACHE[cur_recog_idx][prev_match_idx];  
  if (r != 1.0)
    return r;
  r = -1e200;
  int& choose = CHOICE[cur_recog_idx][prev_match_idx];
  // recursion
  for (int cur_match_idx = 0; cur_match_idx < m; ++cur_match_idx) {
    double cand = T[prev_match_idx][cur_match_idx] +
                  M[cur_match_idx][R[cur_recog_idx]] +
                  _solve(cur_recog_idx + 1, cur_match_idx);
    if (r < cand) {
      r = cand;
      choose = cur_match_idx;
    }
  }
  return r;
}
Ejemplo n.º 6
0
 int uniquePaths(int a, int b) {
   m = a;
   n = b;
   for (int y = 0; y < a; ++y)
     for (int x = 0; x < b; ++x)
       C[y][x] = -1;
   return _solve(0, 0);
 }
Ejemplo n.º 7
0
PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {

	ERR_FAIL_COND_V(!points.has(p_from_id),PoolVector<Vector3>());
	ERR_FAIL_COND_V(!points.has(p_to_id),PoolVector<Vector3>());


	pass++;

	Point* a = points[p_from_id];
	Point* b = points[p_to_id];

	if (a==b) {
		PoolVector<Vector3> ret;
		ret.push_back(a->pos);
		return ret;
	}


	Point *begin_point=a;
	Point *end_point=b;

	bool found_route=_solve(begin_point,end_point);

	if (!found_route)
		return PoolVector<Vector3>();

	//midpoints
	Point *p=end_point;
	int pc=1; //begin point
	while(p!=begin_point) {
		pc++;
		p=p->prev_point;
	}

	PoolVector<Vector3> path;
	path.resize(pc);

	{
		PoolVector<Vector3>::Write w = path.write();

		Point *p=end_point;
		int idx=pc-1;
		while(p!=begin_point) {
			w[idx--]=p->pos;
			p=p->prev_point;
		}

		w[0]=p->pos; //assign first

	}

	return path;

}
Ejemplo n.º 8
0
bool _solve (char* seq, int n, int c)
{
    if (n == 0)
        return false;
    if (c)
    {
        if (n == 1)
            return ((seq[0] - '0') & 1) == 0;
        else if (n == 2)
            return (seq[0] == seq[1] + 1);
        else if (seq[0] == seq[n - 1] + 1)
        {
            seq[n - 2]--;
            bool r = _solve (seq + 1, n - 2, 1);
            seq[n - 2]++;
            return r;
        }
        else if (seq[0] == seq[n - 1] && seq[0] != '9')
        {
            seq[n - 2]--;
            bool r = _solve (seq + 1, n - 2, 0);
            seq[n - 2]++;
            return r;
        }
        else
            return false;
    }
    else
    {
        if (n == 1)
            return ((seq[0] - '0') & 1) == 0;
        else if (n == 2)
            return (seq[0] == seq[1]);
        else if (seq[0] == seq[n - 1] + 1)
            return _solve (seq + 1, n - 2, 1);
        else if (seq[0] == seq[n - 1])
            return _solve (seq + 1, n - 2, 0);
        else
            return false;
    }
}
Ejemplo n.º 9
0
int main ()
{
    freopen ("reverse.in", "rt", stdin);
    freopen ("reverse.out", "wt", stdout);

    while (true)
    {
        scanf ("%s", seq);
        if (seq[0] == '0' && seq[1] == 0)
            return 0;
        int len = (int) strlen (seq);
        if (_solve (seq, len, 0))
            printf ("YES\n");
        else if (seq[0] == '1' && _solve (seq + 1, len - 1, 1))
            printf ("YES\n");
        else
            printf ("NO\n");
    }

    return 0;
}
Ejemplo n.º 10
0
int solve(const std::vector<std::string>& v, std::string start, std::string target) {
  std::vector<bool> visited(v.size(), false);
  for (int i = 0; i < v.size(); ++i) {
    std::string next = v[i];
    if (visited[i] == false && is_adj(start, next)) {
      printf("%s %s\n", start.c_str(), next.c_str());
      visited[i] = true;
      _solve(v, visited, next, target, 1);
      visited[i] = false;
    }
  }
  return (BEST == 0 || BEST == MAX) ? 0 : BEST + 2;
}
Ejemplo n.º 11
0
 bool exist(std::vector<std::vector<char>>& b, std::string s) {
   if (b.size() == 0 || s.empty())
     return false;
   B = b;
   // DFS
   for (int i = 0; i < b.size(); ++i) {
     for (int j = 0; j < b[0].size(); ++j) {
       if (_solve(i, j, s, 0))
         return true;
     }
   }
   return false;    
 }
Ejemplo n.º 12
0
 std::vector<std::string> removeInvalidParentheses(std::string s) {
   int cntl = 0, cntr = 0, cntp = 0;
   for (char c : s) {
     if (c == '(') {
       ++cntl;
     } else if (c == ')') {
       if (cntl > 0) {
         --cntl;
       } else {
         ++cntr;
       }
     }
   }
   _solve(s, 0, cntl, cntr, cntp, "");
   return std::vector<std::string>(m_rslt.begin(), m_rslt.end());
 }
Ejemplo n.º 13
0
void solve() {
  for (int i = 0; i < 102; ++i)
    for (int j = 0; j < 502; ++j)
      CACHE[i][j] = 1.0;

  double r = -1e200;
  for (int cur_match_idx = 0; cur_match_idx = m; ++cur_match_idx) {
    double cand = B[cur_match_idx] + M[cur_match_idx][R[0]] +
        _solve(1, cur_match_idx);
    if (r < cand) {
      CACHE[0][0] = cand;
      CHOICE[0][0] = cur_match_idx;
    }
  }

}
Ejemplo n.º 14
0
//////////////////////////////////////////////////////////////////////
// Backtracking
// O(N^2M)
// ???
void _solve(const std::vector<std::string>& v, std::vector<bool>& visited,
            const std::string& prev, const std::string& end, int n_lv) {
  // base condition
  if (is_adj(prev, end)) {
    BEST = std::min(BEST, n_lv);
    return;
  }
  // pruning
  if (BEST <= n_lv)
    return;
  // recursion
  for (int i = 0; i < v.size(); ++i) {
    std::string next = v[i];   
    if (visited[i] == false && is_adj(prev, next)) {
      print_indent(n_lv);
      printf("%s %s\n", prev.c_str(), next.c_str());
      visited[i] = true;
      _solve(v, visited, next, end, n_lv + 1);
      visited[i] = false;
    }
  }
}
Ejemplo n.º 15
0
bool Board::_solve( std::vector< Cell* >::iterator i )
{
  if( m_usingOptiBoard ) {
    if( i == _optiBoard.end() ) {
      return true;
    }
  } else {
    if( i == _board.end() ) {
      return true;
    }
  }
  ++iterations;
  if( iterations % 50000 == 0 ) {
    show();
  }
  uSet p = (*i)->getPossibles();
  if( p.empty() ) {
    return false;
  }
  for( uSet::iterator j = p.begin(); j != p.end(); ++j ) {
    (*i)->setValue( *j );
    if( _isImpossible( (*i) ) ) {
      ++iterations;
      if( iterations % 50000 == 0 ) {
	show();
      }
      (*i)->setValue( 0 );
      continue;
    }
    ++i;
    if( _solve( i ) == true ) {
      return true;
    }
    --i;
    (*i)->setValue( 0 );
  }
  return false;
}
Ejemplo n.º 16
0
  bool _solve(int y, int x, std::string s, int d) {
    // base condition
    if (B[y][x] != s[d])
      return false;
    if (d+1 >= s.size())
      return true;

    // recursion
    int dy[4] = {-1, 0, 1, 0};
    int dx[4] = {0, -1, 0, 1};
    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i];
      int nx = x + dx[i];

      B[y][x] = ~B[y][x];

      if (ny >= 0 && ny < B.size() && nx >= 0 && nx < B[0].size()
          && B[ny][nx] > 0 && _solve(ny, nx, s, d+1))
        return true;

      B[y][x] = ~B[y][x];
    }   
    return false;
  }
Ejemplo n.º 17
0
void solve(int x, int y, char new_color) {
  if (SCR[y][x] == new_color)
    return;
  _solve(x, y, SCR[y][x], new_color);
}