Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <sys/time.h> // for gettimeofday()
- struct timeval t1,t2;
- int N, M, x1s, y1s, x2s, y2s, Mx, My,cost1,cost2,NODES;
- char Area[100][100];
- char Area2[100][100];
- int robot1[100][2];
- int robot2[100][2];
- int flag1;
- typedef struct Node {
- struct Node *parent;
- int x1;
- int y1;
- double g;
- double h;
- double cost;
- int expandable;
- struct Node *child1;
- struct Node *child2;
- struct Node *child3;
- struct Node *child4;
- struct Node *nextfront;
- struct Node *nextclosed;
- }SearchGraphNode;
- SearchGraphNode *headclosed, *headfront, *new1, *q;
- void displayfront()
- {
- SearchGraphNode *temp = headfront;
- if ( headfront == NULL )
- printf( "QUEUE IS EMPTY\n" );
- else
- {
- printf( "QUEUE IS:\n" );
- while ( temp != NULL )
- {
- printf( "\t%f [%d,%d]",temp->cost,temp->x1,temp->y1);
- temp = temp->nextfront;
- }
- }
- printf("\n");
- }
- void insertfront(SearchGraphNode *new)
- {
- if ( headfront == NULL || new->cost <= headfront->cost )
- {
- new->nextfront = headfront;
- headfront = new;
- }
- else
- {
- q = headfront;
- while ( q->nextfront!= NULL && q->nextfront->cost < new->cost )
- q = q->nextfront;
- new->nextfront = q->nextfront;
- q->nextfront = new;
- }
- }
- void delfront()
- {
- if ( headfront == NULL )
- {
- printf( "\nQUEUE UNDERFLOW\n" );
- }
- else
- {
- new1 = headfront;
- headfront = headfront->nextfront;
- }
- }
- void calc_heuristic(SearchGraphNode *node)
- {
- int r1,r2;
- double f;
- if(node->parent==NULL) node->g=0; else node->g = node->parent->g + 1;
- //ypologismos h
- r1=node->x1;
- r2=node->y1;
- f = ((r1-Mx)*(r1-Mx)+(r2-My)*(r2-My));
- f = sqrt(f);
- node->h=f;
- node->cost=node->g + node->h;
- }
- int searchclosed(int item1,int item2)
- {
- int n=1;
- int boo=0;
- struct Node *node;
- node=headclosed;
- if (node==NULL) return 0;
- else
- {
- while(node->nextclosed!=NULL)
- {
- if((node->x1==item1)&&(node->y1==item2))
- boo=1;
- if(boo) return 1;
- node=node->nextclosed;
- }
- return 0;
- }
- }
- void addBegclosed(int num1, int num2)
- {
- struct Node *temp;
- temp=(struct Node *)malloc(sizeof(struct Node));
- temp->x1 = num1;
- temp->y1 = num2;
- if (headclosed == NULL)
- {
- //List is Empty
- headclosed=temp;
- headclosed->nextclosed=NULL;
- }
- else
- {
- temp->nextclosed=headclosed;
- headclosed=temp;
- }
- }
- void expandup(SearchGraphNode *node)
- {
- int x=node->x1;
- int y=(node->y1) -1;
- if(y>0)
- {
- if(Area[x-1][y-1]=='O')
- {
- struct Node *temp;
- temp=(struct Node *)malloc(sizeof(struct Node));
- NODES++;
- temp->parent=node;
- temp->x1=x;
- temp->y1=y;
- calc_heuristic(temp);
- node->child1=temp;
- insertfront(temp);
- }
- }
- }
- void expanddown(SearchGraphNode *node)
- {
- int x=node->x1;
- int y=(node->y1) + 1;
- if(y<N)
- {
- if((Area[x-1][y-1]=='O'))
- {
- struct Node *temp1;
- temp1=(struct Node *)malloc(sizeof(struct Node));
- temp1->parent=node;
- NODES++;
- temp1->x1=x;
- temp1->y1=y;
- calc_heuristic(temp1);
- node->child2=temp1;
- insertfront(temp1);
- }
- }
- }
- void expandright(SearchGraphNode *node)
- {
- int x=(node->x1) + 1;
- int y=node->y1;
- if(x<=M)
- {
- if(Area[x-1][y-1]=='O')
- {
- struct Node *temp2;
- temp2=(struct Node *)malloc(sizeof(struct Node));
- temp2->parent=node;
- NODES++;
- temp2->x1=x;
- temp2->y1=y;
- node->child3=temp2;
- calc_heuristic(temp2);
- insertfront(temp2);
- }
- }
- }
- void expandleft(SearchGraphNode *node)
- {
- int x=(node->x1)-1;
- int y=node->y1;
- if(x>0)
- {
- if(Area[x-1][y-1]=='O')
- {
- struct Node *temp3;
- temp3=(struct Node *)malloc(sizeof(struct Node));
- temp3->parent=node;
- NODES++;
- temp3->x1=x;
- temp3->y1=y;
- node->child4=temp3;
- calc_heuristic(temp3);
- insertfront(temp3);
- }
- }
- }
- int checkiffinal(int f1x,int f1y,SearchGraphNode *node)
- {
- int x=node->x1;
- int y=node->y1;
- int dif=abs(x-f1x)+abs(y-f1y);
- if(dif<=1) return 1;
- else
- return 0;
- }
- void printtrack1(SearchGraphNode *node)
- {
- int n=0;
- cost1=node->g;
- SearchGraphNode *temp;
- temp=node;
- n=cost1-1;
- while(temp->parent!=NULL)
- {
- robot1[n][0]=temp->x1;
- robot1[n][1]=temp->y1;
- temp=temp->parent;
- n--;
- }
- int i;
- }
- void printtrack2(SearchGraphNode *node)
- {
- int n=0;
- cost2=node->g;
- SearchGraphNode *temp;
- temp=node;
- n=cost2-1;
- while(temp->parent!=NULL)
- {
- robot2[n][0]=temp->x1;
- robot2[n][1]=temp->y1;
- temp=temp->parent;
- n--;
- }
- int i;
- }
- void Astar()
- {
- headclosed=NULL;
- headfront=NULL;
- SearchGraphNode *current;
- current=(struct Node *)malloc(sizeof(struct Node));
- NODES++;
- current->x1=x1s;
- current->y1=y1s;
- current->parent=NULL;
- calc_heuristic(current);
- insertfront(current);
- while(!checkiffinal(Mx,My,current))
- {
- delfront();
- if(!searchclosed(current->x1,current->y1))
- {
- expandup(current);
- expanddown(current);
- expandleft(current);
- expandright(current);
- addBegclosed(current->x1,current->y1);
- }
- if (headfront==NULL) break; else current=headfront;
- }
- if (flag1 == 1) {
- printtrack1(current);
- }
- if (flag1 == 2)
- {
- printtrack2(current);
- }
- }
- main()
- {
- FILE *fp=fopen("inp.txt","r");
- int i, j;
- long long elapsedTime;
- //ReadinRobot A moves from [%d,%d] to [%d,%d]\n",robot1[i-1][0],robot1[i-1][1],robot1[i][0],robot1[i][1]); g the input
- fscanf(fp,"%d",&N);
- fscanf(fp,"%d\n",&M);
- fscanf(fp,"%d",&x1s);
- fscanf(fp,"%d\n",&y1s);
- fscanf(fp,"%d",&x2s);
- fscanf(fp,"%d\n",&y2s);
- fscanf(fp,"%d",&Mx);
- fscanf(fp,"%d\n",&My);
- for(i=0;i<M;i++)
- {
- for(j=0;j<N;j++)
- {
- fscanf(fp,"%c",&Area[i][j]);
- Area2[i][j]=Area[i][j];
- }
- fscanf(fp,"\n");
- }
- t2.tv_sec = t1.tv_sec = t2.tv_usec = t1.tv_usec = 0.0;
- // start timer
- gettimeofday(&t1, NULL);
- Area2[x1s-1][y1s-1]='A';
- Area2[x2s-1][y2s-1]='B';
- //Main Code
- NODES=0;
- flag1=1;
- Astar();
- int temp1,temp2;
- temp1=x1s;
- temp2=y1s;
- x1s=x2s;
- y1s=y2s;
- flag1=2;
- Astar();
- x1s=temp1;
- y1s=temp2;
- int min;
- if (cost1<cost2) min=cost1; else min=cost2;
- int counter=0;
- //Main algorithm of checking points of conflict with the other robot
- if((robot1[0][0]!=x2s)||(robot1[0][1]!=y2s)){
- printf("Robot A moves from [%d,%d] to [%d,%d]\n",x1s,y1s,robot1[0][0],robot1[0][1]);
- Area2[robot1[0][0]-1][robot1[0][1]-1]='A';
- }
- else
- {
- printf("Robot A understands that he will collide with Robot B in [%d,%d], so he changes direction\n",x2s,y2s);
- Area[x2s-1][y2s-1]='X';
- flag1=1;
- Astar();
- Area[x2s-1][y2s-1]='O';
- printf("Robot A moves from [%d,%d] to [%d,%d]\n",x1s,y1s,robot1[0][0],robot1[0][1]);
- Area2[robot1[0][0]-1][robot1[0][1]-1]='A';
- }
- counter++;
- if((robot1[0][0]!=robot2[0][0])||(robot1[0][1]!=robot2[0][1])){
- printf("Robot B moves from [%d,%d] to [%d,%d]\n",x2s,y2s,robot2[0][0],robot2[0][1]);
- Area2[robot2[0][0]-1][robot2[0][1]-1]='B';
- }
- else
- {
- printf("Robot B understands that he will collide with Robot A in [%d,%d], so he changes direction\n",robot2[0][0],robot2[0][1]);
- Area[robot2[0][0]-1][robot2[0][1]-1]='X';
- temp1=x1s;
- temp2=y1s;
- x1s=x2s;
- y1s=y2s;
- flag1=2;
- Astar();
- x1s=temp1;
- y1s=temp2;
- Area[robot2[0][0]-1][robot2[0][1]-1]='O';
- printf("Robot B moves from [%d,%d] to [%d,%d]\n",x2s,y2s,robot2[0][0],robot2[0][1]);
- Area2[robot2[0][0]-1][robot2[0][1]-1]='B';
- }
- counter++;
- for(i=1;i<min;i++)
- {
- if((robot1[i][0]!=robot2[i-1][0])||(robot1[i][1]!=robot2[i-1][0])){
- printf("Robot A moves from [%d,%d] to [%d,%d]\n",robot1[i-1][0],robot1[i-1][1],robot1[i][0],robot1[i][1]);
- Area2[robot1[i][0]-1][robot1[i][1]-1]='A';
- }
- else
- {
- printf("Robot A understands that he will collide with Robot B in [%d,%d], so he changes direction\n",robot1[i][0],robot1[i][0]);
- Area[robot1[i][0]-1][robot1[i][1]-1]='X';
- flag1=1;
- Astar();
- Area[robot1[i][0]-1][robot1[i][0]-1]='O';
- printf("Robot A moves from [%d,%d] to [%d,%d]\n",robot1[i-1][0],robot1[i-1][1],robot1[i][0],robot1[i][1]);
- Area2[robot1[i][0]-1][robot1[i][1]-1]='A';
- }
- counter++;
- if((robot1[i][0]!=robot2[i][0])||(robot1[i][1]!=robot2[i][1])){
- printf("Robot B moves from [%d,%d] to [%d,%d]\n",robot2[i-1][0],robot2[i-1][1],robot2[i][0],robot2[i][1]);
- Area2[robot2[i][0]-1][robot2[i][1]-1]='B';
- }
- else
- {
- printf("Robot B understands that he will collide with Robot A in [%d,%d], so he changes direction\n",robot2[i][0],robot2[i][1]);
- Area[robot2[i][0]-1][robot2[i][1]-1]='X';
- temp1=x1s;
- temp2=y1s;
- x1s=x2s;
- y1s=y2s;
- flag1=2;
- Astar();
- x1s=temp1;
- y1s=temp2;
- Area[robot2[i][0]-1][robot2[i][1]-1]='O';
- printf("Robot B moves from [%d,%d] to [%d,%d]\n",robot2[i-1][0],robot2[i-1][1],robot2[i][0],robot2[i][1]);
- Area2[robot2[i][0]-1][robot2[i][1]-1]='B';
- }
- counter++;
- }
- if(min==cost1){
- printf("Robot A moves from [%d,%d] to [%d,%d] and reaches meeting point\n",robot1[min-1][0],robot1[min-1][1],Mx,My);
- Area2[Mx-1][My-1]='A';
- counter++;
- for(i=min;i<cost2-1;i++){
- printf("Robot B moves from [%d,%d] to [%d,%d] \n",robot2[i-1][0],robot2[i-1][1],robot2[i][0],robot2[i][1]);
- Area2[robot2[i][0]-1][robot2[i][1]-1]='B';
- counter++;
- }
- printf("Robot B moves from [%d,%d] to [%d,%d] and reaches meeting point\n",robot2[cost2-2][0],robot2[cost2-2][1],robot2[cost2-1][0],robot2[cost2-1][1]);
- counter++;
- Area2[robot2[cost2-1][0]-1][robot2[cost2-1][1]-1]='B';
- }
- else
- {
- printf("Robot A moves from [%d,%d] to [%d,%d]\n",robot1[min-1][0],robot1[min-1][1],robot1[min][0],robot1[min][1]);
- Area2[robot1[min][0]-1][robot1[min][1]-1]='A';
- counter++;
- printf("Robot B moves from [%d,%d] to [%d,%d] and reaches meeting point\n",robot2[min-1][0],robot2[min-1][1],Mx,My);
- Area2[robot2[min-1][0]-1][robot2[min-1][1]-1]='B';
- counter++;
- for(i=min+1;i<cost1-1;i++){
- printf("Robot A moves from [%d,%d] to [%d,%d] \n",robot1[i-1][0],robot1[i-1][1],robot1[i][0],robot1[i][0]);
- counter++;
- Area2[robot1[i][0]-1][robot1[i][1]-1]='A';
- }
- printf("Robot A moves from [%d,%d] to [%d,%d] and reaches meeting point\n",robot1[cost1-2][0],robot1[cost1-2][1],robot1[cost1-1][0],robot1[cost1-1][0]);
- Area2[robot1[cost1-1][0]-1][robot1[cost1-1][1]-1]='A';
- counter++;
- }
- // stop timer
- gettimeofday(&t2, NULL);
- elapsedTime = 0.0;
- // compute and print the elapsed time in millisec
- elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000000.0; // sec to us
- elapsedTime += (t2.tv_usec - t1.tv_usec) ; // us
- printf("--------------------------------\n");
- printf("Time of execution = %lld us\n",elapsedTime);
- printf("--------------------------------\n");
- printf("A SOLUTION WAS FOUND IN %d STEPS\n",counter);
- printf("--------------------------------\n");
- printf("RobotA starting point [%d,%d],Robot B starting point [%d,%d] and final meeting point [%d,%d]\n",x1s,y1s,x2s,y2s,Mx,My);
- printf("--------------------------------\n");
- printf(" THE FIRST MAP \n\n");
- for(i=0;i<M;i++)
- {
- for(j=0;j<N;j++)
- {
- printf("%c",Area[i][j]);
- }
- printf("\n");
- }
- printf("--------------------------------\n");
- printf(" THE FINAL MAP \n\n");
- for(i=0;i<M;i++)
- {
- for(j=0;j<N;j++)
- {
- printf("%c",Area2[i][j]);
- }
- printf("\n");
- }
- printf("--------------------------------\n");
- printf("TOTAL NUMBER OF NODES PRODUCED: %d\n",NODES);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement