Advertisement
Guest User

Incercarea unui cal de a strabate o tabla de sah

a guest
Oct 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. using namespace std;
  5. ifstream f("sah.in");
  6. ofstream g("sah.out");
  7. int a[10][10],i1,j1,l=0,ii,ji,ok=0,x[100],p=0;
  8. int E[4]={-1,-2,1,-2};
  9. int V[4]={-1,2,1,2};
  10. int N[4]={-2,-1,-2,1};
  11. int S[4]={2,-1,2,1};
  12. int in(int i,int j)
  13. {
  14. x[l]=i*10+j;
  15. l++;
  16. }
  17. int sol(int i,int j)
  18. {
  19. if(i==i1&&j==j1) {ok=1;
  20. for(i=1;i<=8;i++)
  21. for(j=1;j<=8;j++)
  22. if(a[i][j]==1) a[i][j]=7;
  23. return 1;}
  24. else return 0;
  25. }
  26. int sol1(int i,int j)
  27. {
  28. if(i==i1&&j==j1) {a[i][j]=1; return 1;}
  29. else return 0;
  30. }
  31. int valid(int &i,int &j)
  32. {
  33. p++;
  34. if(i+N[0]==i1&&j+N[1]==j1) {i=i+N[0]; j=j+N[1]; return 1;}
  35. if(i+N[2]==i1&&j+N[3]==j1) {i=i+N[2]; j=j+N[3]; return 1;}
  36. if(i+S[0]==i1&&j+S[1]==j1) {i=i+S[0]; j=j+S[1]; return 1;}
  37. if(i+S[2]==i1&&j+S[3]==j1) {i=i+S[2]; j=j+S[3]; return 1;}
  38. if(i+E[0]==i1&&j+E[1]==j1) {i=i+E[0]; j=j+E[1]; return 1;}
  39. if(i+E[2]==i1&&j+E[3]==j1) {i=i+E[2]; j=j+E[3]; return 1;}
  40. if(i+V[0]==i1&&j+V[1]==j1) {i=i+V[0]; j=j+V[1]; return 1;}
  41. if(i+V[2]==i1&&j+V[3]==j1) {i=i+V[2]; j=j+V[3]; return 1;}
  42. if(i1<i||j1<j)
  43. {
  44. if(a[i+N[0]][j+N[1]]==0&&i+N[0]>=1&&j+N[1]>=1) {i=i+N[0]; j=j+N[1]; return 1;}
  45. else if(a[i+N[2]][j+N[3]]==0&&i+N[2]>=1&&j+N[3]<=8) {i=i+N[2]; j=j+N[3]; return 1;}
  46. else if(a[i+E[0]][j+E[1]]==0&&i+E[0]>=1&&j+E[1]>=1) {i=i+E[0]; j=j+E[1]; return 1;}
  47. else if(a[i+E[2]][j+E[3]]==0&&i+E[2]<=8&&j+E[3]>=1) {i=i+E[2]; j=j+E[3]; return 1;}
  48. else if(a[i+V[0]][j+V[1]]==0&&i+V[0]>=1&&j+V[1]<=8) {i=i+V[0]; j=j+V[1]; return 1;}
  49. else if(a[i+V[2]][j+V[3]]==0&&i+V[2]<=8&&j+V[3]<=8) {i=i+V[2]; j=j+V[3]; return 1;}
  50. else if(a[i+S[0]][j+S[1]]==0&&i+S[0]<=8&&j+S[1]>=1) {i=i+S[0]; j=j+S[1]; return 1;}
  51. else if(a[i+S[2]][j+S[3]]==0&&i+S[2]<=8&&j+S[3]<=8) {i=i+S[2]; j=j+S[3]; return 1;}
  52. }
  53. else{
  54. if(a[i+S[2]][j+S[3]]==0&&i+S[2]<=8&&j+S[3]<=8) {i=i+S[2]; j=j+S[3]; return 1;}
  55. else if(a[i+S[0]][j+S[1]]==0&&i+S[0]<=8&&j+S[1]>=1) {i=i+S[0]; j=j+S[1]; return 1;}
  56. else if(a[i+V[2]][j+V[3]]==0&&i+V[2]<=8&&j+V[3]<=8) {i=i+V[2]; j=j+V[3]; return 1;}
  57. else if(a[i+V[0]][j+V[1]]==0&&i+V[0]>=1&&j+V[1]<=8) {i=i+V[0]; j=j+V[1]; return 1;}
  58. else if(a[i+E[2]][j+E[3]]==0&&i+E[2]<=8&&j+E[3]>=1) {i=i+E[2]; j=j+E[3]; return 1;}
  59. else if(a[i+E[0]][j+E[1]]==0&&i+E[0]>=1&&j+E[1]>=1) {i=i+E[0]; j=j+E[1]; return 1;}
  60. else if(a[i+N[2]][j+N[3]]==0&&i+N[2]>=1&&j+N[3]<=8) {i=i+N[2]; j=j+N[3]; return 1;}
  61. else if(a[i+N[0]][j+N[1]]==0&&i+N[0]>=1&&j+N[1]>=1) {i=i+N[0]; j=j+N[1]; return 1;}
  62. }
  63. return 0;
  64. }
  65. int bac(int i,int j)
  66. {
  67. while(sol(i,j)==0&&ok==0&&p<64)
  68. {
  69. if(valid(i,j))
  70. {
  71. in(i,j);
  72. a[i][j]++;
  73.  
  74. }
  75. else bac(i,j);
  76. }
  77. i1=ii;
  78. j1=ji;
  79. while(sol1(i,j)==0&&p<64)
  80. {
  81. if(valid(i,j))
  82. {
  83. in(i,j);
  84. a[i][j]++;
  85. }
  86. else bac(i,j);
  87. }
  88.  
  89. }
  90. int main()
  91. {
  92. int n,i,j;
  93. char c;
  94. f>>n>>c;
  95. if(c=='a') {a[n][1]=1; i=n, j=1;}
  96. else if(c=='b') {a[n][2]=1; i=n, j=2;}
  97. else if(c=='c') {a[n][3]=1; i=n,j=3;}
  98. else if(c=='d') {a[n][4]=1; i=n, j=4;}
  99. else if(c=='e') {a[n][5]=1; i=n,j=5;}
  100. else if(c=='f') {a[n][6]=1; i=n,j=6;}
  101. else if(c=='g') {a[n][7]=1; i=n,j=7;}
  102. else if(c=='h') {a[n][8]=1; i=n,j=8;}
  103. f>>n>>c;
  104. if(c=='a') {a[n][1]=1; i1=n, j1=1;}
  105. else if(c=='b') {a[n][2]=1; i1=n, j1=2;}
  106. else if(c=='c') {a[n][3]=1; i1=n,j1=3;}
  107. else if(c=='d') {a[n][4]=1; i1=n, j1=4;}
  108. else if(c=='e') {a[n][5]=1; i1=n,j1=5;}
  109. else if(c=='f') {a[n][6]=1; i1=n,j1=6;}
  110. else if(c=='g') {a[n][7]=1; i1=n,j1=7;}
  111. else if(c=='h') {a[n][8]=1; i1=n,j1=8;}
  112. while(f>>n)
  113. {
  114. f>>c;
  115. if(c=='a') a[n][1]=9;
  116. else if(c=='b') a[n][2]=9;
  117. else if(c=='c') a[n][3]=9;
  118. else if(c=='d') a[n][4]=9;
  119. else if(c=='e') a[n][5]=9;
  120. else if(c=='f') a[n][6]=9;
  121. else if(c=='g') a[n][7]=9;
  122. else if(c=='h') a[n][8]=9;
  123. }
  124. ii=i; ji=j;
  125. bac(i,j);
  126. if(ok)
  127. {
  128. g<<i<<" ";
  129. if(j==1) g<<'a'<<endl;
  130. else if(j==2) g<<'b'<<endl;
  131. else if(j==3) g<<'c'<<endl;
  132. else if(j==4) g<<'d'<<endl;
  133. else if(j==5) g<<'e'<<endl;
  134. else if(j==6) g<<'f'<<endl;
  135. else if(j==7) g<<'g'<<endl;
  136. else if(j==8) g<<'h'<<endl;
  137. for(i=0;i<l;i++)
  138. {
  139. g<<x[i]/10<<" ";
  140. if(x[i]%10==1) g<<'a'<<endl;
  141. else if(x[i]%10==2) g<<'b'<<endl;
  142. else if(x[i]%10==3) g<<'c'<<endl;
  143. else if(x[i]%10==4) g<<'d'<<endl;
  144. else if(x[i]%10==5) g<<'e'<<endl;
  145. else if(x[i]%10==6) g<<'f'<<endl;
  146. else if(x[i]%10==7) g<<'g'<<endl;
  147. else if(x[i]%10==8) g<<'h'<<endl;
  148. }
  149. }
  150. else g<<"IMPOSIBIL";
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement