fuliver123

Bài toán tìm đường trên đồ thị - Thoát khỏi mê cung.

Dec 30th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int a[10][20]={},hx[5]={0,1,-1,0,0},hy[5]={0,0,0,-1,1},source[10000],direction[10000],x0,y0,cay[10000],kq[100],cau[10000];
  5.  
  6. int deny(int x, int y)
  7. {
  8.     if (x>8 || x<1 || y>11 || y<1) return 0;
  9.     if (x==1 && y>1 && y<11) return 0;
  10.     if (y==6 && x<6) return 0;
  11.     if ((x==3 || x==5) && (y==2 || y==4 || y==8 || y==10)) return 0;
  12.     if (x==7 && (y!=3 && y!=9)) return 0;
  13.     if (x==8 && (y<3 || y>9)) return 0;
  14.     return 1;
  15. }
  16.  
  17. int nut(int x, int y)
  18. {
  19.     if ((x+y)%2==0) return 0;
  20.     return 1;
  21. }
  22.  
  23. void chep_cay(int k, int x, int y)
  24. {
  25.     cay[2*k]=x; cay[2*k+1]=y;
  26. }
  27.  
  28. void doc_cay(int k)
  29. {
  30.     x0=cay[2*k]; y0=cay[2*k+1];
  31. }
  32.  
  33. int check_direction(int k, int x, int y, int i)
  34. {
  35.     for (k=source[k];k!=0;k=source[k])
  36.         if (cay[2*k]==x && cay[2*k+1]==y && direction[k]==i) return 1;
  37.     return 0;
  38. }
  39.  
  40. int od(int k, int i)
  41. {
  42.     if (k==1 && i==2) return 0;
  43.     if (k==2 && i==1) return 0;
  44.     if (k==3 && i==4) return 0;
  45.     if (k==4 && i==3) return 0;
  46.     return 1;
  47. }
  48.  
  49. void inkq(int k)
  50. {
  51.     int i,dem=1;
  52.     for (i=0;k!=0;)
  53.     {
  54.         if (nut(cay[2*k],cay[2*k+1])==1) kq[i++]=direction[k];
  55.         k=source[k];
  56.     }
  57.     for (i--;i>=0 && printf("\n%d. ",dem++);i--)
  58.         if (kq[i]==1) printf("Di len ");
  59.         else if (kq[i]==2) printf("Di xuong ");
  60.         else if (kq[i]==3) printf("Sang trai ");
  61.         else if (kq[i]==4) printf("Sang phai ");
  62.     printf("\n");
  63. }
  64.  
  65. int c_cau(int k)
  66. {
  67.     if (cau[k]!=0) return cau[k];
  68.     return c_cau(source[k]);
  69. }
  70.  
  71. void timduong()
  72. {
  73.     int x=1,y=1,k,k0,i;
  74.     k0=0; k=1; cau[0]=1;
  75.     cay[0]=cay[1]=1;
  76.     for (;k!=k0;k0++)
  77.     {
  78.         doc_cay(k0);
  79.         for (i=1;i<=4;i++)
  80.         {
  81.             x=x0; y=y0;
  82.             if (od(direction[k0],i)==1 && deny(x+hx[i],y+hy[i])==1 && c_cau(k0)!=a[x+hx[i]][y+hy[i]] && check_direction(k0,x,y,i)==0)
  83.             {
  84.                 source[k]=k0;   direction[k]=i;
  85.                 x+=hx[i]; y+=hy[i];
  86.                 chep_cay(k,x,y);
  87.                 if(a[x][y]==1 || a[x][y]==2) cau[k]=a[x][y];
  88.                 if (x==1 && y==11)
  89.                 {
  90.                     printf("Loi giai: ");
  91.                     inkq(k);
  92.                     return;
  93.                 }
  94.                 k++;
  95.             }
  96.         }
  97.     }
  98. }
  99.  
  100. int main()
  101. {
  102.     a[1][1]=a[2][2]=a[5][1]=a[5][3]=a[4][4]=a[5][5]=a[8][6]=a[4][8]=a[6][8]=a[5][9]=a[2][10]=a[5][11]=a[1][11]=1;
  103. a[3][1]=a[4][2]=a[6][2]=a[3][3]=a[7][3]=a[6][4]=a[5][7]=a[7][9]=a[4][10]=a[3][11]=2;
  104.     timduong();
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment