Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <queue>
- using namespace std;
- ifstream ifs("input.txt");
- ofstream ofs("output.txt");
- int v[101][101];
- int w[101][101];
- int t;
- pair<int,int> check(int x, int y,int k){
- int td=(v[x][y])?t:1;
- if(w[x][y]>k+td){
- w[x][y]=k+td;
- return make_pair(x,y);
- };
- return make_pair(0,0);
- };
- int main()
- {
- int n,m,x1,x2,y1,y2;
- ifs>>m>>n>>y1>>x1>>y2>>x2>>t;
- for(int i=0;i<=n+1;i++)
- for(int j=0;j<=m+1;j++)
- v[i][j]=-1;
- for(int i=1;i<=n;i++)
- for(int j=1;j<=m;j++) ifs>>v[i][j];
- for(int i=1;i<=n;i++)
- for(int j=1;j<=m;j++) w[i][j]=10000;
- w[x1][y1]=0;
- queue< pair<int,int> > q;
- pair<int,int> temp(0,0);
- q.push(make_pair(x1,y1));
- int x,y,td=0;
- while(!q.empty()){
- x=q.front().first;
- y=q.front().second;
- q.pop();
- if(v[x+1][y]!=-1)temp=check(x+1,y,w[x][y]);
- if(!((temp.first==0)||(temp.second==0)))q.push(temp);
- if(v[x-1][y]!=-1)temp=check(x-1,y,w[x][y]);
- if(!((temp.first==0)||(temp.second==0)))q.push(temp);
- if(v[x][y+1]!=-1)temp=check(x,y+1,w[x][y]);
- if(!((temp.first==0)||(temp.second==0)))q.push(temp);
- if(v[x][y-1]!=-1)temp=check(x,y-1,w[x][y]);
- if(!((temp.first==0)||(temp.second==0)))q.push(temp);
- }
- ofs<<w[x2][y2];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment