Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Tutto e di + - Risolto il problema
Forum - Tutto e di + - Risolto il problema "Camelot"

Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 13:16
Sabato, 24/12/2005
Mhuahua :asd:

Ho risolto il problema difficolt? "Guru" che hanno messo alle olimpiadi d'informatica di qualche anno fa in circa 40-50 minuti.

Il testo lo trovate qui:

http://143.225.229.60/ioi-site/problemi/ITA053%20(Camelot) ...

Ed ecco la mia soluzione in C (non ho curato per niente i commenti e lo "stile", dal momento che valutano solamente il risultato finale).

Codice sorgente - presumibilmente C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct knight{
  5.   int id;
  6.   int *friends;
  7. };
  8.  
  9. int *disposition;
  10. int n;
  11. int firstdispose;
  12. struct knight *knightarr;
  13.  
  14.  
  15. int GetUnhappyKnightId(){
  16. int c;
  17.  for (c = 0; c<n; c++){
  18.   if (!ThisKnightIsFriendOf(knightarr[c],GetKnightOnTheLeftOf(knightarr[c].id))) return knightarr[c].id;
  19.   if (!ThisKnightIsFriendOf(knightarr[c],GetKnightOnTheRightOf(knightarr[c].id))) return knightarr[c].id;
  20.  }
  21. }
  22.  
  23. int GetDispositionIndexFromKnightId(int kid){
  24.   int c;
  25.   for (c=0; c<n; c++) if (disposition[c] == kid) return c;
  26.   return -1;
  27. }
  28.  
  29.  
  30. void SwitchPosition(int kid, int kid2){
  31.   int t;
  32.   int i1, i2;
  33.  
  34.   i1 = GetDispositionIndexFromKnightId(kid);
  35.   i2 = GetDispositionIndexFromKnightId(kid2);
  36.  
  37.   t = disposition[i1];
  38.   disposition[i1] = disposition[i2];
  39.   disposition[i2] = t;
  40. }
  41.  
  42. void TryAssignment(){
  43.  int c;
  44.  int kid;
  45.  
  46.  if (firstdispose) {
  47.   firstdispose = 0;
  48.   for (c=0; c<n; c++) disposition[c] = knightarr[c].id;
  49.  }else{
  50.  
  51.   kid = GetUnhappyKnightId();
  52.   SwitchPosition(kid,GetKnightOnTheLeftOf(kid));
  53.  
  54.  }
  55. }
  56.  
  57.  
  58.  
  59.  
  60. int ThisKnightIsFriendOf(struct knight k, int fid){
  61.     if (k.friends[fid-1]) return 1;
  62.     else return 0;
  63. }
  64.  
  65.  
  66. int AllHappy(){
  67.  int c;
  68.  for (c = 0; c<n; c++){
  69.   if (!ThisKnightIsFriendOf(knightarr[c],GetKnightOnTheLeftOf(knightarr[c].id))) return 0;
  70.   if (!ThisKnightIsFriendOf(knightarr[c],GetKnightOnTheRightOf(knightarr[c].id))) return 0;
  71.  }
  72.  return 1;
  73. }
  74.  
  75.  
  76.  
  77. int GetKnightOnTheLeftOf(int id){
  78.  int c;
  79.  for (c=0; c<n; c++){
  80.   if (disposition[c] == id) {
  81.    if (c != 0) return disposition[c-1];
  82.    else return disposition[n-1];
  83.   }
  84.  }
  85. }
  86.  
  87. int GetKnightOnTheRightOf(int id){
  88.  int c;
  89.  for (c=0; c<n; c++){
  90.   if (disposition[c] == id) {
  91.    if (c != (n-1)) return disposition[c+1];
  92.    else return disposition[0];
  93.   }
  94.  }
  95. }
  96.  
  97. int main(void){
  98.   FILE *in, *out;
  99.   int i,j;
  100.   int t;
  101.  
  102.   if ((in = fopen("input.txt","r")) == NULL) exit(1);
  103.   fscanf(in,"%d",&n);
  104.  
  105.  
  106.   knightarr = (struct knight *)malloc(sizeof(struct knight)*n);
  107.   for (t=0; t<n; t++) knightarr[t].friends = (int *)malloc(sizeof(int)*n);
  108.   disposition = (int *)malloc(sizeof(int)*n);
  109.  
  110.  
  111.   for (i = 0; i<n; i++){
  112.    knightarr[ i].id = i + 1;
  113.    
  114.    for (j = 0; j<n; j++){
  115.      fscanf(in,"%d",&knightarr[ i].friends[j]);
  116.    }  
  117.   }
  118.  
  119.   firstdispose = 1;
  120.  
  121.   do{
  122.     TryAssignment();
  123.   }while(!AllHappy());
  124.  
  125.  
  126.   if ((out = fopen("output.txt","w")) == NULL) exit(1);
  127.  
  128.   for (t = 0; t<n; t++) fprintf(out,"%d ",disposition[t]);
  129.  
  130.   fclose(out);
  131.   fclose(in);
  132.  
  133.  
  134. }


Ultima modifica effettuata da pierotofy il 24/12/2005 alle 13:19


Il mio blog: https://piero.dev
PM Quote