Example #1
0
	void checkIntersections(vector<MapLine*> lines)
	{
		double x, y;
		MapLine* line1;
		MapLine* line2;

		// Clear existing intersections
		intersections.clear();

		// Go through lines
		for (unsigned a = 0; a < lines.size(); a++)
		{
			line1 = lines[a];

			// Go through uncompared lines
			for (unsigned b = a + 1; b < lines.size(); b++)
			{
				line2 = lines[b];

				// Check intersection
				if (map->linesIntersect(line1, line2, x, y))
					intersections.push_back(line_intersect_t(line1, line2, x, y));
			}
		}
	}
Example #2
0
void printBoard() {
	for(int i = 0; i < board.size(); i++) {
		for(int j = 0; j < board.size(); j++)
			cout << (board[i][j]?'X':'.');
		cout << endl;
	}
}
Example #3
0
void getMaxLayers(vector<int>& maxlayer, vector<vector<int> >& voice, 
		HumdrumFile& infile) {
	int track;
	maxlayer.resize(infile.getMaxTracks() + 1);
	std::fill(maxlayer.begin(), maxlayer.end(), 0);
	int i, j;
	for (i=0; i<infile.getNumLines(); i++) {
		if (!infile[i].isData()) {
				continue;
		}
		for (j=0; j<infile[i].getFieldCount(); j++) {
				if (!infile[i].isExInterp(j, "**kern")) {
				continue;
				}
				if (strcmp(infile[i][j], ".") == 0) {
				continue;
				}
				if (strchr(infile[i][j], 'r') != NULL) {
				continue;
				}
				track = infile[i].getPrimaryTrack(j);
				if (voice[i][j] + 1 > maxlayer[track]) {
				maxlayer[track] = voice[i][j] + 1;
				}
		}
	}
}
Example #4
0
 vector <vector <string> > solveNQueens(int n) {
     if (n == 1) {
         vector <string> tmp;
         tmp.push_back("Q");
         vector <vector <string> > result;
         result.push_back(tmp);
         return result;
     }
     for (int i = 0; i < 20; ++ i) {
         x_flag[i] = y_flag[i] = diagonal_l[i] = diagonal_r[i] = false;
     }
     this->n = n;
     for (int i = 0; i < n; ++ i) {
         string tmp(n, '.');
         now.push_back(tmp);
     }
     for (int i = 0; i < n; ++ i)
         for (int j = 0; j < n; ++ j) {
             now[i][j] = 'Q';
             x_flag[i] = y_flag[j] = true;
             diagonal_l[n - 1 + i - j] = diagonal_r[2 * (n - 1) - i - j] = true;
             dfs(n - 1, i, j);
             diagonal_l[n - 1 + i - j] = diagonal_r[2 * (n - 1) - i - j] = false;
             x_flag[i] = y_flag[j] = false;
             now[i][j] = '.';
     }
     return result;
 }
    int maxPoints(vector<Point> &points) {
		int n=points.size();
		if(n<3){return n;}
		sort(points.begin(),points.end(),Compare);
		int result=2;
		for(int i=0;i<n;i++)
			{Point &p1=points[i];
			 unordered_map<double,int> slope;
			 //slope is only defined with respect to a specific starting point and thus there is no yval problem. If slope same then yval is the same.
			 //thus this contains all the points that are on the same line with p1
			 double m=0.0;
			 int same_points=1;
			 int local_max=1;
			 for(int j=i+1;j<n;j++)
				{
				 Point &p2=points[j];
				 if(p1.x==p2.x and p1.y==p2.y)
					{same_points++;
					 local_max=max(local_max,same_points);//this line takes care of (0,0),(0,1),(0,0),(0,-1),(0,0) issue...so the x=0 points can be randomly placed and Compare is justified
					 continue;
					}
				 else if(p1.x==p2.x)
					{m=0xfffffff;}
				 else
					{m=double(p2.y-p1.y)/(p2.x-p1.x);}
				 if(slope.find(m)==slope.end())
					{slope.insert(make_pair(m,same_points));}
				 slope[m]++;
				 local_max=max(local_max,slope[m]);
				}
			result=max(result,local_max);
			}
		return result;
	}
Example #6
0
 int threeSumClosest(vector<int>& nums, int target) {
     int len = nums.size();
     sort(nums.begin(), nums.end());
     
     int result, min_diff = INT_MAX;
     
     for (int i = 0; i < len-2; ++i) {
         if (i == 0 || i > 0 && nums[i] != nums[i-1]) {
             int left = i+1, right = len-1;
             while (left < right) {
                 int sum = nums[i]+nums[left]+nums[right];
                 if (sum == target) return target;
                 
                 int diff = abs(sum-target);
                 if (diff < min_diff) {
                     min_diff = diff;
                     result = sum;
                 }
                 
                 if (sum < target) ++left;
                 else --right;
             }
         }
     }
     return result;
 }
Example #7
0
 void
 set_capture_instructions(){
   instructions.clear();
   instructions.push_back(string("Select expressive frames."));
   instructions.push_back(string("s - use this frame"));
   instructions.push_back(string("q - done"));
 }
Example #8
0
void unmap_path(string path, string fileid) {
  //cout << "in unmap_path" << endl;
  //cout << "path: " << path << " fileid: " << fileid << endl;
  string token = "";
  string attr = "";
  stringstream ss2(path.c_str());
  while(getline(ss2, token, '/')) {
    //cout << "got token " << token << endl;
    if(strcmp(token.c_str(),"null")!=0) {
      if(attr.length()>0) {
        //cout << "removing map " << attr << " to " << token << endl;
        database_remove_val(fileid, attr, token);
        if(attr=="location") {
          int pos=find(server_ids.begin(),server_ids.end(),token)-server_ids.begin();
          if( pos < server_ids.size() ) {
            database_remove_val(fileid, "server", servers.at(pos));
          }
        }
        attr = "";
      } else {
        attr = token;
      }
    }
  }
  //cout << "finished unmap_path" << endl << endl;
}
Example #9
0
static void calcChessboardCorners(Size boardSize, float squareSize, vector<Point3f>& corners, Pattern patternType = CHESSBOARD)
{
    corners.resize(0);
    
    switch(patternType)
    {
      case CHESSBOARD:
      case CIRCLES_GRID:
        for( int i = 0; i < boardSize.height; i++ )
            for( int j = 0; j < boardSize.width; j++ )
                corners.push_back(Point3f(float(j*squareSize),
                                          float(i*squareSize), 0));
        break;

      case ASYMMETRIC_CIRCLES_GRID:
        for( int i = 0; i < boardSize.height; i++ )
            for( int j = 0; j < boardSize.width; j++ )
                corners.push_back(Point3f(float((2*j + i % 2)*squareSize),
                                          float(i*squareSize), 0));
        break;

      default:
        CV_Error(CV_StsBadArg, "Unknown pattern type\n");
    }
}
Example #10
0
 vector<string> anagrams(vector<string> &strs) {
     if (strs.empty())
         return{};
     vector<string> output;
     unordered_map<string, int> myum;
     for (int i = 0; i < (int)strs.size(); i++) {
         string temp = strs[i];
         sort(temp.begin(), temp.end());
         auto got = myum.find(temp);
         if (got == myum.end()) {
             myum.emplace(temp, i);
         }
         else {
             if (got->second != -1) {
                 output.push_back(strs[got->second]);
                 output.push_back(strs[i]);
                 got->second = -1;
             }
             else {
                 output.push_back(strs[i]);
             }
         }
     }
     return output;
 }
Example #11
0
void BlankGeom::LoadDrawObjs(vector< DrawObj* > & draw_obj_vec)
{
    char str[256];

    if ( m_Vehicle->IsGeomActive( m_ID ) )
    {
        sprintf(str,"%d",1);
        m_HighlightDrawObj.m_GeomID = m_ID+string(str);
        m_HighlightDrawObj.m_Visible = !m_GuiDraw.GetNoShowFlag();

        // Set Render Destination to Main VSP Window.
        m_HighlightDrawObj.m_Screen = DrawObj::VSP_MAIN_SCREEN;
        m_HighlightDrawObj.m_Type = DrawObj::VSP_POINTS;
        draw_obj_vec.push_back( &m_HighlightDrawObj) ;
    }

    if ( ( m_GuiDraw.GetDispFeatureFlag() && !m_GuiDraw.GetNoShowFlag() ) || m_Vehicle->IsGeomActive( m_ID ))
    {
        for ( int i = 0; i < m_FeatureDrawObj_vec.size(); i++ )
        {
            m_FeatureDrawObj_vec[i].m_Screen = DrawObj::VSP_MAIN_SCREEN;
            sprintf( str, "_%d", i );
            m_FeatureDrawObj_vec[i].m_GeomID = m_ID + "Feature_" + str;
            m_FeatureDrawObj_vec[i].m_LineWidth = 2.0;
            m_FeatureDrawObj_vec[i].m_LineColor = vec3d( 0.0, 0.0, 0.0 );
            m_FeatureDrawObj_vec[i].m_Type = DrawObj::VSP_LINE_STRIP;
            draw_obj_vec.push_back( &m_FeatureDrawObj_vec[i] );
        }
    }
}
Example #12
0
   bool can_win(long long x, vector <int> gs, vector <pair< int, long long> > e) {
      int pos = 0;
      for(int i=0; pos<(int)e.size() && i<(int)gs.size(); i++) {
         long long str = x;
         if( e[pos].first > gs[i] ) return 0;

         while( str && pos<(int)e.size() ) {
            if( e[pos].second > str ) {
               e[pos].second -= str;
               str = 0LL;
            }
            else if( e[pos].second == str ) {
               e[pos].second = 0LL;
               pos++;
               str = 0LL;
            }
            else {
               str -= e[pos].second;
               e[pos].second = 0LL;
               pos++;
            }
         }
      }
      return pos >= (int)e.size();
   }
Example #13
0
	bool fixProblem(unsigned index, unsigned fix_type, MapEditor* editor)
	{
		if (index >= overlaps.size())
			return false;

		// Get thing to remove (depending on fix)
		MapThing* thing = NULL;
		if (fix_type == 0)
			thing = overlaps[index].thing1;
		else if (fix_type == 1)
			thing = overlaps[index].thing2;

		if (thing)
		{
			// Remove thing
			editor->beginUndoRecord("Delete Thing", false, false, true);
			map->removeThing(thing);
			editor->endUndoRecord();

			// Clear any overlaps involving the removed thing
			for (unsigned a = 0; a < overlaps.size(); a++)
			{
				if (overlaps[a].thing1 == thing || overlaps[a].thing2 == thing)
				{
					overlaps.erase(overlaps.begin() + a);
					a--;
				}
			}

			return true;
		}

		return false;
	}
Example #14
0
	bool fixProblem(unsigned index, unsigned fix_type, MapEditor* editor)
	{
		if (index >= overlaps.size())
			return false;

		if (fix_type == 0)
		{
			MapLine* line1 = overlaps[index].line1;
			MapLine* line2 = overlaps[index].line2;

			editor->beginUndoRecord("Merge Lines");

			// Remove first line and correct sectors
			map->removeLine(line1);
			map->correctLineSectors(line2);

			editor->endUndoRecord();

			// Remove any overlaps for line1 (since it was removed)
			for (unsigned a = 0; a < overlaps.size(); a++)
			{
				if (overlaps[a].line1 == line1 || overlaps[a].line2 == line1)
				{
					overlaps.erase(overlaps.begin() + a);
					a--;
				}
			}

			return true;
		}

		return false;
	}
Example #15
0
int main()
{
	ifstream fin("namenum.in");
	fin >> code;
	fin.close();

	n = code.length();

	ifstream dictionary("dict.txt");
	for(int i = 0 ; i < 4617 ; i++)
	{
		string abc;
		dictionary >> abc;
		dic[i] = abc;
	}
	dictionary.close();
	ofstream fout("namenum.out");
	solve(0);


	if(answer.size() == 0)
	{
		fout << "NONE" << "\n";
		fout.close();
		return 0;
	}
	for(int i =0  ; i < answer.size() ; i++)
		fout << answer[i] << "\n";
	fout.close();
	return 0;
}
Example #16
0
static double computeReprojectionErrors(
        const vector<vector<Point3f> >& objectPoints,
        const vector<vector<Point2f> >& imagePoints,
        const vector<Mat>& rvecs, const vector<Mat>& tvecs,
        const Mat& cameraMatrix, const Mat& distCoeffs,
        vector<float>& perViewErrors )
{
    vector<Point2f> imagePoints2;
    int i, totalPoints = 0;
    double totalErr = 0, err;
    perViewErrors.resize(objectPoints.size());
    
    for( i = 0; i < (int)objectPoints.size(); i++ )
    {
        projectPoints(Mat(objectPoints[i]), rvecs[i], tvecs[i],
                      cameraMatrix, distCoeffs, imagePoints2);
        err = norm(Mat(imagePoints[i]), Mat(imagePoints2), CV_L2);
        int n = (int)objectPoints[i].size();
        perViewErrors[i] = (float)std::sqrt(err*err/n);
        totalErr += err*err;
        totalPoints += n;
    }
    
    return std::sqrt(totalErr/totalPoints);
}
int TrySail::get(vector <int> strength) {
	memo[0][0][0] = 1;
	int result = 0;
	for(int n = 1; n <= strength.size() + 1; n++) {
		int x = (n <= strength.size() ? strength[n - 1] : 0);
		for(int i = 255; i >= 0; i--) {
			for(int j = 255; j >= 0; j--) {
				for(int k = 255; k >= 0; k--) {
					if(memo[i][j][k] == n or memo[i][j][k] == -1) {
						if(n == strength.size() + 1) {
							result = max(result, i + j + k);
							continue;
						}
						int n_i = i ^ x;
						int n_j = j ^ x;
						int n_k = k ^ x;

						memo[n_i][j][k] = (memo[n_i][j][k] == n ? -1 : n + 1);	
						memo[i][n_j][k] = (memo[i][n_j][k] == n ? -1 : n + 1);	
						memo[i][j][n_k] = (memo[i][j][n_k] == n ? -1 : n + 1);	
					}
					if(memo[i][j][k] == n) memo[i][j][k] = 0;
					else if(memo[i][j][k] == -1) memo[i][j][k] = n + 1;
				}
			}
		}
	}
	
	return result;
}
// Using rules, CPT and binding to predict cases, return prediction results.
Pred predict(const vector<Constraint>& cons, const vector<CPTRow>& cpt, const vector<string>& plst, const vector<string>& nlst)
{
	Pred resu;
	resu.TP = -1;
	resu.FP = -1;
	resu.TN = -1;
	resu.FN = -1;

	int TP = 0;
	for(size_t i = 0; i < plst.size(); i++)
	{
		string gene = plst[i];
		int idx = classification(gene, cons);
		if(cpt[idx].k0 <= cpt[idx].k1)
			TP++;
	}
	resu.TP = TP;
	resu.FN = (int)plst.size() - TP;

	int TN = 0;
	for(size_t i = 0; i < nlst.size(); i++)
	{
		string gene = nlst[i];
		int idx = classification(gene, cons);
		if(cpt[idx].k0 > cpt[idx].k1)
			TN++;
	}
	resu.TN = TN;
	resu.FP = (int)nlst.size() - TN;

	return resu;
}
Example #19
0
void printStat(vector<books_record> books)
{
    size_t counter = 1;
    size_t size = books.size();
    empty emptyF;
    empty emptyN;
    empty emptyISBN;
    vector <books_record>::iterator it = books.begin();
    while (it != books.end())
    {
        if (it->bookname == "")
        {
            emptyN.count++;
            emptyN.record.push_back(counter);
        }
        if (it->fio == "")
        {
            emptyF.count++;
            emptyF.record.push_back(counter);
        }
        if (it->isbn == "")
        {
            emptyISBN.count++;
            emptyISBN.record.push_back(counter);
        }
        counter++;
        it++;
    }
    
    cout << "размер: " << size << endl;
    cout << "кол-во без автора: " << emptyF.count << endl;
    cout << "кол-во без заглавия: " << emptyN.count << endl;            
    cout << "кол-во без isbn: " << emptyISBN.count << endl;    
}
Example #20
0
File: 12.cpp Project: Libaier/ABC
    void reOrderArray(vector<int> &array) {
        if(array.empty())
        {
            return ;
        }
        vector<int> odd,even;
 
        for (int i = 0; i < array.size(); ++i)
        {
            if (array[i]&0x1==1)
            {
                odd.push_back(array[i]);
            }else{
                even.push_back(array[i]);
            }
        }
        int i = 0;
        for (; i < odd.size(); ++i)
        {
            array[i] = odd[i];
        }
 
        for (int j = 0; j < even.size(); ++j,++i)
        {
            array[i] = even[j];
        }
    }
Example #21
0
bool SaveMat(const string& file_name, const Mat& mat,
             const vector<float>& param)
{
    FILE *out_file;
    string name = file_name;
    if ((out_file = fopen((name + FILE_EXT).c_str(), "w")) == nullptr)
    {
        return false;
    }

    fprintf(out_file, "%d\n", static_cast<int>(param.size()));
    for (size_t i = 0; i < param.size(); ++i)
    {
        fprintf(out_file, "%f ", param[i]);
    }
    fprintf(out_file, "\n");

    fprintf(out_file, "%d %d\n", mat.rows, mat.cols);
    for (int i = 0; i < mat.rows; ++i)
    {
        for (int j = 0; j < mat.cols; ++j)
        {
            fprintf(out_file, "%f ", mat.at<float>(i, j));
        }
        fprintf(out_file, "\n");
    }

    fclose(out_file);

    return true;
}
void quickSort(vector<int>& v)
{
    if (v.size() <= 1)
        return;
    else
        quickSortStep(v, 0, v.size()-1);
}
Example #23
0
void MySqlBasicData::push_back(vector<string>& v) throw(mysql_slopover)
{
	if(v.size() == 0 || v.size()!=_col.size()) {
		throw mysql_slopover("MySqlBasicData::push_back: num is not match");
	}
	_data.push_back(v);
}
Example #24
0
 FenwickTree (const vector<int>& v)
     :
     treep (v.size ())
 {
     for (size_t i = 0; i < v.size (); ++i)
         modify (i, v[i]);
 }
    int minPathSum(vector<vector<int> >& grid) {
        if(grid.size() <= 0 || grid[0].size() <= 0) return 0;

        int m = grid.size();
        int n = grid[0].size();
        vector<vector<int> > path(m, vector<int>(n, 0));

        path[0][0] = grid[0][0];

        for(int i=1; i<m; i++)
        {
            path[i][0] = path[i-1][0] + grid[i][0];
        }

        for(int i=1; i<n; i++)
        {
            path[0][i] = path[0][i-1] + grid[0][i];
        }

        int minSum = INT_MAX;
        for (int i = 1; i < m; ++i)
        {
            for (int j = 1; j < n; ++j)
            {
                path[i][j] = min(path[i-1][j], path[i][j-1]) + grid[i][j];
            }
        }

        

        return path[m-1][n-1];
    }
Example #26
0
int solution(vector<int> &A) {
    sort( A.begin(), A.end() );
    
    int distinct = 1;
    int previous = 0;
    
    if( A.size() > 0 )
    {
        previous = A[0];
    }
    else
    {
        return 0;
    }
    
    for( int i = 1; i < A.size(); i++ )
    {
        if( A[i] != previous )
        {
            previous = A[i];
            distinct++;
        }
    }
    
    return distinct;
}
Example #27
0
	FlujoCosto AumentarFlujo( int s, int t, Flujo f ){
		Costo1D dist( n, CINF );
		fill( prv.begin(), prv.end(), Par( -1, -1 ) );
		priority_queue <CostoNodo, vector<CostoNodo>, greater<CostoNodo> > pq;
		pq.push( FlujoCosto( 0, s ) ); dist[s] = 0;
		while( !pq.empty() ){
			int u = pq.top().second;
			Costo p = pq.top().first;
			pq.pop();
			if( !Igual( dist[u], p ) ) continue;
			for( int i = 0; i < aristas[u].size(); i++ ){
				AristaFlujo* v = aristas[u][i];
				if( v->flujo == v->cap ) continue;
				Costo ndist = dist[u] + v->npeso;
				if( !Igual( ndist, dist[v->dst] ) && ndist < dist[ v->dst ]){
					dist[ v->dst ] = dist[u] + v->npeso;
					pq.push( CostoNodo( ndist, v->dst ) );
					prv[ v->dst ].second = i;
					prv[v->dst].first = u;
				}
			}
		}
		if( Igual( dist[t], CINF ) ) return FlujoCosto( 0, 0 );
		RecalcularCosto( dist );
		return ActualizarFlujo( t, f );
	}
Example #28
0
void evaluate()
{
	//use binary search to look for the answer
	string str = "";
	for(int i =0  ; i < n ;i++)
		str += current[i];
	int lo = 0;
	int high = 4616;
	int mid;

	while(lo < high)
	{
		mid = (lo + high)/2;
		if(str > dic[mid]) lo = mid + 1;
		else if (str < dic[mid]) high = mid;
		else if (str == dic[mid])
		{
			answer.push_back(str);
			return;
		}
	}

	if(str == dic[mid]) answer.push_back(str);
	return;
}
Example #29
0
    int threeSumClosest(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());

        bool first = true;
        int closest;

        for (int i = 0; i < nums.size(); i++) {
            const int t = target - nums[i];
            int j = i + 1;
            int k = nums.size() - 1;
            while (j < k) {
                const int sum = nums[i] + nums[j] + nums[k];
                if (first || (abs(closest - target) > abs(sum - target))) {
                    closest = sum;
                    first = false;
                }
                if (nums[j] + nums[k] < t) {
                    j++;
                } else if (nums[j] + nums[k] > t) {
                    k--;
                } else {
                    return sum;
                }
            }
        }

        return closest;
    }
Example #30
0
	virtual bool fixProblem(unsigned index, unsigned fix_type, MapEditor* editor)
	{
		if (index >= sectors.size())
			return false;

		if (fix_type == 0)
		{
			// Browse textures
			MapTextureBrowser browser(theMapEditor, 1, "", map);
			if (browser.ShowModal() == wxID_OK)
			{
				// Set texture if one selected
				string texture = browser.getSelectedItem()->getName();
				editor->beginUndoRecord("Change Texture");
				if (floor[index])
					sectors[index]->setStringProperty("texturefloor", texture);
				else
					sectors[index]->setStringProperty("textureceiling", texture);

				editor->endUndoRecord();

				// Remove problem
				sectors.erase(sectors.begin() + index);
				floor.erase(floor.begin() + index);
				return true;
			}

			return false;
		}

		return false;
	}