Example #1
0
void Print::getprint(queue<char> str)
{
	while (!str.empty())
	{
		if (str.front() >= 48 && str.front() <= 57)
		{
			cout << str.front();//输出队首元素
			str.pop();   //删除队首元素
		}
		else if (str.front() != 40 && str.front() != 41 && str.front() != 46)
		{
			cout << endl;
			cout << str.front() << endl;
			str.pop();
		}
		else if (str.front() == 40)
		{
			cout << str.front() << endl;
			str.pop();
		}
		else if (str.front() == 41)
		{
			cout << endl << str.front();
			str.pop();
		}
		else
		{
			cout << str.front();
			str.pop();
		}
	}
	cout << endl;
}
Example #2
0
bool spfa(const int st){ //返回值:TRUE为找到最短路返回,FALSE表示出现负环退出
	for(int i=0;i<V;i++){ //初始化:将除了原点st的距离外的所有点到st的距离均赋上一个极大值
		if(i==st){
			dist[st]=0; //原点距离为0;
			continue;
		}
		dist[i]=INF; //非原点距离无穷大
	}
	buff.push(st); //原点入队
	done[st]=1; //标记原点已经入队
	cnt[st]=1; //修改入队次数为1
	while(!buff.empty()){ //队列非空,需要继续松弛
		int tmp=buff.front(); //取出队首元素
		for(int i=0;i<(int)e[tmp].size();i++){ //枚举该边连接的每一条边
			Edge *t=&e[tmp][i]; //由于vector的寻址速度较慢,故在此进行一次优化
			if(dist[tmp]+(*t).v<dist[(*t).to]){ //更改后距离更短,进行松弛操作
				dist[(*t).to]=dist[tmp]+(*t).v; //更改边权值
				if(!done[(*t).to]){ //没有入队,则将其入队
					buff.push((*t).to); //将节点压入队列
					done[(*t).to]=1; //标记节点已经入队
					cnt[(*t).to]=1; //节点入队次数自增
					if(cnt[(*t).to]>V){ //已经超过V次,出现负环
						while(!buff.empty())buff.pop(); //清空队列,释放内存
						return false; //返回FALSE
					}
				}
			}
		}
		buff.pop();//弹出队首节点
		done[tmp]=0;//将队首节点标记为未入队
	}
	return true; //返回TRUE
} //算法结束
Example #3
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 #4
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;
    }
void bfs(int a, int b)
{
    memset(visit, 0, sizeof(visit));
    memset(dist, -1, sizeof(dist));
    while(!Q.empty())
        Q.pop();
    visit[mm[a]] = 1;
    dist[mm[a]] = 0;
    Q.push(mm[a]);

    int j, u, p;
    p = 0;
    while(!Q.empty())
    {
        u = Q.front();
        Q.pop();
        for(j=1; j<=i; j++)
        {
            if(arr[u][j] == 1 && visit[j] == 0)
            {
                dist[j] = dist[u] + 1;
//                if(dist[j] > b)
//                {
//                    p = 1;
//                    break;
//                }
                visit[j] = 1;
                Q.push(j);
            }
        }
//        if(p == 1)
//            break;
    }
    return ;
}
Example #6
0
void AC()
{
    while(!q.empty())q.pop();
    int i,j;
    for(i=0; i<26; i++)
    {
        if(ch[0][i])
        {
            fail[ch[0][i]]=0;
            q.push(ch[0][i]);
        }
    }
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        for(i=0; i<26; i++)
        {
            if(ch[u][i])
            {
                fail[ch[u][i]]=ch[fail[u]][i];
                q.push(ch[u][i]);
            }
            else
                ch[u][i]=ch[fail[u]][i];
        }
    }
}
Example #7
0
int bfs(char c, int x1, int y1) {
    while (!qu.empty()) {
        qu.pop();
    }
    memset(vis, 0, sizeof(vis));
    vis[x1][y1] = 1;
    mat[x1][y1] = '.';
    qu.push(node(x1, y1, 0));
    while (!qu.empty()) {
        node now = qu.front();
        qu.pop();
        int x1 = now.x;
        int y1 = now.y;
        int t = now.step;
        if (mat[x1][y1] == c) {
            sx = x1;
            sy = y1;
            return t;
        }
        for (int i = 0; i < 4; ++i) {
            int x2 = x1 + dir[i][0];
            int y2 = y1 + dir[i][1];
            if (!judge(x2, y2) || vis[x2][y2]) {
                continue;
            }
            if (mat[x2][y2] == '#' || (mat[x2][y2] >= 'A' && mat[x2][y2] <= 'Z' && mat[x2][y2] != c)) {
                continue;
            }
            vis[x2][y2] = 1;
            qu.push(node(x2, y2, t + 1));
        }
    }
    return -1;
}
Example #8
0
void spfa()
{
    int i,j;
    vis[sx][sy]=1;
    d[sx][sy]=0;
    while(!q.empty())q.pop();
    q.push(make_pair(sx,sy));
    while(!q.empty())
    {
        int ux = q.front().first;
        int uy = q.front().second;
        q.pop();
        vis[ux][uy]=0;
        for(i=0; i<G[ux].size(); i++)
        {
            int vx = G[ux][i];
            if(g[vx][uy])
               relax(vx, uy,ux, uy,  1);
            for(j=0; j<G[uy].size(); j++)
            {
                int vy = G[uy][j];
                if(ux == vy && uy == vx) continue;
                if(g[vx][vy]) relax(vx, vy,ux, uy,  2);
            }
        }
        for(i=0; i<G[uy].size(); i++)
        {
            int vy = G[uy][i];
            if(g[ux][vy]) relax(ux, vy,ux, uy,  1);
        }
    }
}
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 #10
0
inline int bfs(int source,int dest)
{
bool color[r*c+2];
for(int i=0;i<=r*c+1;i++)
color[i]=false;
while(!q.empty())
q.pop();
q.push(make_pair(source,0));
color[source]=true;
pair<int,int> pr;
while(!q.empty())
{
pr=q.front();
q.pop();
if(pr.first==dest)
return pr.second;
for(int i=0;i<graph[pr.first].size();i++)
{
if(color[graph[pr.first][i]])
continue;
color[graph[pr.first][i]]=true;
q.push(make_pair(graph[pr.first][i],1+pr.second));
}
}
}
Example #11
0
void ek()
{
  while(1)
  {
    while(!Q.empty())Q.pop();
    memset(a,0,sizeof(a));
    memset(p,0,sizeof(p));
    a[0]=inf;
    Q.push(0);
    while(!Q.empty())
    {
      int u=Q.front();
      Q.pop();
      for(int v=0;v<=n+1;v++)
      {
        if(!a[v]&&edge[u][v]>0)
        {
          a[v]=min(a[u],edge[u][v]);
          p[v]=u;
          Q.push(v);
        }
      }
      if(a[n+1])break;
    }
    if(!a[n+1])break;
    for(int u=n+1;u!=0;u=p[u])
    {
      edge[p[u]][u]-=a[n+1];
      edge[u][p[u]]+=a[n+1];
    }
    flow+=a[n+1];
  }
}
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 #13
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;
    }
}
void bfs(Point start, int cnt)
{
	while (!q.empty()) q.pop();
	while (!s.empty()) s.pop();
	q.push(start); vis[start.x][start.y] = true;
	s.push(start);
	int sum = 1;
	while (!q.empty())
	{
		Point from = q.front(); q.pop();
		for (int i = 0; i < 4; i++)
		{
			Point to = from + dir[i];
			if (wall(to) || map[to.x][to.y] == '*' || vis[to.x][to.y]) continue;
			q.push(to); vis[to.x][to.y] = true;
			s.push(to); sum++;
		}
	}
	while (!s.empty())
	{
		Point p = s.top(); s.pop();
		num[p.x][p.y] = sum;
		block[p.x][p.y] = cnt;
	}
}
bool spfa()
{
	int dqx,dqy;
	for (int i=1;i<=n;++i) dist[i]=1e7;
	dist[1]=0;
	memset(flag,false,sizeof(flag));
	flag[1]=true;
	while (!q.empty()) q.pop();
	q.push(1);
	while (!q.empty())
	{
		dqx=q.front();
		q.pop();
		for (int i=b[dqx];i!=-1;i=next[i])
		{
			dqy=a[i];
			if (f[i]>0&&dist[dqx]+cost[i]<dist[dqy])
			{
				dist[dqy]=dist[dqx]+cost[i];
				pe[dqy]=i;
				px[dqy]=dqx;
				if (!flag[dqy])
				{
					q.push(dqy);
					flag[dqy]=true;
				}
			}
		}
		flag[dqx]=false;
	}
	if (dist[n]<1e7) return true;
	else return false;
}
Example #16
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 #17
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 #18
0
bool bfs()
{
    while(!q.empty())q.pop();
    vis[sx][sy]=true;
    tmp.x=sx;tmp.y=sy;
    q.push(tmp);
    int x,y,dx,dy;
    while(!q.empty())
    {
        array pa=q.front();q.pop();
        x=pa.x;y=pa.y;
        for(int i=0;i<4;i++)
        {
            dx=x+dir[i][0];
            dy=y+dir[i][1];
            if(dx>=0&&dy>=0&&dx<n&&dy<m&&maze[dx][dy]!='#'&&vis[dx][dy]==false)
            {
                if(dx==0||dy==0||dx==n-1||dy==m-1) return true;
                vis[dx][dy]=true;
                array tk;
                tk.x=dx;tk.y=dy;
                q.push(tk);
            }
        }
    }
    return false;
}
bool spfa()
{
    int i, u, head, tail;
    for (i = 1; i <= cnt; i++) // init
    {
        d[i] = INF;
        vis[i] = false;
    }
    d[0] = 0;
    while (!q.empty())
        q.pop();
    q.push(0);
    while (!q.empty())
    {
        u = q.front();
        q.pop();
        vis[u] = true;
        for (i = 1; i <= cnt; i++)
            if (cap[u][i] && d[i] > d[u] + cost[u][i]) // 松弛
            {
                d[i] = d[u] + cost[u][i];
                pre[i] = u;
                if (!vis[i])
                {
                    vis[i] = true;
                    q.push(i);
                }
            }
        vis[u] = false; // 取消标记反复搜索
    }
    if (d[1] < INF) // end
        return true;
    return false;
}
Example #20
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 init()
{
    while(!que.empty()) que.pop();
    quenod e,t;
    e.s=0;e.step=0;
    que.push(e);
    visit[0] = true;
    while(!que.empty())
    {
        e = que.front();que.pop();
        makemap(e.s);
        for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        {
            for(int k=map[i][j]+1;k<5;k++)
            if(ok(i,j,k))
            {
                t.s = e.s+zip[i*3+j]*(k-map[i][j]);
                if(visit[t.s])continue;
                visit[t.s] = true;
                memset(re[cnt].a,0,sizeof(re[cnt].a));
                for(int ii=0;ii<3;ii++)
                for(int jj=0;jj<3;jj++)
                if(map[ii][jj]) re[cnt].a[map[ii][jj]]++;
                re[cnt].a[map[i][j]]--;
                re[cnt].a[k]++;
                t.step=e.step+1;
                ans[cnt]=t.step;
                cnt++;
                que.push(t);
            }
        }
    }
}
Example #22
0
File: A.cpp Project: Igorjan94/CF
bool bfs(int v)
{
    used[v] = 1;
    q.push(v);
    while (!q.empty())
    {
        v = q.front();
        visited[v] = true;
        q.pop();
        for (int i = 0; i < a[v].size(); i++)
            if (used[a[v][i]])
            {
                if ((used[a[v][i]] - used[v]) % 2 == 0)
                {
                    while (!q.empty())
                        q.pop();
                    return true;
                }
            } else
            {
                used[a[v][i]] = used[v] + 1;
                q.push(a[v][i]);
            }
    }
    return false;
}
Example #23
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 #24
0
void bfs()
{
    node popt,temp;
    str[1]='\0';
    memset(vis,0,sizeof(vis));
    int i,modi;
    while(!q.empty())
    {
        q.pop();
    }
    for(i=1; i<=9; i++)
    {
        if(numcan[i])
        {
            modi=i%n;
            temp.x=modi;
            str[0]=i+'0';
            temp.re=str;
            vis[modi]=1;
            if(modi==0)
            {
                cout<<temp.re<<endl;
                return ;
            }
            q.push(temp);
        }
    }
    while(!q.empty())
    {
        popt=q.front();
        q.pop();
        for(i=0; i<=9; i++)
        {
            if(numcan[i])
            {
                modi=(popt.x*10+i)%n;
                if(modi==0)
                {
                    cout<<popt.re<<i<<endl;
                    return;
                }
                if(!vis[modi])
                {
                    vis[modi]=1;
                    str[0]=i+'0';
                    temp.re=popt.re+str;
                    temp.x=modi;
                    q.push(temp);
                }
            }
        }
    }
    printf("-1\n");
    return;
}
Example #25
0
   // Removes the element on top of the stack.
   void pop() {
	int n = q.size(), temp;
	while(n > 1){
		temp = q.front();
		q.pop();
		q.push(temp);
		t = temp;
		--n;
	}
	q.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 #28
0
int countCastle(int x,int y)
{
    int ans=0;
    if(used[x][y])return -1;
    while(!q.empty())q.pop();
    q.push(x*100+y);
    used[x][y]=true;
    ans++;
    while (!q.empty())
    {
        int to=1;
        int now=q.front();
        q.pop();
        x=now/100;
        y=now%100;
        while (to<=8)
        {
            if ((to&mod[x][y])<=0)
            {
                if(to==1&&(y-1)>=0&&(4&mod[x][y-1])<=0&&!used[x][y-1])
                {
                    //printf("Go left\n");
                    ans++;
                    q.push(x*100+y-1);
                    used[x][y-1]=true;
                }
                if(to==4&&(y+1)<m&&(1&mod[x][y+1])<=0&&!used[x][y+1])
                {
                    //printf("Go right\n");
                    ans++;
                    q.push(x*100+y+1);
                    used[x][y+1]=true;
                }
                if(to==2&&(x-1)>=0&&(8&mod[x-1][y])<=0&&!used[x-1][y])
                {
                    //printf("Go north\n");
                    ans++;
                    q.push((x-1)*100+y);
                    used[x-1][y]=true;
                }
                if(to==8&&(x+1)<n&&(2&mod[x+1][y])<=0&&!used[x+1][y])
                {
                    //printf("Go south\n");
                    ans++;
                    q.push((x+1)*100+y);
                    used[x+1][y]=true;
                }
            }
            to*=2;
        }
    }
    return ans;
}
int bfs(int s,int d) {

    memset(pre,-1,sizeof(pre));
    memset(col,0,sizeof(col));

    int i,j,k,l,ret;

    while(!q.empty()) q.pop();

    q.push(s);
    col[s]=1;

    while(!q.empty()) {

        i=q.front();
        q.pop();

        for(j=1; j<=n; j++) {
            if(col[j]==0&&mat[i][j]>0) {
                pre[j]=i;
                q.push(j);
                col[j]=1;
                if(j==d) break;
            }
        }
    }

    int wh,path,prev;
    path=inf;
    wh=d;

    while(pre[wh]!=-1) {
        prev=pre[wh];
        path=min(path,mat[prev][wh]);
        wh=prev;
    }
    wh=d;

    while(pre[wh]!=-1) {
        prev=pre[wh];
        mat[prev][wh]-=path;
        mat[wh][prev]+=path;
        wh=prev;
    }

    if(path==inf) return 0;
    return path;



    return 0;

}
Example #30
0
void UniformCostSearch()
{
    int stateNum=0;
    while(!Q.empty())Q.pop();

    Q.push(Initstate);

    printf("Expanding state:\n");
    Initstate.output();

    while(!Q.empty())
    {
        stateNum++;
        Puzzle queueFront = Q.front();
        Q.pop();
        outputExpanding(queueFront);
        //getchar();
        if(judgeComplete(queueFront))
        {
            break;
        }
        else
        {
            Puzzle tPuzzle = queueFront;
            if(tranform(LEFT,tPuzzle)&&!judgeSame(tPuzzle,queueFront))
            {
                giveFatherValue(tPuzzle,queueFront);
                Q.push(tPuzzle);
            }
            tPuzzle = queueFront;
            if(tranform(RIGHT,tPuzzle)&&!judgeSame(tPuzzle,queueFront))
            {
                giveFatherValue(tPuzzle,queueFront);
                Q.push(tPuzzle);
            }
            tPuzzle = queueFront;
            if(tranform(UP,tPuzzle)&&!judgeSame(tPuzzle,queueFront))
            {
                giveFatherValue(tPuzzle,queueFront);
                Q.push(tPuzzle);
            }
            tPuzzle = queueFront;
            if(tranform(DOWN,tPuzzle)&&!judgeSame(tPuzzle,queueFront))
            {
                giveFatherValue(tPuzzle,queueFront);
                Q.push(tPuzzle);
            }
        }
    }
    printf("%d\n",stateNum);

}