Ejemplo n.º 1
0
void processroute (uint32 target, uint32 parent) {

	int pindex = SYSERR;
	int tindex = SYSERR;

	//check if the parent exists
	if (getindex (parent) == SYSERR) {
		kprintf (" DAO Message-> Parent does not exist in the map %04x \n", parent);
		return;
	}

	pindex = getindex (parent);

	if (getindex (target) == SYSERR) 
		tindex = assignindex (target);
	else 
		tindex = getindex (target);
	

	//check if the mapping already exists or it is a new mapping
	//ignore if the mapping already exists.
	if (rpladjlist[target][parent] || rpladjlist[parent][target]){
		kprintf (" DAO Message-> Mapping already exists (target - parent)%04x - %04x \n", target, parent);
		return;
	}

	rpladjlist[target][parent] = 1;
	rpladjlist[parent][target] = 1;

	//kickoff the Dijkstras shortest path calculation.
	shortestpath ();
	computepaths ();
	printpaths ();
}
Ejemplo n.º 2
0
void top_menu(int option)
{
int x,y;
    switch(option)
    {
                  case 1:
                       read();
                       glutPostRedisplay();
                       break;
                  case 2:
                       drawline();
                       glutPostRedisplay();
                       break;
                  case 3:
                       for(int i=1; i<=n; i++)
{	
glClear(GL_COLOR_BUFFER_BIT);
initial();
for(int r=1;r<=n;r++)
{
x=a[r];
y=b[r];
glPointSize(40);
if(r==src)
glColor3f(0.7f, 0.4f, 0.0f);
else
glColor3f(0.5f, 0.5f, 0.8f);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
glColor3f(0.0,1.0,0.0);
s1=itoa(r,s,10);
drawstring(x-5,y-5,s1);
setFont(GLUT_BITMAP_HELVETICA_18);
glColor3f(0.0,0.0,0.0);
drawstring(130,470,"For source");
glFlush();
}	
drawline();
s1=itoa(i,s,10);
setFont(GLUT_BITMAP_HELVETICA_18);
glColor3f(0.0,0.0,0.0);
drawstring(230,470,s1);
glutPostRedisplay();
shortestpath(i);
delay();
}
break;
                  case 4:
                       exit(0);
    }
}
Ejemplo n.º 3
0
Boolean cur_push(int i, Boolean absolute, Boolean pushdir)
{
  FilePtr file = dirFile(curdir, i);
  String action;
  char path1[MAXPATHLEN+1], path2[MAXPATHLEN+1];

  root_modified = cur_modified = shelf_modified = cur_changed = False;
  if (file)
    if (!((action = push_action(fileType(file))) && *action) &&
	S_ISDIR(fileStats(file)->st_mode)) {
      return
	cur_chdir(shortestpath(path1, pathname(path2, dirName(curdir),
					       fileName(file))));
    } else {
      Boolean ret = filePush(curdir, i, NULL, absolute, False);
      update();
      return ret;
    }
  else
    return False;
}
Ejemplo n.º 4
0
int main()
{
	int node[7][3] = {
				{1,2,35},
				{2,3,45},
				{2,4,30},
				{3,5,25},
				{4,5,45},
				{4,6,130},
				{5,6,100}
	};
	int i,j;

	for(i = 1; i <= 6; i++)
	{
		for(j = 1; j <=6; j++)
		{
			cost[i][j] = MAXLEN;   //设置数组最长距离
		}
	
	}

	creategraph(node,7);    //创建加权图
	printf("加权图的邻接矩阵内容:\n");
	for(i = 1; i <= 6; i++)
	{
		for(j = 1; j <= 6; j++)
		{
			printf(" %4d ",cost[i][j]);  //输出加权数组内容
		}
		printf("\n");
	}
	printf("\n从顶点1到各顶点最近距离计算过程:\n");
	shortestpath(1,6);  //查找最短路径
	return 0;
}
Ejemplo n.º 5
0
void main_page(int *choice)
{
	CAR cp1;
	MAP mp;
	mp.map_xspeed=0;mp.map_yspeed=0;
	cp1.angle=0;cp1.xspeed=0;cp1.yspeed=0;
	double  speed=0;
	double map_speed=0;
	int i=0;                        
	int flag_click_beg=0;    //the three flags are used to judge if the "Start","Finish","Begin" button is pressed
	double distance=0;                //此距离要根据选择路径的函数来实时反馈
	int start_point=-1, finish_point=-1;
	int  center_x;
	int  center_y;
	int flag_judge_finish=0;
	int flag_road_error=0;
	int map_first=0;  //to judge  car  whether it is out of bounds(150,100,400,250)  
	double p_distance[SPOTNUM][SPOTNUM];
	double d[SPOTNUM];
	int path[SPOTNUM];
	POINT point[SPOTNUM];
	STREET all_street[18];
   	LITTLE all_ninebuilding[3];
	LITTLE all_small_door[5];
	LITTLE all_tree[10];
	LITTLE all_mpeople[5];
	LITTLE all_wpeople[5];
	LITTLE all_lake[3];
	LITTLE all_playground[3];
	LITTLE all_bigbuilding[5];
	LITTLE all_house[10];
	LITTLE bicycle[3];
	init_map(all_street,all_tree,all_mpeople,all_wpeople,all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
	init_point_info(point,p_distance);
	while(1)
	{
		flag_click_beg=0;
		flag_judge_finish=0;
		InitGra(350);
		setbkcolor(LIGHTGREEN);
		draw_map(all_street,all_tree,all_mpeople,all_wpeople,all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
		right_plus();
		 while(!(1==flag_click_beg && start_point>=0 && finish_point>=0))
		{
			switch(*choice)
			{
				case 0:
					search(point,&flag_click_beg,&start_point,&finish_point); 
				     break;
				case 1:
					mouse_ctrl(&flag_click_beg,&start_point,&finish_point);
					break;
				default:
					break;
			}
			cleardevice();
			draw_map(all_street,all_tree,all_mpeople,all_wpeople,all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
			mouse_click();
		}
		shortestpath(p_distance,SPOTNUM,d,path,start_point);
		distance=rest_distance(&cp1,point[finish_point]);
		center_x=340-point[start_point].x;
		center_y=170-point[start_point].y;
		second_init_map(&center_x, &center_y,point,all_street,all_tree,all_mpeople,all_wpeople,all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
		cp1.x=point[start_point].x;
		cp1.y=point[start_point].y;
	     while(0==flag_judge_finish && 0==flag_road_error)
	    {
			while(0==flag_judge_finish && 0==flag_road_error)     //有没有完成一次导航的标志,如果完成就会跳出大循环
			{
				for(i=0;i<2;i++)
				{
					setactivepage(i);
				     cleardevice();
				     draw_map(all_street,all_tree,all_mpeople,all_wpeople,all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
				     drawroutine(start_point, finish_point,point,path); 					//画出最短的路径
					 car_control(&cp1,&speed);
				     map_ctrl(&mp,&cp1,&speed,&map_first,&map_speed,point,all_street,all_tree,all_mpeople,all_wpeople,
					              all_ninebuilding,all_playground,all_small_door,all_lake,all_bigbuilding,all_house,bicycle);
					 distance=rest_distance(&cp1,point[finish_point]);
					 draw_car(&cp1);
				     mouse_click();
					 show_distance(&distance);
				/*======================判断小车所处位置=============================*/
				     judge_pre_car(&cp1,point);
				     change_route(&cp1, point, &start_point, &finish_point,path,&flag_road_error);
				     warning(&map_speed, &speed);
				     setvisualpage(i);
				     delay(15);
			     }
			    flag_judge_finish=judge_finish(&cp1,point[finish_point]);
		    }
	          if(1==flag_judge_finish)
		    {
			     distance=0;
				draw_bar1(494,57,636,78,LIGHTGRAY);
			     dis_hz(495,60,"到达目的地",0,20,YELLOW);
			     guidance_end(150,100);
			     cp1.angle=0;		
			     flag_click_beg=0;
			     speed=0;
			     map_speed=0; 			    //再次进入的时候,还是先判断车速
			     start_point=-1;
				finish_point=-1;
				map_first=0;
		     }
			if(1==flag_road_error)
		    {
				shortestpath(p_distance,SPOTNUM,d,path,start_point);
				draw_bar1(494,57,636,150,LIGHTGRAY);
			     dis_hz(495,60,"路线错误,正在",0,18,LIGHTRED);
				dis_hz(495,80,"为您重新查找……",0,18,LIGHTRED);
			     delay(1000);
			     draw_bar1(494,57,636,150,LIGHTGRAY);
			     flag_road_error=0;
		     }
	     }
	}
}
Ejemplo n.º 6
0
Archivo: main.cpp Proyecto: 613677/echo
main()                                                     
{
     ZJGSU pl[11]={{0,"综合大楼","学校的标志性建筑,学校行政事务处理中心"},{1,"图书馆","不仅仅是阅读和学习"},
					{2,"剧院","学生活动中心"},{3,"信息学院","程序猿,不解释"},
					{4,"墨湖","听荷论道"},{5,"鸽子广场","放的不是鸽子,是梦想!"},
					{6,"球门","学校大门"},{7,"经济学院","虚拟经济"},
					{8,"第一田径场","足球场,篮球场,塑胶跑道,用于学生日常锻炼休闲"},{9,"金沙港","生活园区二"},
	 {10,"钱江湾","生活园区一"}};		/*景点包含的信息*/ 
     int i,j,n=11;
     int d[num],p[num],a[num][num];
     for(i=0;i<n;i++)                      
		for(j=0;j<n;j++)
			a[i][j]=maxdist;
		/*给景点之间的路径赋最大值*/

	 a[0][1]=15;a[0][2]=10;a[0][3]=15;a[0][4]=10;a[0][5]=30;
	 a[1][0]=15;a[1][4]=10;                                    
     a[2][0]=10;a[2][4]=10;a[2][6]=15;a[2][7]=10;
	 a[3][0]=15;a[3][5]=30;
	 a[4][0]=10;a[4][1]=10;a[4][2]=10;a[4][6]=10;
	 a[5][0]=30;a[5][3]=30;a[5][6]=45;
	 a[6][2]=15;a[6][4]=10;a[6][5]=45;a[6][9]=50;a[6][10]=5;
	 a[7][2]=20;a[7][8]=20;a[7][9]=30;
	 a[8][7]=20;a[9][6]=50;a[9][7]=30;a[9][10]=55;a[10][6]=5;a[10][9]=55;
	 /*自定义景点间路径长度*/

	 printf("  ########################################\n");
	 printf("  ##                                    ##\n");
	 printf("  ##   浙江工商大学下沙校区校园导游图   ##\n");
	 printf("  ##                                    ##\n");
	 printf("  ##                                    ##\n");
	 printf("  ########################################\n");
	 printf("\n首先请看浙江工商大学的平面图\n");
	 m(); 
	 m2();	     	//主菜单
     do
     {
		 switch(getchar())
		 {
            case'a':  /*景点名称和简介查询*/
			{ 
				while(1)
				{
					m3();	//副菜单
                    scanf("%d",&i);
                    if(i<0||i>10)          /*输入非0~10退出*/ 
					{   
						printf("该景点不存在!\n");
					    m2();    		//主菜单
                        break;
					}   
                    printf("%s-------%s\n",pl[i].name,pl[i].pr);
				}
			}break;
            case'b': 
			{             /*输入景点代码查到其它景点的最短距离*/
				do
				{
					m3();	    //副菜单
                    scanf("%d",&i);
                    if(i<0||i>10)         /*输入非0~10退出*/   
					{    
						printf("该景点不存在!\n");
					    m2();	  	//主菜单
                        break;
					}
                    shortestpath(a,n,i,d,p);
                    printf("\n\n所有的最短路径分别为:\n");
                    input(i,n,d,p); 
				} 
				while(1);
			}break;
			default: exit(1);
		 }
	 }
	 while(getchar());
}
Ejemplo n.º 7
0
int main(){
    int i,j,k,start,end,count,root,des;
    int Vnum;
    unsigned long max=MAX_LENGTH;
    char *str;
    str = (char *)malloc(max*sizeof(char));
    char p[100];
    char tmp[100];
    Node *array=NULL;
    Node *tra=NULL;
loop: while(getline(&str,&max,stdin)!=EOF)
    {
        if(str[0]=='V')
        {
            array=cleargraph(array);
            memset(tmp,'\0',sizeof(tmp));
            i=2;
            count = 0;
            while((*(str+i)>=48&&*(str+i)<=57)||*(str+i)=='-')
            {
                tmp[count]= *(str+i);
                i++;
                count++;
            }
            Vnum = getint(tmp);
            if(Vnum<0)
            {
                printf("Error: invalid input about the number of V.\n");
                continue;
            }
            array = (Node *)malloc(Vnum*sizeof(Node));
            if(array==NULL)
            {
                printf("Error: there is not enough room for storing the graph.\n");
                return false;
            }
            //printf("Vnum=%d\n",Vnum);
            for(i=0;i<Vnum;i++)       //initialize
            {
                array[i].ID=i;
                array[i].next=NULL;
                //printf("%d\n",array[i].ID);
            }
            
            
        }
        else if(str[0]=='E')
        {
            if(array==NULL)
            {
                printf("Error: the graph does not exist.\n");
                array=cleargraph(array);
                continue;
            }
            edgenum=0;
            for(i=0;*(str+i)!='\0';i++)
            {
                if(*(str+i)=='<')
                {
                    memset(tmp,'\0',sizeof(tmp));
                    i++;
                    count = 0;
                    while((*(str+i)>=48&&*(str+i)<=57)||*(str+i)=='-')
                    {
                        tmp[count]= *(str+i);
                        i++;
                        count++;
                    }
                    start = atoi(tmp);
                    if(start>Vnum-1||start<0)
                    {
                        printf("Error: a vertex specified does not exist.\n");
                        array=cleargraph(array);
                        goto loop;
                    }
                    i++;    //omit ','
                    count = 0;
                    memset(tmp,'\0',sizeof(tmp)); //initialize tmp array
                    while((*(str+i)>=48&&*(str+i)<=57)||*(str+i)=='-')
                    {
                        tmp[count]= *(str+i);
                        i++;
                        count++;
                    }
                    end = atoi(tmp);
                    if(end>Vnum-1||end<0)
                    {
                        printf("Error: a vertex specified does not exist.\n");
                        array=cleargraph(array);
                        goto loop;
                    }
                    if(start != end)
                    {
                        //store a edge
                        if(insertedge(array,start,end)==false)
                        {
                            array=cleargraph(array);
                            goto loop;
                        }
                        edgenum++;
                    }
                    
                    long long vv = (long)Vnum * (long)Vnum;
                    if(edgenum>vv)
                    {
                        printf("Error: the number of edges is beyond the limit\n");
                        array=cleargraph(array);
                        goto loop;
                    }
                }
            }
            if(edgenum==0)
            {
                goto loop;
            }
            int n = Vnum;
            int **arr;
            SAT_Manager mgr; 
            int *clause1, *clause2, *clause3, *clause4;
            for(k=1;k<=Vnum;k++)        //vertex cover of size k
            {  
                int multiply = n*k;     //Vnum*k atoms
                mgr = SAT_InitManager();
                SAT_SetNumVariables(mgr, multiply);
                //dynamic assign two-dimention array
                arr = (int **)malloc(sizeof(int *)*n);
                if(!arr)
                {
                    printf("Error: there is not enough room.\n");
                    SAT_ReleaseManager(mgr);
                    return false;
                }
                memset(arr, 0, sizeof(int *)*n);
                while(n--)
                {
                    arr[n] = (int *)malloc(sizeof(int)*k);
                }
                n = Vnum;
                int index = 1;
                //initialize the VarIndex
                for(i=0;i<n;i++)
                {
                    for(j=0;j<k;j++)
                        arr[i][j]= index++;
                }
                //create clause
                
                //test
                // for(i=0;i<n;i++)
                // {
                //     for(j=0;j<k;j++)
                //         printf("arr[%d][%d]=%d\n",i,j,arr[i][j]);
                // }

                clause1 = (int *)malloc(sizeof(int)*n);
                for(i=0;i<k;i++)
                {
                    for(j=0;j<n;j++)
                    {
                        clause1[j]= arr[j][i]<<1;
                    }
                    SAT_AddClause(mgr, clause1, n);
                    
                }
                
                //free(clause);
                clause2 = (int *)malloc(sizeof(int)*2);
                int r;
                for(i=0;i<n;i++)
                {
                    for(j=0;j<k;j++)
                    {
                        for(r=j+1;r<k;r++)
                        {

                            clause2[0] = (arr[i][j]<<1)+1;
                            clause2[1] = (arr[i][r]<<1)+1;
                            SAT_AddClause(mgr, clause2, 2);
                        }                     
                    }
                }
                //free(clause);
                clause3 = (int *)malloc(sizeof(int)*2);
                for(i=0;i<k;i++)
                {
                    for(j=0;j<n;j++)
                    {
                        for(r=j+1;r<n;r++)
                        {
                            clause3[0] = (arr[j][i]<<1)+1;
                            //printf("%d ",clause3[0]);
                            clause3[1] = (arr[r][i]<<1)+1;
                            //printf("%d\n",clause3[1]);
                            SAT_AddClause(mgr,clause3,2);
                        }
                        
                    }
                }

                //free(clause);
                clause4 = (int *)malloc(sizeof(int)*2*k);
                Node * point;
                int m;
                for(i=0;i<n;i++)
                {
                    point = array+i;
                    while(point->next)
                    {
                        point = point->next;
                        j = point->ID;
                        m=0;
                        //printf("j=%d\n",j);
                        for(r=0;r<k;r++)
                        {
                            clause4[m++] = arr[i][r]<<1;
                            //printf("arr[%d][%d]=%d\n",i,r,arr[i][r]<<1);
                            clause4[m++] = arr[j][r]<<1;
                            //printf("arr[%d][%d]=%d\n",j,r,arr[j][r]<<1);
                        }
                        SAT_AddClause(mgr, clause4, 2*k);
                        // printf("i=%d k=%d\n",i,k);
                         // printf("clause4\n");
                         // for(int bbb=0;bbb<2*k;bbb++)printf("%d ",clause4[bbb]);
                         // printf("\n");
                    }
                }

                //free(clause);
                //finish adding clause
                int pp[2], bak, idx[n];
                pipe(pp);
                bak = dup(STDOUT_FILENO);
                dup2(pp[1],STDOUT_FILENO);
                int result = SAT_Solve(mgr);
                dup2(bak, STDOUT_FILENO);
                int pre = -1;
                
                if(result == SATISFIABLE)
                {

                    int idx_num = SAT_NumVariables(mgr);
                    //printf("idx_num=%d\n",idx_num);
                    for(i=1;i<=idx_num;i++)
                    {
                        //printf("i=%d\n",i);
                        int a = SAT_GetVarAsgnment(mgr, i);
                        //printf("%d\n",a);
                        if(a==1)
                        {
                            if(pre!= -1)
                            {
                                printf("%d ",pre);
                                fflush(stdout);
                            }
                            pre = (i-1)/k;
                            //printf("i=%d,pre=%d\n",i,pre);
                        }
                        else if(a == -1){
                            printf("Error: not get the right result\n");
                            fflush(stdout);
                            goto loop;
                        }
                    }
                    printf("%d\n",pre);
                    fflush(stdout);
                    for(i=0;i<n;i++)
                        free(arr[i]);
                    free(arr);
                    free(clause1);
                    free(clause2);
                    free(clause3);
                    free(clause4);
                    SAT_ReleaseManager(mgr);
                    goto loop;
                }
            }
            for(i=0;i<n;i++)
                free(arr[i]);
            free(arr);
            SAT_ReleaseManager(mgr);
            printf("unsat\n");
            fflush(stdout);
            goto loop;
        }     
        else if(str[0]=='s')
        {

            i=0;
            if(array==NULL)
            {
                printf("Error: there is no graph existing, please input a new graph firstly\n");
                goto loop;
            }
            //for(i=0;*(str+i)!='\0';i++)printf("i=%d %c\n",i,*(str+i));
            i+=2;
            if(*(str+i)!='\0')
            {
                //while(*(str+i)<48||*(str+i)>57)i++;
                memset(tmp,'\0',sizeof(tmp));
                count = 0;
                while((*(str+i)>=48&&*(str+i)<=57)||*(str+i)=='-')
                {
                    tmp[count]= *(str+i);
                    i++;
                    count++;
                }
                //printf("%s\n",tmp);
                root = atoi(tmp);
                //printf("root=%d\n",root);
                if(root<0 || root> Vnum-1)
                {
                    printf("Error: a vertex specified does not exist.\n");
                    goto loop;
                }
                //while(*(str+i)<48||*(str+i)>57)i++;
                i++;
                memset(tmp,'\0',sizeof(tmp));
                count = 0;
                while((*(str+i)>=48&&*(str+i)<=57)||*(str+i)=='-')
                {
                    tmp[count]= *(str+i);
                    i++;
                    count++;
                }
                
                des = atoi(tmp);
                //printf("des=%d\n",des);
                if(des<0 || des> Vnum-1)
                {
                    printf("Error: a vertex specified does not exist.\n");
                    goto loop;
                }    
            }
            shortestpath(array,Vnum,root,des);
            printf("\n");
        }
        
    }
    free(array);
    return true;
}
Ejemplo n.º 8
0
int main()
{
	int i,c,v,flag,v1,v2,src,tar,graphcount=0;
	char** tokens;
	char* token; 
	char** edge;
	char buffer[2500];	
	while(fgets(buffer,sizeof buffer,stdin)!=NULL)
	
	{	flag=0;
		
		if(feof(stdin))		
			break;
		switch(buffer[0])
		{
			case 'V':
				if(graphcount>1){for(i=0;i<graphcount;i++)free(cost[i]);free(cost);}
				fprintf(stdout,"%s",buffer);
				fflush(stdout);
				tokens=split(buffer,' ');
				v=atoi(*(tokens+1));
				if(v<0){printf("Error: Number of total Vertices can not be negative\n");graphcount=0;}
				else if(v==0){printf("Error: There will be no graph with 0 vertices\n");graphcount=0;}
				else
				{	graphcount=init(v);}
				free(tokens);				
				continue;
			case 'E':
				fprintf(stdout,"%s",buffer);
				fflush(stdout);
				v1=v2=0;				
				tokens = split(buffer, ' ');
			        if (tokens)
    				{
					token=*(tokens+1);
					for(i=0;i<strlen(token);i++)
					{
						if(token[i]=='-')
						{
						  printf("Error: Negetive vertex is not possible for edge\n");
							flag=1;free(tokens);free(token);
							break;
						}
						if(token[i]=='{'||token[i]=='<')
						{
							token[i]='0';
						}

					}
				        if(flag!=1)
					{
						token[i-2]='\0';
						tokens=split(token,'>');
							
						if(tokens)
						{
        						for (i = 0; *(tokens + i); i++)
        						{
            							token=*(tokens + i);
								if(token[0]==',')token[0]='0';
            							edge=split(token,',');
								if(edge)							
								{
									v1=atoi(*(edge+0));
									v2=atoi(*(edge+1));
								
									if(v1>(v-1)||v2>(v-1))
									{
										printf("Error: The vertex you have given for edge does not exist\n");
			          
										break;
									}
									else
										graph(v1,v2);
							 	}
		
								free(token);
								free(edge);
							}
						}
        					free(tokens);
					}
  				}				
				continue;			
			case 's':
				src=tar=0;
				tokens=split(buffer,' ');
				src=atoi(*(tokens+1));
				tar=atoi(*(tokens+2));
				if(src>v||tar>v||src<0||tar<0) 
					{fprintf(stderr,"Error: Vertex you have given for shortest path does not exist  \n");
					fflush(stderr);}				
				else
					c=shortestpath(cost,src,tar,v);
				free(tokens);
				continue;


		}

         }				
		
	
return 0;
}