コード例 #1
0
ファイル: path1.c プロジェクト: cehmanish/chapter7
int main()
{
    int cost[N][N],i,j,w,ch,co;
    int source, target,x,y;
    printf("\tShortest Path Algorithm(DIJKSRTRA's ALGORITHM\n\n");
    for(i=1;i< N;i++)
    for(j=1;j< N;j++)
    cost[i][j] = IN;
    for(x=1;x< N;x++)
    {
        for(y=x+1;y< N;y++)
        {
            printf("Enter the weight of the path between node %d and %d: ",x,y);
            scanf("%d",&w);
            cost [x][y] = cost[y][x] = w;
        }
        printf("\n");
    }
    printf("\nEnter The Source:");
    scanf("%d", &source);
    printf("\nEnter The target");
    scanf("%d", &target);
    co = dijsktra(cost,source,target);
    printf("\nShortest Path: %d",co);
}
// ==================================================================================
// main
// ==================================================================================
int main(int argc, char *argv[]){
  // Handle parameter
  if(argc != 2 && argc != 3){
    fprintf(stderr, USAGE_MESSAGE, argv[0]);
    return EXIT_FAILURE;
  }
  size_t N = atoi(argv[1]);

  // Create N nodes
  node_t *nodes = calloc(N, sizeof(node_t));
  int i, j, distance;
  int edge_count = 0;
  srand48(time(NULL));
  for(i=0; i<(N-1); ++i){
    for(j=i+1; j<N; ++j){
      // Add edges
      distance = lrand48() % RANGE_MAX + 1;
      add_edge(&(nodes[i].edge_head), &nodes[j], distance);
      add_edge(&(nodes[j].edge_head), &nodes[i], distance);
      edge_count++;
    }
  }

  // Print nodes with edges before dijkstra  
  if(argc == 3){
    printf("Nodes before dijkstra\n");
    for(i=0; i<N; ++i){
      print_node(&nodes[i]);
    }
    printf("\n");
  }

  // Measure time
  clock_t begin, end;
  double time_spent;
  printf("Starting dijkstra for problem size (node) %zu and %i edges\n", N, edge_count);
  begin = clock();
  dijsktra(nodes, N, &nodes[0]);
  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("Time spent: %fs\n", time_spent);

  // Free memory
  for(i=0; i<N; ++i){
    // Print nodes after dijkstra
    if(argc == 3){
      printf("\nNodes after dijkstra:\n");
      print_node(&nodes[i]);
    }
    free_edges(nodes[i].edge_head);
  }  
  free(&nodes[0]);

  return EXIT_SUCCESS;
}
コード例 #3
0
int main()
{
    int n;
    scanf("%d",&n);
    int arr[20][20];
    int i,j;
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            scanf("%d",&arr[i][j]);
        }
    }
    dijsktra(0,arr,n);
    return 0;
}
コード例 #4
0
ファイル: MINTURN.c プロジェクト: My-Favorites/ioi-training
int main(void){

    int i,j;
    scanf("%d%d",&M,&N);

    for(i = 0;i < M; i++){
        for(j = 0;j < N; j++){
            scanf("\n%c %c %c %c",&Park[i][j].top,&Park[i][j].right,&Park[i][j].bottom,&Park[i][j].left);
        }
    }

    for(i = 0;i < M;i ++){
        for(j = 0;j < N;j++){
            D[i][j] = infinity;
            visited[i][j] = 0;
        }
    }

    D[0][0] = 0;
    heap_size = 0;

    for(i = 0;i < M ;i ++){
        for(j = 0;j < N;j ++){
            A[heap_size].i = i;
            A[heap_size].j = j;
            pointers[i][j] = heap_size;
            heap_size++;
        }
    }

    build_heap();
    vertex min;
    while(heap_size > 0){
        min = extract_min();
        dijsktra(min.i,min.j);
    }

    printf("%d",D[M-1][N-1]);

    return 0;
}
コード例 #5
0
double test_dijkstra_without_heap(struct Graph *graph, int v1, int v2) {
	struct Dijkstra_Arrays results;
	return dijsktra(graph, &results, v1, v2);
}
コード例 #6
0
int main(int argc, char **argv)
{
  FILE *fp;
  int x_1,y_1,x_2,y_2,x_3,y_3;
  char a;
  int i,j;
  int outside;
  int ButtonPressed = 0;
  int temp_x, temp_y;
  pixel_count = 0;
  for(i=0;i<302;i++)
  {              
    for(j=0;j<302;j++)  
    {          
      cost[i][j] = 9999; 
    } 
  }

  if( (display_ptr = XOpenDisplay(display_name)) == NULL )
    { printf("Could not open display. \n"); exit(-1);}
  printf("Connected to X server  %s\n", XDisplayName(display_name) );
  screen_num = DefaultScreen( display_ptr );
  screen_ptr = DefaultScreenOfDisplay( display_ptr );
  color_map  = XDefaultColormap( display_ptr, screen_num );
  display_width  = DisplayWidth( display_ptr, screen_num );
  display_height = DisplayHeight( display_ptr, screen_num );

  printf("Width %d, Height %d, Screen Number %d\n", 
           display_width, display_height, screen_num);
  border_width = 10;
  win_x = 0; win_y = 0;
  win_width = display_width;
  win_height = display_height; 
  
  win= XCreateSimpleWindow( display_ptr, RootWindow( display_ptr, screen_num),
                            win_x, win_y, win_width, win_height, border_width,
                            BlackPixel(display_ptr, screen_num),
                            WhitePixel(display_ptr, screen_num) );
  size_hints = XAllocSizeHints();
  wm_hints = XAllocWMHints();
  class_hints = XAllocClassHint();
  if( size_hints == NULL || wm_hints == NULL || class_hints == NULL )
    { printf("Error allocating memory for hints. \n"); exit(-1);}

  size_hints -> flags = PPosition | PSize | PMinSize  ;
  size_hints -> min_width = 60;
  size_hints -> min_height = 60;

  XStringListToTextProperty( &win_name_string,1,&win_name);
  XStringListToTextProperty( &icon_name_string,1,&icon_name);
  
  wm_hints -> flags = StateHint | InputHint ;
  wm_hints -> initial_state = NormalState;
  wm_hints -> input = False;

  class_hints -> res_name = "x_use_example";
  class_hints -> res_class = "examples";

  XSetWMProperties( display_ptr, win, &win_name, &icon_name, argv, argc,
                    size_hints, wm_hints, class_hints );

  XSelectInput( display_ptr, win, 
            ExposureMask | StructureNotifyMask | ButtonPressMask );
  
  XMapWindow( display_ptr, win );

  XFlush(display_ptr);
  gc_red = XCreateGC( display_ptr, win, valuemask, &gc_red_values);
  XSetLineAttributes( display_ptr, gc_red, 3, LineSolid, CapRound, JoinRound);
  if( XAllocNamedColor( display_ptr, color_map, "red", 
      &tmp_color1, &tmp_color2 ) == 0 )
    {printf("failed to get color red\n"); exit(-1);} 
  else
    XSetForeground( display_ptr, gc_red, tmp_color1.pixel );
  gc_black = XCreateGC( display_ptr, win, valuemask, &gc_black_values);
  XSetLineAttributes(display_ptr, gc_black, 3, LineSolid,CapRound, JoinRound);
  if( XAllocNamedColor( display_ptr, color_map, "black", 
      &tmp_color1, &tmp_color2 ) == 0 )
    {printf("failed to get color black\n"); exit(-1);} 
  else
    XSetForeground( display_ptr, gc_black, tmp_color1.pixel );
  gc_blue = XCreateGC( display_ptr, win, valuemask, &gc_blue_values);
  XSetLineAttributes(display_ptr, gc_blue, 3, LineSolid,CapRound, JoinRound);
  if( XAllocNamedColor( display_ptr, color_map, "blue", 
      &tmp_color1, &tmp_color2 ) == 0 )
    {printf("failed to get blue yellow\n"); exit(-1);} 
  else
    XSetForeground( display_ptr, gc_blue, tmp_color1.pixel );
  gc_white = XCreateGC( display_ptr, win, valuemask, &gc_white_values);
  XSetLineAttributes(display_ptr, gc_white, 3, LineSolid,CapRound, JoinRound);
  if( XAllocNamedColor( display_ptr, color_map, "white", 
      &tmp_color1, &tmp_color2 ) == 0 )
    {printf("failed to get color white\n"); exit(-1);} 
  else
    XSetForeground( display_ptr, gc_white, tmp_color1.pixel );

  if ( argc != 2 ) 
  {
      printf( "Usage: %s filename.extension \n", argv[0] );
      exit(-1);
  }
  fp = fopen(argv[1], "r");
  if (fp == NULL) 
  {
    fprintf(stderr, "Can't open file %s!\n", argv[1]);
    printf( "Usage: %s filename.extension or Check whether file is present or not\n", argv[0] );
    exit(-1);
  } 
  triangle_count = 0;
  while (fscanf(fp, "%c %c%d%c%d%c %c%d%c%d%c %c%d%c%d%c%c", &a,&a,&x_1,&a,&y_1,&a,&a,&x_2,&a,&y_2,&a,&a,&x_3,&a,&y_3,&a,&a) != EOF) 
  {
    if(get_x_min(x_1,x_2,x_3) >= 0 && get_y_min(y_1,y_2,y_3) >= 0 && get_x_max(x_1,x_2,x_3) <= display_width - 300 && get_y_max(y_1,y_2,y_3) <= display_height - 200)
    {
      t[triangle_count].x1 = x_1;
      t[triangle_count].x2 = x_2;
      t[triangle_count].x3 = x_3;
      t[triangle_count].y1 = y_1;
      t[triangle_count].y2 = y_2;
      t[triangle_count].y3 = y_3;
      triangle_count++;
    }   
  }
  
  x_min = t[0].x1;
  x_max = x_min;
  y_min = t[0].y1;
  y_max = y_min;
  for(i=0;i<triangle_count;i++)
  {
    if(x_min > t[i].x1)
      x_min = t[i].x1;
    if(x_min > t[i].x2)
      x_min = t[i].x2;
    if(x_min > t[i].x2)
      x_min = t[i].x3;
    if(y_min > t[i].y1)
      y_min = t[i].y1;
    if(y_min > t[i].y2)
      y_min = t[i].y2;
    if(y_min > t[i].y3)
      y_min = t[i].y3;
    if(x_max < t[i].x1)
      x_max = t[i].x1;
    if(x_max < t[i].x2)
      x_max = t[i].x2;
    if(x_max < t[i].x3)
      x_max = t[i].x3;
    if(y_max < t[i].y1)
      y_max = t[i].y1;
    if(y_max < t[i].y2)
      y_max = t[i].y2;
    if(y_max < t[i].y3)
      y_max = t[i].y3;        
  }
   
  x_center = win_width / 2;
  y_center = win_height / 2;
  x_gap = x_max - x_min;
  y_gap = y_max - y_min;
  x_start = x_center - x_gap/2 - 50;
  y_start = y_center - y_gap/2 - 50;
  x_end = x_start + x_gap + 100;
  y_end = y_start + y_gap + 100;
  rec_width = x_end - x_start;
  rec_height = y_end - y_start;

  for(i=0;i<triangle_count;i++)
  {
    t[i].x1 = t[i].x1 + x_start + 50 - x_min;
    t[i].x2 = t[i].x2 + x_start + 50 - x_min;
    t[i].x3 = t[i].x3 + x_start + 50 - x_min;
    t[i].y1 = t[i].y1 + y_start + 50 - y_min;
    t[i].y2 = t[i].y2 + y_start + 50 - y_min;
    t[i].y3 = t[i].y3 + y_start + 50 - y_min;
  }
  for(i=0;i<triangle_count;i++)
  {
    t[i].point_1_inside_triangle = 0;
    t[i].point_2_inside_triangle = 0;
    t[i].point_3_inside_triangle = 0;       
  }
  for(i=0;i<triangle_count;i++)
  {
    for(j=0;j<triangle_count;j++)
    {
      if(j!=i)
      {
        if(point_in_triangle(t[i].x1,t[i].y1,t[j].x1,t[j].y1,t[j].x2,t[j].y2,t[j].x3,t[j].y3)==1)
        {
          t[i].point_1_inside_triangle = 1;
        }
        if(point_in_triangle(t[i].x2,t[i].y2,t[j].x1,t[j].y1,t[j].x2,t[j].y2,t[j].x3,t[j].y3)==1)
        {
          t[i].point_2_inside_triangle = 1;
        }
        if(point_in_triangle(t[i].x3,t[i].y3,t[j].x1,t[j].y1,t[j].x2,t[j].y2,t[j].x3,t[j].y3)==1)
        {
          t[i].point_3_inside_triangle = 1;
        }
      }
    }
  }
  
  while(1)
  { 
    XNextEvent( display_ptr, &report );
    switch( report.type )
	  {
	    case Expose:
	      for(i=0;i<triangle_count;i++)
        {
          XDrawLine(display_ptr, win, gc_black, t[i].x1, t[i].y1, t[i].x2, t[i].y2);
          XDrawLine(display_ptr, win, gc_black, t[i].x2, t[i].y2, t[i].x3, t[i].y3);
          XDrawLine(display_ptr, win, gc_black, t[i].x3, t[i].y3, t[i].x1, t[i].y1);
        }
        XDrawRectangle(display_ptr, win, gc_black, x_start, y_start, rec_width, rec_height ); 
        break;
        
      case ConfigureNotify:
          win_width = report.xconfigure.width;
          win_height = report.xconfigure.height;
          break;
        
      case ButtonPress:
      {  
        int x, y;
  	    x = report.xbutton.x;
        y = report.xbutton.y;
        if (report.xbutton.button == Button1 )
	      {
          outside = 0;
          if(x <= x_start || y <= y_start || x >= x_end || y >= y_end)
          {
            outside = 1;
          } 
          for(i=0;i<triangle_count;i++)
          {
            if(point_in_triangle(x,y,t[i].x1,t[i].y1,t[i].x2,t[i].y2,t[i].x3,t[i].y3)==1)
            {
              outside = 1;
              break;
            }
          }
          if(outside == 0)
          {
            ButtonPressed++;
            if(ButtonPressed == 1)
            { 
              temp_x = x;
              temp_y = y;
              pixel_count = 0;
              XFillArc( display_ptr, win, gc_red, x - win_height/150, y - win_height/150, win_height/100, win_height/100, 0, 360*64);
              pixel[pixel_count].name = pixel_count;
              pixel[pixel_count].x = x; 
              pixel[pixel_count].y = y;
              pixel_count++;
            }
            else if(ButtonPressed == 2)
            { 
              if(temp_x == x && temp_y == y)
              {
                ButtonPressed = 1;
                break;
              }
              XFillArc( display_ptr, win, gc_red, x - win_height/150, y - win_height/150, win_height/100, win_height/100, 0, 360*64);
              for(i=0;i<triangle_count;i++)
              {
                if(t[i].point_1_inside_triangle != 1)
                {
                  pixel[pixel_count].name = pixel_count;
                  pixel[pixel_count].x = t[i].x1; 
                  pixel[pixel_count].y = t[i].y1;
                  pixel_count++;
                }
                if(t[i].point_2_inside_triangle != 1)
                {
                  pixel[pixel_count].name = pixel_count;
                  pixel[pixel_count].x = t[i].x2; 
                  pixel[pixel_count].y = t[i].y2;
                  pixel_count++;
                }
                if(t[i].point_3_inside_triangle != 1)
                {
                  pixel[pixel_count].name = pixel_count;
                  pixel[pixel_count].x = t[i].x3; 
                  pixel[pixel_count].y = t[i].y3;
                  pixel_count++;
                }
              }
              pixel[pixel_count].name = pixel_count;
              pixel[pixel_count].x = x; 
              pixel[pixel_count].y = y;
              pixel_count++;
              for(i=0;i<pixel_count-1;i++)
              {         
                for(j=i+1;j<pixel_count;j++)
                {
                  if(can_see(pixel[i].x,pixel[i].y,pixel[j].x,pixel[j].y)==1)
                  {
                    cost[pixel[i].name][pixel[j].name] = cost[pixel[j].name][pixel[i].name] = eculidian_dist(pixel[i].x,pixel[i].y,pixel[j].x,pixel[j].y);
                  }
                }
              }
              dijsktra(pixel[0].name, pixel[pixel_count-1].name);
            }
            else
            {
              clear_cost();
              expose();
              ButtonPressed = 0;          
            }
          }
        }
        else
        {
          XFlush(display_ptr);
          XCloseDisplay(display_ptr);
          exit(0);
        }
      }
      break;
      default:
        break;
    }
  }
  exit(0);
}
コード例 #7
0
ファイル: Enemy.cpp プロジェクト: ankitkushwah107/Load-Runner
void Enemy::update()
{
	

	int playerRow = player->getRow();
	int playerCol = player->getCol();
	

	if (checkRow)
	{
		if (row == playerRow)
		{
			path = dijsktra(AIMap,new Node(AIMap[blockNumber(row, col)], row, col, 0),new Node(AIMap[blockNumber(playerRow,playerCol)],playerRow,playerCol,0));
			count = 1;
			pathLastCol = path[path.size() - 1] % COLUMNS;
			pathLastRow = path[path.size() - 1] / COLUMNS;

			checkRow = false;
			checkCol = true;
		}
	}

	if (checkCol)
	{
		if (col == playerCol)
		{
			path = dijsktra(AIMap, new Node(AIMap[blockNumber(row, col)], row, col, 0), new Node(AIMap[blockNumber(playerRow, playerCol)], playerRow, playerCol, 0));
			count = 1;
			pathLastCol = path[path.size() - 1] % COLUMNS;
			pathLastRow = path[path.size() - 1] / COLUMNS;

			checkCol = false;
			checkRow = true;
		}
	}


	
	if (row == pathLastRow && col == pathLastCol)
	{
		path = dijsktra(AIMap, new Node(AIMap[blockNumber(row, col)], row, col, 0), new Node(AIMap[blockNumber(playerRow, playerCol)], playerRow, playerCol, 0));
		count = 1;
		pathLastCol = path[path.size() - 1] % COLUMNS;
		pathLastRow = path[path.size() - 1] / COLUMNS;
	}

	indexCol = path[count] % COLUMNS;
	indexRow = path[count] / COLUMNS;


	if (row < indexRow)
	{
		spriteCurrRow = FOR_DOWN;
		y += speed;
		if (y+yOffSet > indexRow * TILE_HEIGHT)
		{
			row = indexRow;
			count++;
		}

		if (++currFrame >= frameDelay)
		{
			if (++sourceX > 2)
				sourceX = 1;
			currFrame = 0;
		}
	
	}

	if (row > indexRow)
	{
		spriteCurrRow = FOR_UP;
		y -= speed;
		if ((y+height-yOffSet) < (indexRow+1) * TILE_HEIGHT)
		{
			row = indexRow;
			count++;
		}

		if (++currFrame >= frameDelay)
		{
			if (++sourceX > 2)
				sourceX = 1;
			currFrame = 0;
		}
		
	}


	if (col < indexCol)
	{
		spriteCurrRow = FOR_RIGHT;
		x += speed;
		if (x+xOffSet > indexCol * TILE_WIDTH)
		{
			col = indexCol;
			count++;
		}

		if (++currFrame >= frameDelay)
		{
			if (++sourceX > 2)
				sourceX = 1;
			currFrame = 0;
		}
		
	}


	if (col > indexCol)
	{
		spriteCurrRow = FOR_LEFT;
		x -= speed;
		if ((x+width-xOffSet) < (indexCol+1) * TILE_WIDTH)
		{
			col = indexCol;
			count++;
		}

		if (++currFrame >= frameDelay)
		{
			if (++sourceX > 2)
				sourceX = 1;
			currFrame = 0;
		}
	
	}
}
コード例 #8
0
ファイル: Enemy.cpp プロジェクト: ankitkushwah107/Load-Runner
Enemy::Enemy(ALLEGRO_BITMAP *enemySprite, int map[],Player*player, int curRow, int curCol,PlayerSpriteRow spriteCurrRow)
{

	//copying original map to Player::map
	for (int i = 0; i < ROWS*COLUMNS; i++)
	{
		this->map[i] = map[i];
	}

	//Generating A different map for for dijkstra algorithm
	for (int row = 0; row < ROWS; row++)
	{
		for (int col = 0; col < COLUMNS; col++)
		{
			if (map[blockNumber(row + 1, col)] == FLOOR1 || map[blockNumber(row + 1, col)] == FLOOR2 || map[blockNumber(row, col)] == LADDER|| map[blockNumber(row+1, col)] == LADDER)
				AIMap[blockNumber(row, col)] = 1;
			else
				AIMap[blockNumber(row, col)] = 10;
		}
	}

	//setting player up

	this->spriteCurrRow = spriteCurrRow;

	this->row = curRow;
	this->col = curCol;

	x = col * TILE_WIDTH;
	y = row * TILE_HEIGHT;

	indexRow = 0;
	indexCol = 0;

	width = TILE_WIDTH;
	height = TILE_HEIGHT;

	sourceX = 0;

	this->enemySprite = enemySprite;

	this->frameDelay = 8;
	this->currFrame = 0;
	
	this->player = player;

	checkRow = checkCol = true;

	xOffSet = 19;
	yOffSet = 2;
	
	count = 1;

	

	int playerRow = player->getRow();
	int playerCol = player->getCol();


	path = dijsktra(AIMap, new Node(AIMap[blockNumber(row, col)], row, col, 0), new Node(AIMap[blockNumber(playerRow, playerCol)], playerRow, playerCol, 0));
	
	pathLastCol = path[path.size() - 1] % COLUMNS;
	pathLastRow = path[path.size() - 1] / COLUMNS;

	speed = 4;
}