Example #1
0
int main()
{
	int nbcases;
	cin >> nbcases;
	for (int i=0; i<nbcases; i++)
	{
		int n, c, xi, xj;
		cin >> n >> c >> xi >> xj;
		g_n = n;
		g_count = 0;

		g_grille = vector<int>(g_n*g_n, -1);
		g_marques = vector<int>(g_n*g_n, 0);
		for (int j=0; j<g_n*g_n; j++)
			cin >> g_grille[j];

		//test
		//printe(g_grille,g_n);

		g_visit.push(point(xi,xj));
		g_marques[xi*g_n+xj] = 1;
		while (g_visit.size() != 0)
		{
			visiter(g_visit.front());
			g_visit.pop();
			//printe(g_grille, n);
			//sleep(1);
		}

		//printe(g_grille,g_n);
		printf("%d\n", g_count);
	}
}
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';
    }
}
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;
}
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;
	}	


}
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 #6
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;
			}
		}
	}
}
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 #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;
	}
}
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);
                }
            }
        }
    }
}
Example #11
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 #12
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 );
		
}
Example #13
0
void bfs () {
    
    int value = jiema ();
    st_que.push (value);
    dis_map.insert (pair<int,int> (value,0));

    while (!st_que.empty ()) {
        int temp = st_que.front ();

        yima (temp);

        if (is_equal ()) {
            count = 1;
            return;
        }

        pair<int,int> pos = pos_zero ();
        if (pos.first-1 >= 0) 
            make_map_temp (pos.first , pos.second , 1);
        if (pos.first+1 <= 2)
            make_map_temp (pos.first , pos.second , 2);
        if (pos.second-1 >= 0)
            make_map_temp (pos.first , pos.second , 3);
        if (pos.second+1 <= 2)
            make_map_temp (pos.first , pos.second , 4);
        st_que.pop ();
   }
}
Example #14
0
void convertnfa2dfa(GN *head){
	int cnt = 0;
	for(char i = 'a'; i <= 'z'; i++){
		if(exist[i]){
			syb[cnt++] = i;
		}
	}
	int scnt = 0;
	State init;
	init.pushback(1);
	State s0 = eclosure(init);
	q.push(s0);
	judge.insert(pair<State,int>(s0,scnt++));
	while(!q.empty()){
		State x = q.front(); q.pop();
		for (int i = 0; i < cnt; i++){
			State y = eclosure(move(x,syb[i]));
			if(judge.find(y) == judge.end()){
				judge.insert(pair<State,int>(y,scnt++));
				q.push(y);
			}
			x.show();
			cout<<" s"<<judge[x]<<" ";
			cout<<syb[i]<<" ";
			y.show();
			cout<<" s"<<judge[y]<<endl;
		}
		cout<<1<<endl;
	}
}
double calculate_post(queue<string>& post) {
    stack<double> result_stack;
    while(!post.empty()) {
        string temp = post.front();
        post.pop();
        if(is_operator(temp[0])) { //是操作符
            if(result_stack.size()<2) {
                cout<<"表达式错误"<<endl;
                exit(-1);
            }
            //从栈中取出两个元素,计算并将结果压入栈中
            double operand2 = result_stack.top();
            result_stack.pop();
            double operand1 = result_stack.top();
            result_stack.pop();
            
            double m = calculate_two(operand1, operand2, temp);
            result_stack.push(m);
        } else { //操作数
            double temp_operand = atof(temp.c_str());
            result_stack.push(temp_operand);
        }
    }
    return result_stack.top();
}
Example #16
0
int bfs(int tr,int tc)
{
    while(!q.empty())
    {
        int k;
        node obj=q.front();
        q.pop();
        mark[tr][tc]=1;
        
        if((obj.r==n-1)&&(obj.c==n-1))
        {
            return 2;
        }
        for(k=0;k<=3;k++)
        {
            nr=obj.r+turnr[k];
            nc=obj.c+turnc[k];
            if((nr<n)&&(nc<n)&&(nr>=0)&&(nc>=0)&&(mark[nr][nc]==0)&&(grid[nr][nc]!='#'))
            {
                    node newobj(nr,nc,grid[nr][nc]);
                    q.push(newobj);
                    mark[nr][nc]=1;
                   
            }
        }
    }
    return 3;
}
Example #17
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 #18
0
void BFSfire(int r,int c)
{

    while(fq.empty()==false){
        ii v=fq.front();
        fq.pop();
        fvisited[v.first][v.second]=1;

        int row[4]={1,-1,0,0};
        int col[4]={0,0,1,-1};

        for(int i=0;i<4;i++){
            int n_r=v.first+row[i];
            int n_c=v.second+col[i];
            if(n_r>=1 && n_r<=r && n_c>=1 && n_c<=c){
                if(fvisited[n_r][n_c]==0 && graph[n_r][n_c]==0){
                    fq.push(make_pair(n_r,n_c));
                    fvisited[n_r][n_c]=1;
                    firelvl[n_r][n_c]=firelvl[v.first][v.second]+1;

                }
            }

        }

    }

}
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);
                }
            }
        }
    }
}
Example #20
0
void BFSjoe(int r,int c)
{

    while(jq.empty()==false){
        ii u=jq.front();
        jq.pop();

        int row[4]={1,-1,0,0};
        int col[4]={0,0,1,-1};

        for(int i=0;i<4;i++){
            int n_r=u.first+row[i];
            int n_c=u.second+col[i];
            if(n_r>=1 && n_r<=r && n_c>=1 && n_c<=c){
                if(jvisited[n_r][n_c]==0 && graph[n_r][n_c]==0){
                    jq.push(make_pair(n_r,n_c));
                    jvisited[n_r][n_c]=1;
                    joelvl[n_r][n_c]=joelvl[u.first][u.second]+1;

                }
            }

        }

    }

}
Example #21
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 #22
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;
}
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 #24
0
void Solve()
{
      int i;
      int j;
      value=0;
      memset(vis,false,sizeof(vis));
      memset(use,false,sizeof(use));
      while(!q.empty())
      {
      	ts=q.front();
      	q.pop();
      	tl=ts.length();
            for(i=0;i<4;i++)
            {
                  for(j=0;j<4;j++)
                  {
                        if(Boggle[i][j]==ts[0])
                        {
                              s[0]=0;
                              DFS(i,j,1);
                        }
                  }
            }
      }
      return;
}
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;
		}
	}
}
Example #26
0
File: main.cpp Project: wzcjj/cjj1
 void init(int m = maxn - 1) {
     n = m;
     while(!q.empty())q.pop();
     for(int i = 1;i <= n; i ++) {
         flow[i].clear();
     }
 }
Example #27
0
void DBMS::openTable(queue<Token> params){
	FileIO fileManager;
	string table_name;
	relation intoTable;

	//first token in string is name of table
	table_name=params.front().value;
	params.pop();
	try{
		intoTable = fileManager.readFromFile(table_name, 0);
	}catch(...){
		cout<<"Error: Table "<<table_name<<" could not be found"<<endl;
		return;
	}
	
	//Table already in memory, delete it from queryResults and 6then push back the new one
	vector<relation>::iterator it;
	for (it= queryResults.begin(); it < queryResults.end(); it++){
		if (it->name == table_name){
			queryResults.erase(it);
			break;
		}
	}
		
	queryResults.push_back(intoTable);
}
Example #28
0
void initializeStartingCobasis(){

	cout<<"*Max depth of "<<INITDEPTH<<" to initialize starting cobasis list"<<endl;
        if(OUTSTREAM != NULL)
	    OUTSTREAM <<"*Max depth of "<<INITDEPTH<<" to initialize starting cobasis list"<<endl;
	
	//Copy contents of ine file to temporary file	
	copyFile(INPUTFILE, "init_temp.ine");
	ofstream init_temp_file ("init_temp.ine", ios::app);
	init_temp_file<<"maxdepth "<<INITDEPTH<<endl;
	init_temp_file<<"printcobasis 1"<<endl;
	init_temp_file.close();
	boost::thread consumer_init_thread(consumeOutput);
	boost::thread producer_init_thread(findInitCobasis);

	//Wait for producer thread to find all cobasis at depth <= 1
	producer_init_thread.join();
	//Notify init consumer thread that init producer is finished
	notifyProducerFinished();
	//wait for init consumer thread to finish building starting cobasis list
	consumer_init_thread.join();
	//finished initialization starting cobasis array
	initializing = false;
	producers_finished = false;
	cout<<"*Finished initializing cobasis list with "<<cobasis_list.size()<<" starting cobases"<<endl;
        cobasislistsize = cobasis_list.size();
	
}
Example #29
0
int main(){
	int n;
	loc=0;

	//freopen( "C:\\Users\\fsc\\Desktop\\input_test.txt","r", stdin );


	while(scanf("%d",&n)!=EOF){
			int tmp;

		for(int p=1;p<=n;p++){
		
			scanf("%d",&tmp);
			strP[p]=tmp;
		}
		for(int pi=1;pi<=n;pi++){
			scanf("%d",&tmp);
			strI[pi]=tmp;
		}
		
		Node * root = build(1,n,1,n);
		
		while(!Q.empty())Q.pop();

		Q.push(root);
		push_queue(root,n);

	
	
	}


	return 0;
}
Example #30
0
void computeRUN(priority_queue<Event, vector<Event>, CompareEvent> *eventList, vector<Data> &dataTable,
                vector<Process> &processTable, queue<Process> &interactiveQueue, queue<Process> &real_timeQueue, int computingLine, Device *device, int pid, int &clock)
{
    int CPU_Request_Time = dataTable[computingLine].parameter;
    
    int freeCore = checkCPU(device);
    
    // if a core is free
    if (freeCore != -1)
    {
        // mark core busy until clock + CPU_Request_Time
        device[freeCore].isIdle = false;
        device[freeCore].currentProcess = pid;
        device[freeCore].completionTime = clock + CPU_Request_Time;
        
        createEvent(eventList, "RELEASE CORE", pid, clock + CPU_Request_Time);
        
        processTable[pid].currentLine = computingLine;
    }
    else
    {
        if (processTable[pid].type == "INTERACTIVE")
            interactiveQueue.push(processTable[pid]);
        else
            real_timeQueue.push(processTable[pid]);
    }
}