Exemplo n.º 1
0
/**
 * Counts all the occupied squares connected to a certain position in the
 * grid inclusive, INCLUDING facilities under construction.
 * Mostly used to ensure a base stays connected to the Access Lift.
 * -1 = Unoccupied, 0 = Occupied, 1 = Connected.
 * @param x X position in grid.
 * @param y Y position in grid.
 * @param grid Pointer to connection grid (Null to create one from scratch).
 * @param remove Facility to ignore (in case of facility dismantling).
 * @return Number of squares connected to the starting position.
 */
int BaseView::countConnected(int x, int y, int **grid, BaseFacility *remove) const
{
	bool newgrid = (grid == 0);

	// Create connection grid
	if (newgrid)
	{
		grid = new int*[BASE_SIZE];

		for (int xx = 0; xx < BASE_SIZE; ++xx)
		{
			grid[xx] = new int[BASE_SIZE];
			for (int yy = 0; yy < BASE_SIZE; ++yy)
			{
				if (_facilities[xx][yy] == 0 || _facilities[xx][yy] == remove)
				{
					grid[xx][yy] = -1;
				}
				else
				{
					grid[xx][yy] = 0;
				}
			}
		}
	}

	if (x < 0 || x >= BASE_SIZE || y < 0 || y >= BASE_SIZE || grid[x][y] != 0)
	{
		return 0;
	}

	// Add connected (neighbor) facilities to grid
	int total = 1;
	grid[x][y]++;

	if (0 == _facilities[x][y]->getBuildTime()
	|| (x-1>=0 && 0!=_facilities[x-1][y] && (_facilities[x-1][y] == _facilities[x][y] || _facilities[x-1][y]->getBuildTime() > _facilities[x-1][y]->getRules()->getBuildTime())))
		total += countConnected(x - 1, y, grid, remove);
	if (0 == _facilities[x][y]->getBuildTime()
	|| (y-1>=0 && 0!=_facilities[x][y-1] && (_facilities[x][y-1] == _facilities[x][y] || _facilities[x][y-1]->getBuildTime() > _facilities[x][y-1]->getRules()->getBuildTime())))
		total += countConnected(x, y - 1, grid, remove);
	if (0 == _facilities[x][y]->getBuildTime()
	|| (x+1<BASE_SIZE && 0!=_facilities[x+1][y] && (_facilities[x+1][y] == _facilities[x][y] || _facilities[x+1][y]->getBuildTime() > _facilities[x+1][y]->getRules()->getBuildTime())))
		total += countConnected(x + 1, y, grid, remove);
	if (0 == _facilities[x][y]->getBuildTime()
	|| (y+1<BASE_SIZE && 0!=_facilities[x][y+1] && (_facilities[x][y+1] == _facilities[x][y] || _facilities[x][y+1]->getBuildTime() > _facilities[x][y+1]->getRules()->getBuildTime())))
		total += countConnected(x, y + 1, grid, remove);

	// Delete connection grid
	if (newgrid)
	{
		for (int xx = 0; xx < BASE_SIZE; ++xx)
		{
			delete[] grid[xx];
		}
		delete[] grid;
	}

	return total;
}
//All cycle check function
//Check cycle exist on graphs with connected componentsd
bool Cyclic::isCyclic(bool connected_flag){
	bool return_value=false	;
	int size=countConnected();
	for(size_t index=0;index<size;index++){
		return_value=return_value||isCyclic(bfs_roots[index]);
	}
	return return_value;
}
//Check whether C3 exists or connected components > = 3 for ramsey
bool Cyclic::existC3(){
	if(countConnected()>=3)		//for ramsey enemy case
		return true;
	vector<vector<bool>> nt=nontree;
	for(int i=0;i<adjacencyList.size();i++){
		for(int j=0;j<adjacencyList[i].size();j++){
			if(nt[i][adjacencyList[i][j]->key]){
				//cout<<"Non tree edge : "<<i+1<<","<<adjacencyList[i][j]->key+1<<endl;
				if(vertices[i].parent==vertices[adjacencyList[i][j]->key].parent)//non tree edge with sameparent C3 exists
					return true;
			}
		}
	}
	return false;	//No C3
}
Exemplo n.º 4
0
/**
 * Counts all the occupied squares connected to a certain position in the
 * grid inclusive, but ignoring facilities under construction.
 * Mostly used to ensure a base stays connected to the Access Lift.
 * -1 = Unoccupied, 0 = Occupied, 1 = Connected.
 * @param x X position in grid.
 * @param y Y position in grid.
 * @param grid Pointer to connection grid (Null to create one from scratch).
 * @param remove Facility to ignore (in case of facility dismantling).
 * @return Number of squares connected to the starting position.
 */
int BaseView::countConnected(int x, int y, int **grid, BaseFacility *remove) const
{
	bool newgrid = (grid == 0);

	// Create connection grid
	if (newgrid)
	{
		grid = new int*[BASE_SIZE];

		for (int xx = 0; xx < BASE_SIZE; ++xx)
		{
			grid[xx] = new int[BASE_SIZE];
			for (int yy = 0; yy < BASE_SIZE; ++yy)
			{
				if (_facilities[xx][yy] == 0 || _facilities[xx][yy] == remove)
				{
					grid[xx][yy] = -1;
				}
				else
				{
					grid[xx][yy] = 0;
				}
			}
		}
	}

	if (x < 0 || x >= BASE_SIZE || y < 0 || y >= BASE_SIZE || grid[x][y] != 0)
	{
		return 0;
	}

	// Add connected facilities to grid
	int total = 1;
	grid[x][y]++;

	// Check for facilities under construction bigger than a square
	if (_facilities[x][y]->getBuildTime() > 0)
	{
		if (x > 0 && _facilities[x - 1][y] == _facilities[x][y])
		{
			total += countConnected(x - 1, y, grid, remove);
		}
		if (y > 0 && _facilities[x][y - 1] == _facilities[x][y])
		{
			total += countConnected(x, y - 1, grid, remove);
		}
		if (x < BASE_SIZE - 1 && _facilities[x + 1][y] == _facilities[x][y])
		{
			total += countConnected(x + 1, y, grid, remove);
		}
		if (y < BASE_SIZE - 1 && _facilities[x][y + 1] == _facilities[x][y])
		{
			total += countConnected(x, y + 1, grid, remove);
		}
		return total;
	}

	total += countConnected(x - 1, y, grid, remove);
	total += countConnected(x, y - 1, grid, remove);
	total += countConnected(x + 1, y, grid, remove);
	total += countConnected(x, y + 1, grid, remove);

	// Delete connection grid
	if (newgrid)
	{
		for (int xx = 0; xx < BASE_SIZE; ++xx)
		{
			delete[] grid[xx];
		}
		delete[] grid;
	}

	return total;
}