Exemplo n.º 1
0
/*
 *  Repaint silhouette in color of background
 *
 *  @param  point Extreme point of silhouette
 *  @param  imageMatrix 2d array that contains color's
 *  @param  width The width of the image
 *  @param  height The height of the image
 *  @param  backgroundColor Variable that indicate background color
 */
void SilCounter::fillSilhouette(QPoint &point, int **imageMatrix, int width, int height, int backgroundColor){
    // queue that store all point neighbours, with silhouette color
    MyQueue<QPoint> queueOfPoints;

    // repaint point in background color to prevent it from re-adding to the queue from cycle
    imageMatrix[point.y()][point.x()] = backgroundColor;
    queueOfPoints.push(point);\

    // walk throw all point neigbours with silhouette color
    while(queueOfPoints.size()){

        // temporary variable for poin from queue
        QPoint temp = queueOfPoints.front();
        int x = temp.x();
        int y = temp.y();
        queueOfPoints.pop();\

        // walk throw all point neigbours with silhouette color repaint them and adding to the queue
        for(int i = -1; i <= 1; i++){
            for(int j = -1; j <= 1; j++){
                if( (0 <= x + i) && (x + i < width) && (0 <= y + j) && (y + j < height) && (imageMatrix[y + j][x + i] != backgroundColor)){
                    imageMatrix[y + j][x + i] = backgroundColor;
                    queueOfPoints.push(QPoint(x + i,y + j));
                }
            }
        }
    }
}
Exemplo n.º 2
0
Arquivo: 3_5.cpp Projeto: LarryJ/CTCI
int main()
{
  MyQueue q;
  cout << q.pop() << endl;
  q.push(1);
  q.push(10);
  cout << q.pop() << endl;
  cout << q.pop() << endl;
  return 0;
}
int main(){
    MyQueue<int> q;
    for(int i=0; i<10; ++i){
        q.push(i);
    }
    q.pop();
    q.pop();
    q.push(10);
    cout<< q.sintop() << ' ' << q.top() << endl;
    return 0;
}
int main (char *argv[], int argc) {
	MyQueue q;
	q.push(1);
	q.push(2);
	q.push(3);
	cout << q.front() << endl;
	cout << q.back() << endl;
	q.pop();
	cout << q.front() << endl;
	cout << q.back() << endl;
	return 0;
}
Exemplo n.º 5
0
int main(int argc, char const *argv[])
{
	MyQueue<int> q;
	for(int i = 0; i < 10; i++)
	{
		q.push(i);
	}
	cout<<q.front()<<" "<<q.rear()<<endl;
	q.pop();
	cout<<q.front()<<" "<<q.rear()<<endl;
	q.push(11);
	cout<<q.front()<<" "<<q.rear()<<endl;
	return 0;
}
Exemplo n.º 6
0
int main()
{
	MyQueue mq;
	mq.push(1);
	mq.push(2);
	cout << mq.pop() << endl;
	mq.push(3);
	cout << mq.pop() << endl;

	cout << mq.pop() << endl;

	system("pause");
	return 0;
}
Exemplo n.º 7
0
/* This function takes a picture and make from it filled grid. Each element of
 * grid is image`s pixel. Grid is filled with boolean values. If it is a black
 * point, it will be marked as true, other - false.
 */
void imageParsing(string fileName) {
    string fileAdress = "images/";
    fileAdress = fileAdress + fileName;
    GBufferedImage image;
    image.load(fileAdress);
    pixels.resize(image.getWidth(), image.getHeight());
    for (int column = 0; column < image.getWidth(); column++) {
        for (int row = 0; row < image.getHeight(); row++) {
            if (image.getRGB(column, row) <= GREY) {
                pixels.set(column, row, true);
            } else {
                pixels.set(column, row, false);
            }
        }
    }
    for (int column = 0; column < pixels.nCols; column++) {
        for (int row = 0; row < pixels.nRows; row++) {
            if (pixels.inBounds(column, row) && pixels.get(column, row) == true) {
                pixels.set(column, row, false);
                Points blackPoint = makePoint(column, row);
                checkAllNeighbours.push(blackPoint);
                detectingSilouettes(pixels);
            }
        }
    }
}
Exemplo n.º 8
0
void test2()
{
    MyQueue<int> que;

    for (int i = 0; i < 5; i++)
    {
        que.push(i);
    }

    //empty()

    cout << "empty = " << que.empty() << endl;


//
    //pop()
    que.pop();
    que.pop();
    que.pop();


    //back()
    int temp_back = que.back();
    cout << "temp_back = " << temp_back << endl;

    //front()
    int temp_front = que.front();
    cout << "temp_front = " << temp_front << endl;
}
Exemplo n.º 9
0
int main() {
    MyQueue<int> que;
    que.push(1);
    que.push(2);
    que.push(3);
    que.push(4);
    while(que.size() != 0) {
        cout<<que.pop()<<" ";
    }
    que.push(5);
    que.push(6);
    while(que.size() != 0) {
        cout<<que.pop()<<" ";
    }
    cout<<endl;
    que.pop();
    return 0;
}
Exemplo n.º 10
0
int main() {
    MyQueue q;
    int t, type, data;
    cin >> t;
    for (int i = 0; i < t; i++) {
        cin >> type;
        if (type == 1) {
            cin >> data;
            q.push(data);
        } else if (type == 2) {
Exemplo n.º 11
0
int main(int argc,char*argv[]){
	MyQueue mq;
	for(int i=1;i<=5;++i)
		mq.push(i);
	for(int i=1;i<=6;++i){
		cout << "front: " << mq.front() << endl;
		cout << "back: " << mq.back() << endl;
		mq.pop();
	}
	return 0;
}
Exemplo n.º 12
0
int main() {
    MyQueue mq;
    mq.push(1);
    mq.push(2);
    mq.push(3);
    mq.push(4);
    mq.push(5);
    mq.push(6);

    cout << "size: " << mq.size() << endl;

    cout << "peek: " << mq.peek() << endl;
    cout << mq.pop() << endl;
    cout << mq.pop() << endl;
    mq.push(100);
    cout << mq.pop() << endl;
    cout << mq.pop() << endl;
    cout << mq.pop() << endl;
    cout << mq.pop() << endl;
    cout << mq.pop() << endl;
}
Exemplo n.º 13
0
int C3Q5()
{
    MyQueue mq;
    int pushArray[3] = { 10, 6, 3 };
    int pullArray[3] = { 8, 4, 7 };

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < pushArray[i]; j++) {
            mq.push(j);
            //printf("push %d into queue", j);
        }

        for (int j = 0; j < pullArray[i]; j++) {
            printf("pull %d out\n", mq.pull());
        }
    }
    
    return 0;
}
Exemplo n.º 14
0
int main()
{
	MyQueue *q;
	int n, m;
	int i;
	char s[20];
	int val;

	while (scanf("%d%d", &n, &m) == 2) {
		q = new MyQueue(m);
		for (i = 0; i < n; ++i) {
			scanf("%s", s);
			if (strcmp(s, "Push") == 0) {
				scanf("%d", &val);
				if (q->full()) {
					printf("failed\n");
				} else {
					q->push(val);
				}
			} else if (strcmp(s, "Pop") == 0) {
				if (q->empty()) {
					printf("failed\n");
				} else {
					q->pop();
				}
			} else if (strcmp(s, "Query") == 0) {
				scanf("%d", &val);
				if (val < 1 || val > q->size()) {
					printf("failed\n");
				} else {
					printf("%d\n", q->front(val));
				}
			} else if (strcmp(s, "Isempty") == 0) {
				printf(q->empty() ? "yes\n" : "no\n");
			} else if (strcmp(s, "Isfull") == 0) {
				printf(q->full() ? "yes\n" : "no\n");
			}
		}
		delete q;
	}

	return 0;
}
Exemplo n.º 15
0
/* This function takes filled grid with boolean values. Black points are marked
 * as true, other - as false. If number of neighbour black points more than
 * const MIN_SILOUETTES_SIZE, such area is marked is silouette.
 * Function returns the number of silouettes.
 */
void detectingSilouettes(Grid<bool> &pixels) {
    int countBlackPoints;
    while (!checkAllNeighbours.empty()) {
        Points buffer = checkAllNeighbours.front();
        checkAllNeighbours.pop();
        for (int x = buffer.column - 1; x <= buffer.column + 1; x++) {
            for (int y = buffer.row - 1; y <= buffer.row + 1; y++) {
                if (pixels.inBounds(x, y) && pixels.get(x, y) == true) {
                    countBlackPoints++;
                    Points blackPoint = makePoint(x, y);
                    pixels.set(x, y, false);
                    checkAllNeighbours.push(blackPoint);
                }
            }
        }
    }
    if (countBlackPoints >= MIN_SILOUETTES_SIZE) {
        countSilouettes++;
        countBlackPoints = 0;
    }
}
Exemplo n.º 16
0
void Test_7_QueueWithTwoStack()
{
	MyQueue q;
	try
	{
		cout << "top is " << q.top() << endl;
		cout << "size is " << q.size() << endl;
	}
	catch (exception &e)
	{
		cout << e.what() << endl;
	}

	q.push(1);
	try
	{
		cout << "top is " << q.top() << endl;
		cout << "size is " << q.size() << endl;
	}
	catch (exception &e)
	{
		cout << e.what() << endl;
	}
	q.push(2);
	q.push(3);
	try
	{
		cout << "top is " << q.top() << endl;
		cout << "size is " << q.size() << endl;
	}
	catch (exception &e)
	{
		cout << e.what() << endl;
	}

	try
	{
		q.pop();
		cout << "top is " << q.top() << endl;
		cout << "size is " << q.size() << endl;
	}
	catch (exception &e)
	{
		cout << e.what() << endl;
	}

	try
	{
		q.pop();
		cout << "top is " << q.top() << endl;
		cout << "size is " << q.size() << endl;
	}
	catch (exception &e)
	{
		cout << e.what() << endl;
	}

	try
	{
		q.pop();
		cout << "top is " << q.top() << endl;
		cout << "size is " << q.size() << endl;
	}
	catch (exception &e)
	{
		cout << e.what() << endl;
	}

	try
	{
		q.pop();
		cout << "top is " << q.top() << endl;
		cout << "size is " << q.size() << endl;
	}
	catch (exception &e)
	{
		cout << e.what() << endl;
	}
}
Exemplo n.º 17
0
void test(){
	
	int seq [6] = {0,0,0,0,0,0};
	Board * start ;
	Board * current;
	int record=0;
	initialize();
	int step_size =0 ;
	while(gen_next_recur(seq)){
		record ++ ;
		//if(record < 20)
		//	continue;
		print_seq(seq);
		for(int piindex = 0 ; piindex < PATTERN_NUM ;piindex++){
			start = new Board();
			start->init_with_sequence(seq,piindex);
			open_list.push(start);
			step_size =0;	
			
			while(!open_list.empty()){
				current = open_list.top();
				open_list.pop();
				close_list.push(current);
				//current->print();
				if(current->is_goal()){
					printf("Goal Found for pattern %d , step =%d\n",piindex,current->step);
					//current->print();
					break;
				}
				if(step_size<current->step){
					step_size = current->step;
					printf("%d\n",step_size);
				}
				for(int tile_id =0 ;tile_id < PATTERN_SIZE ;tile_id++){
					for (int direc =0; direc < 4 ;direc++){
						Board * tmp = current->gen_board(tile_id,direc);
						if(tmp){
							if(!close_list.exist(tmp)&&!open_list.exist(tmp)){	
								open_list.push(tmp);
							}else
								delete tmp;
						}
					}
				}
			}

			write_db(seq,piindex,current->step);
			
			while(!open_list.empty()){
				current = open_list.top(); 
				open_list.pop();
				delete current;
			}
			
			while(!close_list.empty()){
				current = close_list.top();
				close_list.pop();
				delete current;
			}
			//break;
		}
		//break;
	}
}