U E D R , A S I H C RSS

미로찾기/최경현김상섭

~cpp 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main()
{
  int count = 0; 
  int row = 5;
  int col = 5;
  int nowr = 1,nowc = 1;
  int abc[row+2][col+2];
  int i,j;
  int n;                        
  srand((unsigned)(time(NULL)));
  
  for(i=0;i<row+2;i++)
  {
    for(j=0;j<row+2;j++)
    {
       abc[i][j]=1;
    }
  }

  abc[1][1]=0;
  abc[1][3]=0;
  abc[1][4]=0;
  abc[1][5]=0;
  abc[2][2]=0;
  abc[2][3]=0;
  abc[2][4]=0;
  abc[3][1]=0;
  abc[3][4]=0;
  abc[3][5]=0;
  abc[4][3]=0;
  abc[5][4]=0;
  abc[5][5]=0;       
  
    n = rand()%8 + 1;
  
  while(!(nowr==5 && nowc==5))
  {
    n = rand()%8 + 1;
    

    
    switch(n)
    {
              case 1 :
                   if(abc[nowr-1][nowc-1] == 0)
                   {
                      nowr-=1, nowc-=1;
                      printf("(%d,%d)  ",nowr,nowc);
                      count++;                    
                   }      
                      break;
             case 2 :
                  if(abc[nowr][nowc-1] == 0)
                   {
                      nowc-=1;
                      count++;
                      printf("(%d,%d)  ",nowr,nowc);                    
                   }      
                    break;        
             case 3 :
                  if(abc[nowr+1][nowc-1] == 0)
                   {
                      nowr+=1, nowc-=1;
                      count++;
                      printf("(%d,%d)  ",nowr,nowc);                    
                   }      
                    break;
             case 4 :
                  if(abc[nowr-1][nowc] == 0)
                   {
                      nowr-=1;
                      count++;
                      printf("(%d,%d)  ",nowr,nowc);                    
                   }      
                      break;
             case 5 :if(abc[nowr+1][nowc] == 0)
                   {
                      nowr+=1;
                      count++;
                      printf("(%d,%d)  ",nowr,nowc);                    
                   }      
                      break;                    
             case 6 :
                  if(abc[nowr-1][nowc+1] == 0)
                   {
                      nowr-=1, nowc+=1;
                      count++;
                      printf("(%d,%d)  ",nowr,nowc);                    
                   }      
                      break;                      
             case 7 :
                  if(abc[nowr][nowc+1] == 0)
                   {
                      nowc+=1;
                      count++;
                      printf("(%d,%d)  ",nowr,nowc);                    
                   }      
                      break;
             case 8 :
                  if(abc[nowr+1][nowc+1] == 0)
                   {
                      nowr+=1, nowc+=1;
                      count++;
                      printf("(%d,%d)  ",nowr,nowc);                    
                   }
                   break;
                     
        }
        
     }

     printf("%d",count);
 
  system("PAUSE");	
  return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:31
Processing time 0.0173 sec