Пример #1
0
// PRECONDITION : !vide(p)
// PRECONDITION : k>0 && k<=hauteur(p)
Bool kieme(PileB p, Nat k){
	Nat d = p.iTete-k;
	while(d != 0){
		p = desempiler(p);
		d--;
	}
	return sommet(p);
}
Пример #2
0
int main()
{
  pile p1;
  initialisationPile(p1);
  unsigned int i;
  for (i=0;i<5;i++)
    {
      empile(p1,i);
    }

  pile p2;
  initialisationPile(p2,p1);

  for (i=0;i<5;i++)
    {
      empile(p1,2*i);
    }

  while(!testPileVide(p1))
    {
      std::printf("%d ",sommet(p1));
      depile(p1);
    }
  std::putchar('\n');

  affectationPile(p1,p2);

  while(!testPileVide(p2))
    {
      std::printf("%d ",sommet(p2));
      depile(p2);
    }
  std::putchar('\n');

  while(!testPileVide(p1))
    {
      std::printf("%d ",sommet(p1));
      depile(p1);
    }
  std::putchar('\n');
  testamentPile(p2);
  testamentPile(p1);
  return 0;
}
Пример #3
0
t_graphe	*parcours_profondeur(t_graphe* graphe, char *n_s)
{
  int			*visite;
  int			*names;
  t_pile		*pile;
  int			s;
  int			u;
  int			v;
  int			i;
  int			succ;
  int			size;
  t_ch_int		*tmp;
  t_ch_ch_int		*ttmp;
  t_graphe		*T;
  void			*data;

  u = 0;
  v = 0;
  data = 0;
  succ = 0;
  size = taille(graphe);
  visite = xmalloc(sizeof(*visite)*size);
  names = xmalloc(sizeof(*names)*size);
  pile = xmalloc(sizeof(*pile));
  pile->pile = xmalloc(sizeof(*pile->pile)*size);
  pile->taille_pile = 0;
  T = init(GO);
  empiler(name_to_number(graphe, n_s), pile);
  for (i = 0, ttmp = graphe->graphe; i < size; i++, ttmp = ttmp->next)
    names[i] = ttmp->number;
  inserer(get_index(s, names, size), visite);
  while (pile->taille_pile != 0)
    {
      u = sommet(pile);
      succ = 0;
      for (ttmp = graphe->graphe; ttmp != NULL; ttmp = ttmp->next)
	if (ttmp->number == u)
	  break;
      for (tmp = ttmp->liste; tmp != NULL; tmp = tmp->next)
	if (!visite[get_index(tmp->number, names, size)])
	  succ++;
      if (succ != 0)
	{
	  for (ttmp = graphe->graphe; ttmp != NULL; ttmp = ttmp->next)
	    if (ttmp->number == u)
	      break;
	  for (tmp = ttmp->liste; tmp != NULL; tmp = tmp->next)
	    if (!visite[get_index(tmp->number, names, size)])
	      {
		v = tmp->number;
		data = tmp->data;
		break;
	      }
	  ajouterArc(T, number_to_name(graphe, u), number_to_name(graphe, v), data);
	  empiler(v,pile);
	  inserer(get_index(v, names, size), visite);
	}
      else
	depiler(pile);
    }
  return (T);
}
Пример #4
0
/**
 * @brief Résout un puzzle
 * @param[in,out] p le puzzle à résoudre
 * @param[in] t le damier initial
 * @param[in,out] os flux de sortie
 */
void jouer(Puzzle& p, const Tab2D& t, std::ostream& os) {
	Etat etatInitial;
	Etat etatCourant;
	Tab2D damierFinal;
	Etat etatDerive;

	double tempsDebutRecherche = getTime();

	but(damierFinal, t.nbL, t.nbC);
	initialiser(etatInitial.damier, t.nbL, t.nbC);
	etatInitial.mouvement = FIXE;
	etatInitial.precedent = 0;
	etatInitial.g = 0;

	//Copie du damier inititial dans etatInitial
	for (unsigned int l = 0; l < t.nbL; ++l) {
		for (unsigned int c = 0; c < t.nbC; ++c) {
			etatInitial.damier.tab[l][c] = t.tab[l][c];
		}
	}
	etatInitial.h = manhattan(etatInitial.damier, damierFinal);

	initialiser(etatDerive.damier, t.nbL, t.nbC);

	inserer(p.lEAE, 0, etatInitial); //étatInitial dans LEAE

	bool solutionTrouvee = false;
	bool mvtPossible;
	unsigned int pos;

	while (p.lEAE.nb != 0) {
		pos = minimal(p.lEAE);
		etatCourant = lire(p.lEAE, pos); //on prend le 1er état à explorer
		//insérer étatCourant dans LEE
		inserer(p.lEE, longueur(p.lEE), etatCourant);
		supprimer(p.lEAE, pos); //supprimer étatCourant de LEAE

		if (etatCourant.h == 0) { // le damier de étatCourant est le damier but
			solutionTrouvee = true;
			break; //sortir de la boucle while
		}

		/*pour_tout (mouvement possible associé à étatCourant)
		mouvement possible relatif à damier de étatCourant (etatCourant.damier)
		ordre d'exploration (obligatoire) NORD, EST, SUD, OUEST */
		//initialiser un étatDérivé // d'après le mouvement

		for(int m = OUEST; m >= NORD; --m) {
			mvtPossible = deriver(etatCourant, (Mouvement) m, etatDerive);
			if (mvtPossible && !rechercher(etatDerive, p.lEAE)\
				&& !rechercher(etatDerive, p.lEE)) {
				etatDerive.precedent = longueur(p.lEE) - 1;
				etatDerive.h = manhattan(etatDerive.damier, damierFinal);
				etatDerive.g = etatCourant.g + 1;
				//insérer étatDérivé dans LEAE
				inserer(p.lEAE, longueur(p.lEAE), etatDerive);
			}
		}
	}

	double tempsFinRecherche = getTime();
	cout << "Durée de recherche : " << tempsFinRecherche - tempsDebutRecherche
		<<" seconde(s)."<< endl;

	if (solutionTrouvee) {
		Pile sol;
		Etat etatSol;
		initialiser(sol, 3, 2);
		initialiser(etatSol.damier, t.nbL, t.nbC);

		//Stockage de la solution
		etatSol = lire(p.lEE, longueur(p.lEE)-1);
		empiler(sol, etatSol);
		while (etatSol.precedent != 0) {
			etatSol = lire(p.lEE, etatSol.precedent);
			empiler(sol, etatSol);
		}
		empiler(sol, etatInitial);

		//Affichage de la solution
		os << "Damier : " << t.nbL << " lignes " << t.nbC << " colonnes"
			<< endl;
		os << "Solution en " << sol.sommet << " mouvements" << endl;
		while (!estVide(sol)) {
			afficher(sommet(sol), os);
			os << endl;
			depiler(sol);
		}
		detruire(sol);
		detruire(etatSol.damier);
	}
	else {
		os << "Solution non trouvée" << endl;
	}
	detruire(etatInitial.damier);
	detruire(etatCourant.damier);
	detruire(etatDerive.damier);
	detruire(damierFinal);
}
Пример #5
0
void annulerDernierCoup(TPartie *partie)
{
	int i = 0;
	int idBatCase;
	Coup * dernierCoup;
	TBateau * bateauCible;
	TPosition positionBat;
	Coord positionCourante;

	// Récupération du sommet de la pile et remise à l'état normal

	// Machine ------

	dernierCoup = sommet(partie->pileCoups);
	idBatCase = getIdBateauSurCase(partie->grille, dernierCoup->coordTir);

	//Si il y a un bateau sur la case
	if(idBatCase >= 0)
	{
		bateauCible = getBateauFromId(idBatCase);
		positionBat = getPosBateau(bateauCible);

		positionCourante.noCol = positionBat.x;
		positionCourante.noLin = positionBat.y;

		// Si le bateau est coulé, on repasse tout à touché
		if(estCoule(bateauCible))
		{
			for(i=0;i<getTypeBateau(bateauCible);i++)
			{
				bateauCible->etat[i] = TOUCHE;
                setEtatCase(partie->grille, positionCourante, GRILLE_CASE_TOUCHE);

				if(positionBat.direction == HORIZONTAL)
					positionCourante.noCol += 1;
				else if(positionBat.direction == VERTICAL)
					positionCourante.noLin += 1;

			}
		}

		// On traite la case du coup à proprement parler

		positionCourante.noCol = positionBat.x;
		positionCourante.noLin = positionBat.y;

		if(positionBat.direction == HORIZONTAL)
			i = dernierCoup->coordTir.noCol - positionCourante.noCol;
		else if(positionBat.direction == VERTICAL)
			i = dernierCoup->coordTir.noLin - positionCourante.noLin;

		bateauCible->etat[i] = INTACT;

	}

	setEtatCase(partie->grille, dernierCoup->coordTir, GRILLE_CASE_NORMAL);

	// On dépile le coup
	partie->pileCoups = depiler(partie->pileCoups);


	// Joueur -------

	//On ajoute 1 au score pour le coup tiré
    partie->scorePlayer = partie->scorePlayer + 1;

	dernierCoup = sommet(partie->pileCoups);
	idBatCase = getIdBateauSurCase(partie->grilleMachine, dernierCoup->coordTir);

	if(idBatCase >= 0)
	{
		bateauCible = getBateauFromId(idBatCase);
		positionBat = getPosBateau(bateauCible);

		positionCourante.noCol = positionBat.x;
		positionCourante.noLin = positionBat.y;

		// Si le bateau est coulé, on repasse tout à touché
		if(estCoule(bateauCible))
		{
			for(i=0;i<getTypeBateau(bateauCible);i++)
			{
				bateauCible->etat[i] = TOUCHE;

                setEtatCase(partie->grilleMachine, positionCourante, GRILLE_CASE_TOUCHE);

				if(positionBat.direction == HORIZONTAL)
					positionCourante.noCol += 1;
				else if(positionBat.direction == VERTICAL)
					positionCourante.noLin += 1;
			}
		}

		// On traite la case du coup à proprement parler

		positionCourante.noCol = positionBat.x;
		positionCourante.noLin = positionBat.y;

		if(positionBat.direction == HORIZONTAL)
			i = dernierCoup->coordTir.noCol - positionCourante.noCol;
		else if(positionBat.direction == VERTICAL)
			i = dernierCoup->coordTir.noLin - positionCourante.noLin;

		bateauCible->etat[i] = INTACT;

	}

	setEtatCase(partie->grilleMachine, dernierCoup->coordTir, GRILLE_CASE_NORMAL);

	// On dépile le coup
	partie->pileCoups = depiler(partie->pileCoups);
}