Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <queue>
- using namespace std;
- int s,n,m,a[1001][1001],b[1001][1001];
- queue <pair<int,int> > q;
- int dx[4]={-1,0,1,0};
- int dy[4]={0,1,0,-1};
- bool interior(int x,int y)
- {
- if(x>0 && x<=n && y>0 && y<=m)
- return 1;
- return 0;
- }
- int main()
- {
- int x,y,x1,y1,i,j,startx,starty,stopx,stopy;
- ifstream f("ubuph.in");
- ofstream g("ubuph.out");
- f>>n>>m;
- for(i=1;i<=n;i++)
- for(j=1;j<=m;j++)
- {
- f>>a[i][j];
- b[i][j]=5001000;
- }
- f>>startx>>starty>>stopx>>stopy;
- q.push(make_pair(startx,starty));
- b[startx][starty]=a[startx][starty];
- while(!q.empty())
- {
- x=q.front().first;
- y=q.front().second;
- for(i=0;i<4;i++)
- {
- x1=x+dx[i];
- y1=y+dy[i];
- if(interior(x1,y1) && b[x][y]+a[x1][y1]<b[x1][y1])
- {
- b[x1][y1]=b[x][y]+a[x1][y1];
- q.push(make_pair(x1,y1));
- }
- }
- q.pop();
- }
- g<<b[stopx][stopy];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement