Guest User

Untitled

a guest
Dec 8th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. ifstream ifs("input.txt");
  8. ofstream ofs("output.txt");
  9.  
  10. int v[101][101];
  11. int w[101][101];
  12. int t;
  13.  
  14. pair<int,int> check(int x, int y,int k){
  15.     int td=(v[x][y])?t:1;
  16.     if(w[x][y]>k+td){
  17.         w[x][y]=k+td;
  18.         return make_pair(x,y);
  19.         };
  20.     return make_pair(0,0);
  21. };
  22.  
  23. int main()
  24. {
  25.     int n,m,x1,x2,y1,y2;
  26.     ifs>>m>>n>>y1>>x1>>y2>>x2>>t;
  27.     for(int i=0;i<=n+1;i++)
  28.         for(int j=0;j<=m+1;j++)
  29.             v[i][j]=-1;
  30.     for(int i=1;i<=n;i++)
  31.         for(int j=1;j<=m;j++) ifs>>v[i][j];
  32.     for(int i=1;i<=n;i++)
  33.             for(int j=1;j<=m;j++) w[i][j]=10000;
  34.     w[x1][y1]=0;
  35.     queue< pair<int,int> > q;
  36.     pair<int,int> temp(0,0);
  37.     q.push(make_pair(x1,y1));
  38.     int x,y,td=0;
  39.     while(!q.empty()){
  40.         x=q.front().first;
  41.         y=q.front().second;
  42.         q.pop();
  43.         if(v[x+1][y]!=-1)temp=check(x+1,y,w[x][y]);
  44.         if(!((temp.first==0)||(temp.second==0)))q.push(temp);
  45.         if(v[x-1][y]!=-1)temp=check(x-1,y,w[x][y]);
  46.         if(!((temp.first==0)||(temp.second==0)))q.push(temp);
  47.         if(v[x][y+1]!=-1)temp=check(x,y+1,w[x][y]);
  48.         if(!((temp.first==0)||(temp.second==0)))q.push(temp);
  49.         if(v[x][y-1]!=-1)temp=check(x,y-1,w[x][y]);
  50.         if(!((temp.first==0)||(temp.second==0)))q.push(temp);
  51.     }
  52.     ofs<<w[x2][y2];
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment