Source code for submission s584

ololo.cpp

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <memory.h>
  6. #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
  7. #define FI(i,b) FOR(i,0,b)
  8. #define V(t) vector < t >
  9. #define pb push_back
  10. #define MEMS(a,b) memset((a),(b),sizeof(a))
  11. using namespace std;
  12.  
  13. int dx[]={1,1,-1,-1,2,2,-2,-2};
  14. int dy[]={2,-2,2,-2,1,-1,1,-1};
  15. int n,m;
  16. int was[110][110];
  17. int p[110*110][2];
  18. int d[110][110];
  19. inline bool iscor(int x, int y)
  20. {
  21. return ((x>=0) && (y>=0) && (x<=n-1) && (y<=m-1));
  22. }
  23.  
  24.  
  25. int main()
  26. {
  27. int xs,ys,xt,yt;
  28. while (scanf("%d%d%d%d%d%d",&n,&m,&xs,&ys,&xt,&yt)!=EOF)
  29. {
  30. MEMS(was,0);
  31. //MEMS(p,0);
  32. //MEMS(d,0);
  33. int l=0,r=0;
  34. was[xs][ys]=1;
  35. d[xs][ys]=0;
  36. p[0][0]=xs;
  37. p[0][1]=ys;
  38. int res=-1;
  39. while (l<=r)
  40. {
  41. int x=p[l][0];
  42. int y=p[l][1];
  43. if ((x==xt) && (y==yt))
  44. {
  45. res=d[x][y];
  46. break;
  47. }
  48. FOR(k,0,8)
  49. {
  50. int nx=x+dx[k];
  51. int ny=y+dy[k];
  52. if (iscor(nx,ny) && (was[nx][ny]==0))
  53. {
  54. was[nx][ny]=1;
  55. d[nx][ny]=d[x][y]+1;
  56. r++;
  57. p[r][0]=nx;
  58. p[r][1]=ny;
  59. }
  60. }
  61. l++;
  62. }
  63. if (res>=0)
  64. printf("%d\n",res);
  65. else
  66. printf("impossible\n");
  67. }
  68. return 0;
  69. }