2010-07-15 4 views
1

이것은 숙제 문제가 아니며, 실제로 재미로하고 있습니다."완벽한 교실"만들기

내가 (의자)에 지정된 "폭"한 교실이 있고, 교실에서 어떤 대화가 없다, 그래서 나는 모든 학생을 배치해야합니다 : 여기

문제입니다.

한 학생이 다른 학생 옆에 있으면 대화가 있습니다. (Diagonals count)

이 예의 경우, 교실 폭은 3입니다. 우리는 Matheus, Gabriel 및 Ravi 학생이 있다고 가정 해 봅시다. 그들은 모두 서로 이야기하고, 이제 다른 학생 A, B, C, D, E, F를

위의 문제에 대한 해결책은 다음과 같습니다

|---------|--------|---------| 
| Gabriel | A  | Matheus | 
|---------|--------|---------| 
| B  | C  | D  | 
|---------|--------|---------| 
| Ravi | E  | F  | 
|---------|--------|---------| 

내 실제 코드는 다음을하지만, 어떤 이유는 브라우저 (무한 루프) 충돌 :

/* 
    Class Classroom 
*/ 
function Classroom($width){ 
    $width=$width||6; 
    var self=this; 
    var alunos=[]; 
    self.addClassmate=function($classmate){ 
     alunos.push($classmate); 
    } 

    self.createClassroom=function(){ 
     var mapa=[]; 
     var width=$width; 
     var height=Math.ceil(alunos.length/width); 
     for(var i=0;i<width;++i){ 
      mapa[i]=[]; 
     } 

     // Shuffle the array 
     alunos.sort(function(a,b){ 
      return 0.5 - Math.random(); 
     }); 


     var i=0; 
     var px=width; 
     var py=height; 


     // Fill the array 
     while(px--){ 
      while(py--){ 
       if(i<alunos.length){ 
        mapa[px][py]=alunos[i]; 
       }else{ 
        mapa[px][py]=new Student('---'); 
       } 

       ++i; 
      } 
      py=height; 
     } 

     function changePosition(px,py){ 
      var dx=Math.floor(Math.random()*width); 
      var dy=Math.floor(Math.random()*height); 
      var me=mapa[px][py]; 
      var other=mapa[dx][dy]; 
      mapa[dx][dy]=me; 
      mapa[px][py]=other; 
      alert('lol'); 
      checkChairs(); 
     } 

     // DO IT 

     function checkChairs(){ 
      for(var px=0;px<width;++px){ 
       for(var py=0;py<height;++py){ 
        var me=mapa[px][py]; 
        var leftCorner = px==0; 
        var rightCorner = px==width-1; 
        var topCorner = py==0; 
        var bottomCorner = py==height-1; 

        if(!leftCorner){ 
         if(mapa[px-1][py].hasRelationWith(me)){ 
          changePosition(px,py); 
          return; 
         } 
         if(!topCorner){ 
          if(mapa[px-1][py-1].hasRelationWith(me)){ 
           changePosition(px,py); 
           return; 
          } 
         } 
         if(!bottomCorner){ 
          if(mapa[px-1][py+1].hasRelationWith(me)){ 
           return; 
          } 
         } 
        } 

        if(!rightCorner){ 
         if(mapa[px+1][py].hasRelationWith(me)){ 
          changePosition(px,py); 
          return; 
         } 
         if(!topCorner){ 
          if(mapa[px+1][py-1].hasRelationWith(me)){ 
           changePosition(px,py); 
           return; 
          } 
         } 
         if(!bottomCorner){ 
          if(mapa[px+1][py+1].hasRelationWith(me)){ 
           changePosition(px,py); 
           return; 
          } 
         } 
        } 

        if(!topCorner){ 
         if(mapa[px][py-1].hasRelationWith(me)){ 
          changePosition(px,py); 
          return; 
         } 
        } 

        if(!bottomCorner){ 
         if(mapa[px][py+1].hasRelationWith(me)){ 
          changePosition(px,py); 
          return; 
         } 
        } 
       } 
      } 
     } 

     checkChairs(); 

     return mapa; 
    } 
} 


/* 
    Class Student 
*/ 
function Student($name){ 
    var self=this; 
    var name=$name; 
    var relation=[]; 

    self.addRelationWith=function($classmate,$mutual){ 
     $mutual=$mutual||true; 
     if(self.hasRelationWith($classmate)) return; 
     relation.push($classmate); 
     if($mutual){ 
      $classmate.addRelationWith(self, false); 
     } 
    } 

    self.hasRelationWith=function($classmate){ 
     var i=relation.length; 
     while(i--){ 
      if(relation[i]==$classmate){ 
       return true; 
      } 
     } 
     return false; 
    } 

    self.getName=function(){ 
     return name; 
    } 

    self.toString=function(){ 
     return '[Student '+self.getName()+']'; 
    } 
} 



var s=new Classroom(3); 

var Matheus=new Student('Matheus'); 
var Gabriel=new Student('Gabriel'); 
var Ravi=new Student('Ravi'); 

Matheus.addRelationWith(Gabriel); 
Matheus.addRelationWith(Ravi); 
Gabriel.addRelationWith(Ravi); 

s.addClassmate(Matheus); 
s.addClassmate(Gabriel); 
s.addClassmate(Ravi); 
s.addClassmate('A'); 
s.addClassmate('B'); 
s.addClassmate('C'); 
s.addClassmate('D'); 
s.addClassmate('E'); 
s.addClassmate('F'); 

alert(s.createClassroom()); 

(이 게시물에서 준 예제는 코드의 끝 부분에 있습니다)

+0

내가 대체 알고리즘을 제안 할 수 있음 : 여기

올바른 코드? 첫째, 한 구석에 한 학생을 배치하십시오 (대각선으로 계산하면 이웃이 세 개인 점). 그런 다음, 학생 옆에 모든 좌석을 기입하십시오. 이제 학생을 채워지지 않은 자리의 다른 "구석"에 앉으십시오. 모서리를 사용할 수없는 경우 최소한의 이웃 수를 가진 지점을 선택하십시오. 모든 학생이 배치 될 때까지 반복하십시오. – Borealid

+0

좋은 생각, 시간이되면 알고리즘을 구현해 보겠습니다. 감사합니다! –

답변

0

LOL! 나는 이것을 믿을 수 없다. 나는 학생들 대신에 문자열을 함수에 전달하고 있었다.

/* 
    Class Classroom 
*/ 
function Classroom($width){ 
    $width=$width||6; 
    var self=this; 
    var alunos=[]; 
    self.addClassmate=function($classmate){ 
     alunos.push($classmate); 
    } 

    self.createClassroom=function(){ 
     var mapa=[]; 
     var width=$width; 
     var height=Math.ceil(alunos.length/width); 
     for(var i=0;i<width;++i){ 
      mapa[i]=[]; 
     } 

     // Shuffle the array 
     alunos.sort(function(a,b){ 
      return 0.5 - Math.random(); 
     }); 



     // Fill the array 
     var i=0; 
     for(var px=0;px<width;++px){ 
      for(var py=0;py<height;++py){ 
       if(i<alunos.length){ 
        mapa[px][py]=alunos[i]; 
        ++i; 
       }else{ 
        mapa[px][py]=new Student('---'); 
       } 
      } 
      py=height; 
     } 
     alert(mapa); 
     function changePosition(px,py){ 
      var dx=Math.floor(Math.random()*width); 
      var dy=Math.floor(Math.random()*height); 
      var me=mapa[px][py]; 
      var other=mapa[dx][dy]; 
      mapa[dx][dy]=me; 
      mapa[px][py]=other; 
      checkChairs(); 
     } 

     // DO IT 

     function checkChairs(){ 
      for(var px=0;px<width;++px){ 
       for(var py=0;py<height;++py){ 
        var me=mapa[px][py]; 
        var leftCorner = px==0; 
        var rightCorner = px==width-1; 
        var topCorner = py==0; 
        var bottomCorner = py==height-1; 

        if(!leftCorner){ 
         if(mapa[px-1][py].hasRelationWith(me)){ 
          changePosition(px,py); 
          return; 
         } 
         if(!topCorner){ 
          if(mapa[px-1][py-1].hasRelationWith(me)){ 
           changePosition(px,py); 
           return; 
          } 
         } 
         if(!bottomCorner){ 
          if(mapa[px-1][py+1].hasRelationWith(me)){ 
           return; 
          } 
         } 
        } 

        if(!rightCorner){ 
         if(mapa[px+1][py].hasRelationWith(me)){ 
          changePosition(px,py); 
          return; 
         } 
         if(!topCorner){ 
          if(mapa[px+1][py-1].hasRelationWith(me)){ 
           changePosition(px,py); 
           return; 
          } 
         } 
         if(!bottomCorner){ 
          if(mapa[px+1][py+1].hasRelationWith(me)){ 
           changePosition(px,py); 
           return; 
          } 
         } 
        } 

        if(!topCorner){ 
         if(mapa[px][py-1].hasRelationWith(me)){ 
          changePosition(px,py); 
          return; 
         } 
        } 

        if(!bottomCorner){ 
         if(mapa[px][py+1].hasRelationWith(me)){ 
          changePosition(px,py); 
          return; 
         } 
        } 
       } 
      } 
     } 

     checkChairs(); 

     return mapa; 
    } 
} 


/* 
    Class Student 
*/ 
function Student($name){ 
    var self=this; 
    var name=$name; 
    var relation=[]; 

    self.addRelationWith=function($classmate,$mutual){ 
     $mutual=$mutual||true; 
     if(self.hasRelationWith($classmate)) return; 
     relation.push($classmate); 
     if($mutual){ 
      $classmate.addRelationWith(self, false); 
     } 
    } 

    self.hasRelationWith=function($classmate){ 
     var i=relation.length; 
     while(i--){ 
      if(relation[i]==$classmate){ 
       return true; 
      } 
     } 
     return false; 
    } 

    self.getName=function(){ 
     return name; 
    } 

    self.toString=function(){ 
     return self.getName(); 
    } 
} 



var s=new Classroom(3); 

var Matheus=new Student('Matheus'); 
var Gabriel=new Student('Gabriel'); 
var Ravi=new Student('Ravi'); 

Matheus.addRelationWith(Gabriel); 
Matheus.addRelationWith(Ravi); 
Gabriel.addRelationWith(Ravi); 
s.addClassmate(Matheus); 
s.addClassmate(Gabriel); 
s.addClassmate(Ravi); 
s.addClassmate(new Student('A')); 
s.addClassmate(new Student('B')); 
s.addClassmate(new Student('C')); 
s.addClassmate(new Student('D')); 
s.addClassmate(new Student('E')); 
s.addClassmate(new Student('F')); 

alert(s.createClassroom()); 

내가 moar 커피 필요가 ...

관련 문제