fork download
  1. //ALgoritmul lui Lee sau pe coada
  2. //n si m matrice pt A
  3. // pe urmatoarele n linii sunt cate m cifre de 0 sau 1 = imaginea codificata a unui labirint
  4. //pe ultima linie se afla coordonatele unui punct de start . Se cere sa se d cea mai scurta intre p de start si oricare alt punct din labirint
  5. #include <iostream>
  6. #include <fstream>
  7. //punem 1 in casuta de start,la vecini punem k+1 si tot asa
  8. using namespace std;
  9.  
  10. int n=10 , m=10,xi=7,yi=3;
  11. int a[50][50]=
  12. {
  13. {0,0,0,0,0,0,0,0,0,0},
  14. {0,1,0,0,0,0,0,0,0,0},
  15. {0,1,0,1,1,1,1,0,0,0},
  16. {0,1,0,1,0,0,1,0,0,0},
  17. {0,1,0,1,1,1,1,0,0,0},
  18. {0,1,0,0,0,0,0,0,0,0},
  19. {0,1,1,1,1,1,1,0,0,0},
  20. {0,0,0,0,0,0,0,0,0,0},
  21. {0,0,0,0,0,0,1,1,1,1},
  22. {0,0,0,0,0,0,1,0,0,0}
  23.  
  24. };
  25. const int dx[4]={-1,0,1,0};
  26. const int dy[4]={0,1,0,-1};
  27.  
  28. void citire()
  29. {
  30. int i,j;
  31. ifstream in("date.in");
  32. in >> n >>m;
  33. for (i=0;i<n;i++)
  34. for (j=0;j<m;j++)
  35. in >> a[i][j];
  36. in >> xi >> yi;
  37. }
  38.  
  39. int bun(int ii,int jj)
  40. {
  41. return ii>=0 && ii<n && jj>=0 && jj<m;
  42. }
  43.  
  44. void Lee()
  45. {
  46. int cx[100],cy[100],pi,ps,i,j;
  47. int k,ii,jj;
  48. pi=0;
  49. ps=0;
  50. cx[0]=xi;
  51. cy[0]=yi;
  52.  
  53. while(pi<=ps)
  54. {
  55. i=cx[pi];
  56. j=cy[pi];
  57. for(k=0; k<4; k++)
  58. {
  59. ii=i+dx[k];
  60. jj=j+dy[k];
  61. if (bun(ii,jj))
  62. if (a[ii][jj]==0)
  63. {
  64. a[ii][jj]=a[i][j]+1;
  65. ps++;
  66. cx[ps]=ii;
  67. cy[ps]=jj;
  68. }
  69. }
  70. pi++;
  71. }
  72. }
  73.  
  74. void afisare()
  75. {
  76. int i,j;
  77. for (i=0;i<n;i++)
  78. {
  79. for (j=0;j<m;j++)
  80. cout << a[i][j]<<"\t";
  81. cout << endl;
  82. }
  83. }
  84.  
  85. int main()
  86. {
  87. //citire();
  88. a[xi][yi]=1;
  89. Lee();
  90. afisare();
  91. return 0;
  92. }
  93.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
11	12	13	14	15	14	13	12	13	14	
10	1	14	15	14	13	12	11	12	13	
9	1	15	1	1	1	1	10	11	12	
8	1	14	1	0	0	1	9	10	11	
7	1	13	1	1	1	1	8	9	10	
6	1	12	11	10	9	8	7	8	9	
5	1	1	1	1	1	1	6	7	8	
4	3	2	1	2	3	4	5	6	7	
5	4	3	2	3	4	1	1	1	1	
6	5	4	3	4	5	1	0	0	0