Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define oo 0x3f3f3f3f
- using namespace std;
- ifstream fin("taxa.in");
- ofstream fout("taxa.out");
- int a[1001][1001];
- int d[1001][1001];
- int n,m,xs,ys,xf,yf;
- deque <pair<int,int>>q;
- const int dx[]={0, 0, -1, 1, -1, -1, 1, 1};
- const int dy[]={-1, 1, 0, 0, -1, 1, -1, 1};
- inline bool inM(int i,int j)
- {
- return (i<=n&&1<=i&&1<=j&&j<=m);
- }
- void Lee()
- {
- int i,j,x,y;
- d[xs][ys]=0;
- q.push_back({xs,ys});
- while(!q.empty())
- {
- i=q.front().first;
- j=q.front().second;
- q.pop_front();
- for(int k=0;k<8;++k)
- {
- x=i+dx[k];
- y=j+dy[k];
- if(inM(x, y))
- {
- int ac=0;
- if(a[x][y]!=a[i][j])
- ac=a[i][j]*a[x][y];
- if(ac+d[i][j]<d[x][y]&&ac+d[i][j]<d[xf][yf])
- {
- d[x][y]=ac+d[i][j];
- q.push_back({x,y});
- }
- }
- }
- }
- }
- int main()
- {
- fin>>n>>m>>xs>>ys>>xf>>yf;
- for(int i=1;i<=n;++i)
- for(int j=1;j<=m;++j)
- fin>>a[i][j],d[i][j]=oo;
- Lee();
- fout<<d[xf][yf]<<'\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement