void SPFA()
{
    visit[25]=1;
    q.push(25);
    D[25]=0;
    while(!q.empty())
    {
        int tmp = q.front();
        q.pop();
        visit[tmp]=0;
        for(int i=0;i<ver[tmp].size();i++)
        {
            node=ver[tmp][i];
            int tmpTo=node.to;
            if(D[tmpTo]>D[tmp]+node.val)
            {
                D[tmpTo]=D[tmp]+node.val;
                if(visit[tmpTo]==0)
                {
                    visit[tmpTo]=1;
                    q.push(tmpTo);
                }
            }
        }
    }
}
 vector<vector<int> > levelOrderBottom(TreeNode *root) {
     vector<vector<int> > answer;
     if (root == NULL)
     {
         return answer;
     }
     queue<traverseNode> tmpQueue;
     traverseQueue.swap(tmpQueue);
     traverseQueue.push(traverseNode(root, 1));
     while(!traverseQueue.empty())
     {
         traverseNode topNode = traverseQueue.front();
         if (topNode.level > answer.size())
         {
             vector<int> emptyVector;
             answer.push_back(emptyVector);
         }
         answer[topNode.level - 1].push_back(topNode.node->val);
         traverseQueue.pop();
         if (topNode.node -> left != NULL)
         {
             traverseQueue.push(traverseNode(topNode.node->left, topNode.level + 1));
         }
         if (topNode.node -> right != NULL)
         {
             traverseQueue.push(traverseNode(topNode.node->right, topNode.level + 1));
         }
     }
     for (int i = 0; i < (answer.size()/2); i++)
     {
         answer[i].swap(answer[answer.size() - i - 1]);
     }
     return answer;
 }
Example #3
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;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	q.push(make_pair(n,0));
	vst[n]=true;
	for (;;)
	{
		pair<int,int> t=q.front();
		q.pop();
		if (t.first==m)
		{
			printf("%d",t.second);
			return 0;
		}
		if (2*t.first<=2*m&&!vst[2*t.first])
		{
			q.push(make_pair(t.first*2,t.second+1));
			vst[t.first*2]=true;
		}
		if (t.first-1>0&&!vst[t.first-1])
		{
			q.push(make_pair(t.first-1,t.second+1));
			vst[t.first-1]=true;
		}
	}
}
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 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;
}
void gogo(){
	for(int i=1;i<=tn;i++){
		q.push(first[i]);
		inq[first[i]]=1;
	}
	while(!q.empty()){
		int u=q.front();
		q.pop();
		inq[u]=0;
		if(!bb[u]){
			c[u]-=U[u];
			bb[u]=true;
		}
		if(head[u]!=0&&c[u]>0){
			for(int i=head[u];i;i=edge[i].next){
				int v=edge[i].v;
				c[v]+=c[u]*edge[i].c;
				if(!inq[v])
				{
					q.push(v);
					inq[v]=1;
				}
			}
		}
		else if(head[u]==0){
			cend[u]=c[u];
		}
	}
}
Example #8
0
void BFS()
{
    for(int i = 1; i <= v; i++)
    {
        color[i] = 0;
    }
    color[s] = 1;
    dist[s] = 0;
    Q.push(s);
    while(!Q.empty())
    {
        u = Q.front();
        Q.pop();
        for(int i = 1; i <= v; i++)
        {
            if(adjMatrix[u][i] == 1)
            {
                if(color[i] == 0)
                {
                    color[i] = 1;
                    dist[i] = dist[u] + 1;
                    Q.push(i);
                }
            }
        }
    }
}
Example #9
0
void bfs(int S){
	int i, u, v;
	for(u=1; u<=nV; u++){
		color[u] = WHITE;
		d[u] = INF;
		pi[u] = NIL;
	}
	color[S] = GRAY;
	d[S] = 0;
	pi[S] = NIL;
	Q.push(S);
	while( !Q.empty() ){
		u = Q.front();
		Q.pop();
		for(i=0; i<edge[u].size(); i++){
			v = edge[u][i];
			if( color[v] == WHITE ){
				color[v] = GRAY;
				d[v] = d[u] + 1;
				pi[v] = u;
				Q.push(v);
			}
		}
		color[u] = BLACK;
	}
}
Example #10
0
void bfs(int temp)
{
    que.push(temp);
    while(!que.empty())
    {
        temp = que.front();
        que.pop();

        for(int i=0;i<path_num;++i)
        {
            if(check[i]==false && (nw[i].from == temp || nw[i].to == temp))
            {
                if(nw[i].from == temp)
                {
                    if(nw[i].to != temp)
                    {
                        city.find(nw[i].to)->second = (city.find(nw[i].to)->second == 0)? city.find(temp)->second+1 :city.find(nw[i].to)->second;
                        que.push(nw[i].to);
                    }
                }
                else if(nw[i].to == temp)
                {
                    city.find(nw[i].from)->second = (city.find(nw[i].from)->second == 0)? city.find(temp)->second+1 :city.find(nw[i].from)->second;
                    que.push(nw[i].from);
                }
                check[i] = true;
            }
        }
    }
}
Example #11
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 #12
0
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    scanf("%d%d", &n, &m);
    memset(d, 0x3f, sizeof(d));
    d[n] = 0;
    q.push(n);
    while (!q.empty()) {
        int i = q.front();
        q.pop();
        if (i * 2 < nm && d[i * 2] > d[i] + 1) {
            d[i * 2] = d[i] + 1;
            if (i * 2 == m)
                break;
            q.push(i * 2);
        }
        if (i - 1 > 0 && d[i - 1] > d[i] + 1) {
            d[i - 1] = d[i] + 1;
            if (i - 1 == m) {
                break;
            }
            q.push(i -  1);
        }
    }
    printf("%d\n", d[m]);
}
Example #13
0
int check(int k){
//	printf("enterinf gor --> %d\n ",k);
	visited[k]=1;
	color[k]=1;
	q.push(k);
	int i;
	int flag=1;
	while(!q.empty()){
		k=q.front();
		q.pop();
//		printf("entering -- >%d\n",k);
		for(i=0;i<v[k].size();i++){
			int u=v[k][i];
			// what to do ???????
			if(visited[u]==0){
			parent[u]=parent[k]+1;
//			printf("for eightbour --> %d for k--> %d value of u --> %d\n",u,k,parent[u]);
				visited[u]=1;
//				color[u]=3-color[k];
				q.push(u);
			}
		}
	}
	return flag;
}
Example #14
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 #15
0
bool bfs()
{
    memset(color, 0, sizeof(color));
    memset(flag, false, sizeof(flag));
    
    color[1] = 1;
    q.push(1);
    flag[1] = true;
    while (!q.empty())
    {
        int pre = q.front();
        for (int i = 0; i < v[pre].size(); i++)
        {
            if (color[v[pre][i]] == EMPTY)
            {
                color[v[pre][i]] = (color[pre] == BLACK) ? WHITE : BLACK;
                flag[v[pre][i]] = true;
                q.push(v[pre][i]);
            }
            else if (color[pre] == color[v[pre][i]])
                return false;
        }
        q.pop();
    }    
    return true;    
}
Example #16
0
int main(){
	//freopen("entrada.in","r",stdin);
	int casos;
	cin >> casos;
	for(int caso = 0; caso < casos; caso++){
		if(caso)
			cout << endl;
		int N, edges;
		cin >>  N >> edges;
		memset(m,false, sizeof m);
		memset(d, -1,sizeof d);

		while(edges--){
			int a, b;
			cin >> a >> b;
			m[a][b] = m[b][a] = true;
		}
		
		d[0] = 0;
		q.push(0);
		
		while(!q.empty()){
			int act = q.front();q.pop();
			for(int i = 0; i < N; ++i)
				if(m[act][i] && d[i] == -1){
					d[i] = d[act] + 1;
					q.push(i);
				}
		}
		for(int i = 1; i < N; ++i)
			cout << d[i] << endl;

	}
	return 0;
}
Example #17
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;
}
 void search(int a, int b, int m, int n, queue<loc> &q, vector<vector<char>>& board){
     assert(q.empty());
     q.push(loc(a,b));
     board[a][b]='p';
     loc cur_loc;
     while(!q.empty()){
         cur_loc=q.front();
         
         q.pop();
         if(cur_loc.first-1>0 && board[cur_loc.first-1][cur_loc.second]=='O'){
             q.push(loc(cur_loc.first-1,cur_loc.second));
             board[cur_loc.first-1][cur_loc.second]='p';
         }//up
         if(cur_loc.first+1<m && board[cur_loc.first+1][cur_loc.second]=='O'){
             q.push(loc(cur_loc.first+1,cur_loc.second));
             board[cur_loc.first+1][cur_loc.second]='p';
         }//down
         if(cur_loc.second-1>0 && board[cur_loc.first][cur_loc.second-1]=='O'){
             q.push(loc(cur_loc.first,cur_loc.second-1));
             board[cur_loc.first][cur_loc.second-1]='p';
         }//left
         if(cur_loc.second+1<n && board[cur_loc.first][cur_loc.second+1]=='O'){
             q.push(loc(cur_loc.first,cur_loc.second+1));
             board[cur_loc.first][cur_loc.second+1]='p';
         }//right
     }
 }
void bfs(int s){

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

	while(!q.empty()){
		int temp=q.front();
		q.pop();
		
		for(int i=0;i<v[temp].size();i++){
			int t1=v[temp][i];
			if(!col[t1]){
				dist[t1]=dist[temp]+1;
				col[t1]=1;
				pre[t1]=temp;
				q.push(t1);
			}
		}

		col[temp]=2;
	}	


}
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;
	}
Example #21
0
int FBellman()
{
  for (int i = 0; i <= t; ++i) d[i] = INF;
  d[s] = 0;
  q.push(s);
  use[s] = true;
  while (!q.empty())
  {
    int v = q.front();
    use[v] = false;
    q.pop();
    for (int i = 0; i < g[v].size(); ++i)
    {
      int to = g[v][i].to;
      if (d[to] > d[v] + g[v][i].cst && g[v][i].c > g[v][i].f)
      {
        d[to] = d[v] + g[v][i].cst;
        if (!use[to]) q.push(to);
        use[to] = true;
        par[to] = v;
        pared[to] = i;
      }
    }
  }
  return d[t];
}
Example #22
0
int Diagram::FindStreams(Element* currente, queue<Stream*>& elemq){
	vector<Element*>::iterator it;
	Stream* tmp(NULL);
	int flag(0); // start_end分界
	if (currente){  //节点不为空
		for (it = elems.begin(); it != elems.end(); it++){
			if ((*it)->isStream())
			{
				tmp = (Stream*)(*it);
				if (tmp->CompareStartElementWith(currente))  // Stream终点图元为传入图元时
				{
					elemq.push(tmp);  //传出流
					flag++;
				}
			}
		}
		for (it = elems.begin(); it != elems.end(); it++){
			if ((*it)->isStream())

			{
				tmp = (Stream*)(*it);
				if (tmp->CompareEndElementWith(currente))  // Stream终点图元为传入图元时
				{
					elemq.push(tmp);  //传出流
				}
			}
		}
	}
	return flag;
}
Example #23
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;
}
Example #24
0
void  connected_colors(int x)
{
	bool visited[100010];
	for(int i=1;i<=n+1;i++)
	visited[i]=0;
	v.push(&c[x]);
	cout<<"Pushed "<<x<<endl;
	while(!v.empty())
	{
		node* t= v.front();
		v.pop();
		visited[t->nam]=1;
		cout<<"Poped : "<<t->nam<<endl;
		for(int i=0;i < t->link.size();i++)
		{
			cout<<"Checking : "<<t->link[i]->nam<<endl;
			if(!visited[t->link[i]->nam] && t->color == t->link[i]->color)
			{
				ans++;
				v.push( t->link[i] );
				cout<<"Pushed : "<< t->link[i]->nam <<endl;
			}
		}
	}
}
Example #25
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];
        }
    }
}
void spfa(int st, int dist[],bool done[], vector < pair <int,int> > adj[] )
{
    dist[st] = 0;
    done[st] = true;
    buff.push(st);
    while (!buff.empty())
    {
        int cur = buff.front();
        buff.pop();
        done[cur] = false;
        for (int nex = 0; nex < (int)adj[cur].size(); nex++)
        {
            pair <int,int> node = adj[cur][nex];
            if (dist[cur]+node.second<dist[node.first])
            {
                dist[node.first] = dist[cur]+node.second;
                if (!done[node.first])
                {
                    done[node.first] = true;
                    buff.push(node.first);
                }
            }
        }
    }
}
void topological(int n)
{
int i,front,id,u,rin=0;
 for(i=1;i<=n;i++)
  {
    if(indegree[i] == 0)
      q.push(i);
  }
  while(!q.empty())
  {
     front = q.front();
     rin++;
     result[rin] = front;
     //cout<<front<<'\t';
     q.pop();
     id = le[front];
     while(id!=-1)
      {
        u = e[id][0];
        indegree[u]--;
        if(indegree[u] == 0)
          q.push(u);
        id = e[id][1];
      }

  }
  if(rin !=n)
   cout<<"Cycle exists";
   else
    {
     for(i=1;i<=n;i++)
       cout<<result[i]<<'\t';
    }
}
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 #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
void SPFA( int s , int  e ){
	
	d[s] = 0 ;
	Q.push( s ) ; 
	visit[s] = 1 ;
	par[s] = s ;
	
	while( !Q.empty() ){
		int a = Q.front() ; Q.pop(); 
		visit[a] = 0 ;
		
		for(int i=0;i<linked[a].size();++i){
			int b = linked[a][i].b ; 
			int v = linked[a][i].v ;
			
			if( d[b] > (d[a] + v) ){ 
				d[b] = d[a] + v ;
				par[b] = a ;
				
				if( !visit[b] ){	
					Q.push( b ) ;
					visit[b] = 1; 
				}
			
			}
			if( d[b] == ( d[a] + v ) ){ // ¦r¨å§Ç³Ì¤p 
				if( par[b] > a ){	
					par[b] = a ; 			
				}
			}
		}
	}
	find_path( e , s );
		
}