int Stack::top(){
    if(!q1.empty()){
        while(q1.size() > 1){
            int temp = q1.front();
            q1.pop();
            q2.push(temp);
        }
        int res = q1.front();
        q1.pop();
        q2.push(res);
        return res;
    }
    else if(!q2.empty()){
        while(q2.size() > 1){
            int temp = q2.front();
            q2.pop();
            q1.push(temp);
        }
        int res = q2.front();
        q2.pop();
        q1.push(res);
        return res;
    }
    else
        return -1;
}
Example #2
0
	// Get the top element.
	int top() {
		int result = 0;
		if (!q1.empty()){
			while (q1.size() != 1){
				int tmp = q1.front();
				q1.pop();
				q2.push(tmp);
			}
			result = q1.front();
			q1.pop();
			q2.push(result);
		}
		else{
			if (!q2.empty()){
				while (q2.size() != 1){
					int tmp = q2.front();
					q2.pop();
					q1.push(tmp);
				}
				result = q2.front();
				q2.pop();
				q1.push(result);
			}
		}
		return result;
	}
void bfs(int sr, int sc) {
    Q.push(ii(sr, sc));
    dist[sr][sc] = 0;
    ii p, q;
    int nr, nc, jr, jc;

    // go directly
    p = Q.front();
    for(int i = 0; i < 3; ++i) {
        nr = p.first+ gdr[i],    nc = p.second + gdc[i];
        if(nr >= 1 && nr <= R && nc >= 1 && nc <= C && !piece[nr][nc]) {
            ans.push_back(make_tuple(nr, nc, 1));
        }
    }

    while(!Q.empty()) {
        p = Q.front();
        Q.pop();
        // jump
        for(int i = 0; i < 5; ++i) {
            nr = p.first+ jdr[i],    nc = p.second + jdc[i];
            if(nr >= 1 && nr <= R && nc >= 1 && nc <= C && piece[nr][nc]) {
                jr = nr + jdr[i],    jc = nc + jdc[i];
                if(jr >= 1 && jr <= R && jc >= 1 && jc <= C && !piece[jr][jc] && dist[jr][jc] == UNVISITED) {
                    Q.push(ii(jr, jc));
                    dist[jr][jc] = dist[p.first][p.second] + 1;
                    ans.push_back(make_tuple(jr, jc, dist[jr][jc]));
                }
            }
        }

    }

}
int Translate( string text ){
	int i ;
	int data , value ;
	while(!s.empty())//Initial
	s.pop() ;
	while(!q.empty())
	q.pop() ; 
	for( i = text.length() - 1 ; i >= 0 ; i-- ){
	Push(m[text[i]]) ;
	while(q.size() >= 3){
		data = 0 ;
		for( value = 1 ; value <= 4 ; value *= 2 ){
			if(q.front() == 1)
				data += value ;
				q.pop() ;
		}
			s.push(data) ;
		}
	}
	data = 0 ;
	value = 1 ;
	while( !q.empty() ){
		if( q.front() == 1 )
		data += value  ;
		value *= 2 ;
		q.pop() ;
	}
	s.push(data) ;
}
Example #5
0
/* tournament() encompasses the fighting between the two lineups for the
	tournament. Pits the first Creatures in the queues against each other, and
	puts the winner back in its team's queue and the loser to its team's loser
	stack.
	Input: first team's queue of Creature pointers, first team's stack of loser
		Creature pointers, second team's queue of Creature pointers, second
		team's stack of loser Creature pointers
	Output: none */
void tournament(queue<Creature*>& lineup1, stack<Creature*>& loser1,
	queue<Creature*>& lineup2, stack<Creature*>& loser2)
{
	cout << "Let the tournament begin!" << endl << endl;
	while (lineup1.size() > 0 && lineup2.size() > 0)
	{
		Creature* unit1 = lineup1.front();
		Creature* unit2 = lineup2.front();
		lineup1.pop();
		lineup2.pop();
		/* If I interpreted Uli's post on the Assignment 4 discussion
			correctly, I can statically allocate Creature pointers, which
			is what I'm doing here. The queues were dynamically generated at
			the beginning. */

		combat(unit1, unit2);
		cout << "This round is over!" << endl << endl << endl;

		if (unit1->isAlive())
		{
			lineup1.push(unit1);
			loser2.push(unit2);
		}
		else
		{
			loser1.push(unit1);
			lineup2.push(unit2);
		}
	}
	// // debugging
	// cout << lineup1.size() << " " << lineup2.size() << endl;
	// cout << loser1.size() << " " << loser2.size() << endl;
}
Example #6
0
int main() {
    int n, q, sum = 0, nz = 0;
    scanf("%d%d", &n, &q);
    for(int i = 0; i < q; ++i) {
        scanf("%d%d", &t, &x);
        x -= 1;
        if(t == 1) {
            sum += 1;
            ar[x].push(nz), main_.push({nz++, x});
        }
        else if(t == 2) {
            sum -= ar[x].size();
            while(!ar[x].empty()) {
                int xs = ar[x].front();
                used[xs] = 1;
                ar[x].pop();
            }
        }
        else {
            while(!main_.empty() && x >= main_.front().first) {
                int i = main_.front().first,
                    xs = main_.front().second;
                main_.pop();
                if(!used[i]) {
                    used[i] = 1;
                    ar[xs].pop();
                    sum -= 1;
                }
            }
        }
        printf("%d\n", sum);
    }
}
Example #7
0
int Arbol::resolverEcuacion(queue<int> constantes, queue<char> operadores)
{
    while(!operadores.empty() && !constantes.empty())
    {
        switch(operadores.front())
        {
        case 'x' :
            respuesta *= constantes.front();
            constantes.pop();
            operadores.pop();
            break;
        case '/' :
            respuesta /= constantes.front();
            constantes.pop();
            operadores.pop();
            break;
        case '-' :
            respuesta -= constantes.front();
            constantes.pop();
            operadores.pop();
            break;
        case '+' :
            respuesta += constantes.front();
            constantes.pop();
            operadores.pop();
            break;

        }
    }
    return respuesta;
}
Example #8
0
void jmxj(wchar_t ch){
    int cond = check_jamo(ch);
    if(cond == JA){
        cout<<"/";
        while (!buf.empty()){
            putwchar(buf.front());
            buf.pop();
        }
        buf.push(ch);
        state = J;
    }
    else if (cond == MO){
        cout<<"/";
        while (buf.size()!=1){
            putwchar(buf.front());
            buf.pop();
        }
        buf.push(ch);
        state = JM;
    }
    else{
        buf.push(ch);
        cout<<"/";
        while (!buf.empty()){
            putwchar(buf.front());
            buf.pop();
        }
        cout<<"/";
        buf.push(ch);
        state = START;
    }
}
Example #9
0
relation DBMS::atomicExpr(queue<Token>& params){
	relation tempRelation;
	vector<relation>::iterator it;
	switch(params.front().kind){
	case STRING:
		for (it= queryResults.begin(); it < queryResults.end(); it++){
			if ( it -> name == params.front().value){
				tempRelation= *it;
				break;
			}
		}
		if(it==queryResults.end()){
			cout<<"Error: Table "<<params.front().value<<" does not exist in memory."<<endl;
			throw::invalid_argument("Improper query");
		}
		params.pop();//STRING
		break;
	case LEFT_PAREN:
		params.pop();//LEFT_PAREN
		tempRelation= expr(params);
		params.pop();//RIGHT_PAREN
		break;
	}
	return tempRelation;
}
Example #10
0
Url * pop_ourlqueue()
{
    Url *url = NULL;
    pthread_mutex_lock(&oq_lock);
    if (!ourl_queue.empty()) {
        url = ourl_queue.front();
        ourl_queue.pop();
        pthread_mutex_unlock(&oq_lock);
        return url;
    } else {
        int trynum = 3;
        struct timespec timeout;
        while (trynum-- && ourl_queue.empty()) {
            get_timespec(&timeout, 500); /* 0.5s timeout*/
            pthread_cond_timedwait(&oq_cond, &oq_lock, &timeout);
        }

        if (!ourl_queue.empty()) {
            url = ourl_queue.front();
            ourl_queue.pop();
        }
        pthread_mutex_unlock(&oq_lock);
        return url;
    }
}
Example #11
0
File: C.cpp Project: S7012MY/Surse
int main() {
  for(cin>>t;t;--t) {
    cin>>n>>m;
    ++n;
    r=(MULT);
    for(int i=0; i<=n; ++i) for(int j=0; j<=n; ++j) dmin[i][j]=(MULT);
    for(int i=0; i<(1<<n); ++i) for(int j=0; j<=n; ++j) bst[i][j]=(MULT);
    for(int i=0; i<m; ++i) {
      int a,b; long long c;
      cin>>a>>b>>c;
      dmin[a][b]=dmin[b][a]=min(dmin[a][b],c);
    }
    for(int k=0; k<n; ++k) for(int i=0; i<n; ++i) for(int j=0; j<n; ++j)
      dmin[i][j]=min(dmin[i][j],dmin[i][k]+dmin[k][j]);
    bst[0][0]=0;
    --n;
    for(c.push(mp(0,0));c.size();c.pop()) {
      int state=c.front().x,lst=c.front().y;
     // cout<<state<<' '<<count(state)<<' '<<lst<<' '<<bst[state][lst]<<'\n';
      int ram=n-count(state);
      if(count(state)==n) {
        r=min(r,bst[state][lst]);
      }
      for(int i=0; i<n; ++i) {
        if(!(state&(1<<i)) && bst[state|(1<<i)][i+1]>bst[state][lst]+ram*1LL*dmin[lst][i+1]) {
          bst[state|(1<<i)][i+1]=bst[state][lst]+ram*1LL*dmin[lst][i+1];
          c.push(mp((state|(1<<i)),i+1));
        }
      }
    }
    if(r==(MULT)) cout<<"-1";
    else cout<<r;
    if(t>1) cout<<'\n';
  }
}
bool bfs()
{
    int a = q.front().first, _v = q.front().second, d, v;
    q.pop();
    if (A[a] == _v)
        v = B[a], d = da[a];
    else
        v = A[a], d = db[a];
    if (v == b)
    {
        cout << d << '\n';
        return 1;
    }
    forauto(i, o[v])
    {
        if (!g(t[a] - t[*i]))
            continue;
        if (A[*i] == v)
        {
            if (!da[*i])
                da[*i] = d + 1, q.push(make_pair(*i, A[*i]));
        }
        else
            if (!db[*i])
                db[*i] = d + 1, q.push(make_pair(*i, B[*i]));
    }
    return 0;
}
Example #13
0
    bool isSymmetric(TreeNode *l, TreeNode *r)
    {
        q1.push(l);
        q2.push(r);

        TreeNode *l1, *h1;

        while(!q1.empty() && !q2.empty())
        {
            l1 = q1.front();
            h1 = q2.front();

            q1.pop();
            q2.pop();

            if(!l1 && !h1)
                continue;

            if(!l1 || !h1 || (l1->val != h1->val))
                return false;

            q1.push(l1->left);
            q1.push(l1->right);

            q2.push(h1->right);
            q2.push(h1->left);
        }

        return true;
    }
Example #14
0
//魔板操作A
void moveA() {
    int count = que.front().count;
    string op = que.front().op;
    string a = que.front().b;
    string b = que.front().a;
    moban tmp = moban(a, b, count+1, op+"A");
    if (cut_leaf(tmp)) que.push(tmp);
}
Example #15
0
 int next() {
     vector<int>::iterator it = q.front().first;
     vector<int>::iterator end = q.front().second;
     q.pop();
     int val = *it;
     it++;
     if (it != end) q.push(make_pair(it, end));
     return val;
 }
Example #16
0
File: 1726.cpp Project: yuseok89/PS
int main(void) {
	int i, j, n, m;
	int a, b, c;

	ios::sync_with_stdio(false);

	cin >> n >> m;

	for (i = 1; i <= n; i++) {
		for (j = 1; j <= m; j++)
			cin >> A[i][j];
	}

	for (i = 0; i <= n + 1; i++)
		A[i][0] = A[i][m + 1] = 1;
	for (i = 0; i <= m + 1; i++)
		A[0][i] = A[n + 1][i] = 1;

	cin >> a >> b >> c;

	Q.push(make_pair(make_pair(a, b), --c));
	D[a][b][c] = 1;

	cin >> a >> b >> c;
	c--;

	while (!Q.empty() && D[a][b][c] == 0) {
		int q1 = Q.front().first.first;
		int q2 = Q.front().first.second;
		int q3 = Q.front().second;

		Q.pop();

		for (i = 1; i <= 3; i++) {
			if (A[q1 + dir[q3][0] * i][q2 + dir[q3][1] * i] == 1)
				break;

			if (D[q1 + dir[q3][0] * i][q2 + dir[q3][1] * i][q3] == 0) {
				D[q1 + dir[q3][0] * i][q2 + dir[q3][1] * i][q3] = D[q1][q2][q3] + 1;
				Q.push(make_pair(make_pair(q1 + dir[q3][0] * i, q2 + dir[q3][1] * i), q3));
			}
		}

		j = (q3 == 0 || q3 == 1) ? 2 : 0;

		for (i = 0; i < 2; i++, j++) {
			if (D[q1][q2][j] == 0) {
				D[q1][q2][j] = D[q1][q2][q3] + 1;
				Q.push(make_pair(make_pair(q1, q2), j));
			}
		}
	}

	cout << D[a][b][c] - 1 << endl;

	return 0;
}
 /** Removes the element on top of the stack and returns that element. */
 int pop() {
     queue<int> tmpQue;
     while(que.size() > 1) {
         last = que.front();
         tmpQue.push(que.front());
         que.pop();
     }
     swap(tmpQue, que);
     return tmpQue.front();
 }
Example #18
0
void printHistory(queue<string> printQ) {
    int n = printQ.size();
    for(int i = 0; i < n ; i++) {
        char c = i + 48;
        write(STDOUT_FILENO, &c, 1);
        write(STDOUT_FILENO, " ", 1);
        write(1, printQ.front().data(), printQ.front().length());   //loop should output strings instead of chars when combined with felix
        write(1, "\n", 1);
        printQ.pop();
    }
}
 // Push element x onto stack.
 void push(int x) {
     while(!que1.empty()) {
         que2.push(que1.front());
         que1.pop();
     }
     que1.push(x);
     while(!que2.empty()) {
         que1.push(que2.front());
         que2.pop();
     }
 }
 // Removes the element on top of the stack.
 void pop() {
     while( !rse.empty()){
         tmp.push(rse.front());
         rse.pop();
     }
     while(tmp.size()-1 !=0){
         rse.push(tmp.front());
         tmp.pop();
     }
     tmp.pop();
 }
Example #21
0
File: RADIX.cpp Project: maldo/ADA
//NOTA: queue no soporta iterator
void print_queue(queue<string> q)
{
	size_t total = q.size();
	for (size_t x = 0; x<total-1; ++x)
	{
		cout<<q.front()<<" ";
		q.pop();
	}
	cout<<q.front();
	cout<<endl;
}
Example #22
0
bool stoppingCondition(queue<float> &objvals){
    vector<float> vals;float minval=objvals.front();float maxval=objvals.front();
    while(!objvals.empty()){
	float val = objvals.front();
	objvals.pop();vals.push_back(val);
	if(val < minval) minval = val;
	if(val > maxval) maxval = val;
    }
    for(unsigned int i = 0; i < vals.size(); i++) objvals.push(vals[i]);
    if(((maxval - minval)/maxval) < eps) return true;
    else return false;
}
Example #23
0
//魔板操作C
void moveC() {

    int count = que.front().count;
    string op = que.front().op;
    string a = que.front().a;
    string b = que.front().b;
    char t = a[1];
    a[1] = b[1];
    b[1] = b[2];
    b[2] = a[2];
    a[2] = t;
    moban tmp = moban(a, b, count+1, op+"C");
    if (cut_leaf(tmp)) que.push(tmp);
}
Example #24
0
int main()
{
	int t; scanf("%d", &t);
	while(t--)
	{
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= m; ++j)
			{
				scanf(" %c", &c[i][j]);
				if(c[i][j] == '$')
				{
					state[i][j] = '1';
					q.push(node(i, j, 0));
				}
			}

		char dosao = 0;

		while(!q.empty())
		{
			int x = q.front().x;
			int y = q.front().y;
			int d = q.front().d;
			q.pop();

			if(c[x][y] == '#')
			{
				printf("%d\n", d);
				dosao = '1';
				break;
			}

			for(int k = 0; k < 4; ++k)
				if(!state[x + dx[k]][y + dy[k]] && c[x + dx[k]][y + dy[k]] != '*' && c[x + dx[k]][y + dy[k]])
				{
					q.push(node( x+ dx[k], y + dy[k], d + 1));
					state[x + dx[k]][y + dy[k]] = '1';
				}
		}

		if(!dosao)
			printf("-1\n");

		while(!q.empty()) q.pop();
		memset(c, 0, sizeof(c));
		memset(state, 0, sizeof(state));
	}
	return 0;
}
Example #25
0
int main(){
	freopen("telecow.in", "r", stdin);
	freopen("telecow.out", "w", stdout);
	scanf("%d%d%d%d", &N, &M, &Source, &Sink);
    	while(M--){
       		scanf("%d%d",&x,&y);
		build(x, y);
    	}
	for(int i = 1; i <= N * 2; i++) //复制现场
		for(edge *e = EDGE[i]; e; e = e->next)
			anse[e->x][e->y] = e->cap;
	while(RefreshDistLabel()) FindMaxFlow();
	printf("%lld\n", MAXF = totalflow);
	for(int k = 1; k <= N * 2; k++) //恢复现场
		for(edge *e = EDGE[k]; e; e = e->next)
			e->cap = anse[e->x][e->y];

	for(int i = 1; i <= N; i++){	//枚举每一个点
		if(i == Sink || i == Source) continue;
		for(edge *e = EDGE[i]; e; e = e->next){
			if(e->y - e->x == N){
				temp = e->cap;
				e->cap = 0, totalflow = 0;
				while(RefreshDistLabel()) FindMaxFlow();
				if(totalflow == MAXF - 1){
					anse[e->x][e->y] = 0;
					que.push(i);
					MAXF = totalflow;
					if(MAXF == 0 && que.size()){
						if(que.size()){
							printf("%d", que.front());
							que.pop();
						}
						while(que.size()){
							printf(" %d", que.front());
							que.pop();
						}
						printf("\n");
						return 0;
					}
				}
				for(int k = 1, tot = 0; k <= N * 2; k++) //恢复现场
					for(edge *e = EDGE[k]; e; e = e->next)
						e->cap = anse[e->x][e->y];
			}
		}
	}
	return 0;
}
Example #26
0
void exec_flags(unsigned flags, queue<string> &paths) {
    while (!paths.empty()) {
        DIR *dirp;
        vector<string> files;
        if ((dirp = opendir((paths.front()).c_str())) == NULL) {
            cout << "cannot access " << paths.front() << " - " << flush;
            perror("OPENDIR");
            paths.pop();
        }
        else {
            string curr_directory = paths.front();
            // cout << "GOING TO DIRECTORY: " << curr_directory << endl;

            if (flag_ALL(flags)) {
               push_all_to_files(dirp, files);
            }
            else {
               push_to_files(dirp, files);

            }
            sort_vector(files);

            if (flag_LONG(flags)) {
                /* too lazy to optimize */
                unsigned blocks = calc_blocks(files, curr_directory);
                cout << "total " << blocks << endl;
                print_long(files, curr_directory);
            }
            else {
                print_short(files, curr_directory);
            }

            if (flag_RECURSIVE(flags)) {
                get_directories(curr_directory, paths, files);
                paths.pop();
                if (!paths.empty()) {
                    cout << endl;
                }
                exec_flags(flags, paths);
            }
            else {
                paths.pop();
            }
            //if (closedir(dirp) == -1) {
            //    perror("CLOSEDIR");
            //}
        }
    }
}
Example #27
0
void DBMS::writeTable(queue<Token> params){
	FileIO fileManager;
	vector<relation>::iterator it;
	for (it= queryResults.begin(); it < queryResults.end(); it++){
		if (it->name == params.front().value){
			fileManager.printToFile(*it);
			break;
		}
	}
	if(it==queryResults.end()){
		cout<<"Table "<<params.front().value<<" is not charged in memory."<<endl;
		return;
	}
	params.pop();
}
Example #28
0
int main(){
	int n;
	scanf("%d",&n);
	for(int i = 1 ; i <= n ; i++) q.push(i);

	while(true){
		if(q.size() == 1){
			printf("%d",q.front());
			break;
		}
		q.pop();
		int back = q.front(); q.pop(); q.push(back);
	}
	return 0;
}
Example #29
0
int EK(int s, int t){
	int f = 0;
	CLEAR(pre, 0);
	CLEAR(flow, 0);
	while(!que.empty()) que.pop();

	while(1){
		CLEAR(a, 0);
		a[s] = INF;
		que.push(s);
		while(!que.empty()){
			int u = que.front(); que.pop();
			REP(n){
				if(!a[i] && cap[u][i] > flow[u][i]){
					pre[i] = u;
					que.push(i);
					a[i] = min(a[u], cap[u][i] - flow[u][i]);//路径上最小的流量
				}
			}
		}
		if(a[t] == 0) break;//未找到增广路
		for(int u=t; u!=s; u = pre[u]){
			flow[pre[u]][u] += a[t];
			flow[u][pre[u]] -= a[t];
		}
		f += a[t];
	}
	return f;
}
Example #30
0
int main()
{
    int T, R, C;
    cin>>T;
    while(T--)
    {
        cin>>R>>C;
        Init();
        Q.push(pp(0,0));
        int count = 0;
        int val = R*C;//the number of cells
        bool flag = false;
        nextMove.push(0);
        while(!Q.empty())
        {
            int i = Q.front().first;
            int j = Q.front().second;
            Q.pop();
            cout<<i<<" "<<j<<" "<<endl;
                MARKED[i][j]=TRUE;
                SEQ.push_back(j+65);
                SEQ.push_back(i+49);//column row format
                ++count;
            if(count == val)
            {
                flag = true;
                break;
            }
        }
        /*if(flag)*/cout<<SEQ<<"\n";
        //else cout<<"-1\n";
    }
    return 0;
}