Advertisement
a53

taxa

a53
Jan 31st, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define oo 0x3f3f3f3f
  3. using namespace std;
  4. ifstream fin("taxa.in");
  5. ofstream fout("taxa.out");
  6. int a[1001][1001];
  7. int d[1001][1001];
  8. int n,m,xs,ys,xf,yf;
  9. deque <pair<int,int>>q;
  10. const int dx[]={0, 0, -1, 1, -1, -1, 1, 1};
  11. const int dy[]={-1, 1, 0, 0, -1, 1, -1, 1};
  12.  
  13. inline bool inM(int i,int j)
  14. {
  15. return (i<=n&&1<=i&&1<=j&&j<=m);
  16. }
  17.  
  18. void Lee()
  19. {
  20. int i,j,x,y;
  21. d[xs][ys]=0;
  22. q.push_back({xs,ys});
  23. while(!q.empty())
  24. {
  25. i=q.front().first;
  26. j=q.front().second;
  27. q.pop_front();
  28. for(int k=0;k<8;++k)
  29. {
  30. x=i+dx[k];
  31. y=j+dy[k];
  32. if(inM(x, y))
  33. {
  34. int ac=0;
  35. if(a[x][y]!=a[i][j])
  36. ac=a[i][j]*a[x][y];
  37. if(ac+d[i][j]<d[x][y]&&ac+d[i][j]<d[xf][yf])
  38. {
  39. d[x][y]=ac+d[i][j];
  40. q.push_back({x,y});
  41. }
  42. }
  43. }
  44. }
  45. }
  46.  
  47. int main()
  48. {
  49. fin>>n>>m>>xs>>ys>>xf>>yf;
  50. for(int i=1;i<=n;++i)
  51. for(int j=1;j<=m;++j)
  52. fin>>a[i][j],d[i][j]=oo;
  53. Lee();
  54. fout<<d[xf][yf]<<'\n';
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement