标签:
Description
Input
Output
Sample Input
Sample Output
1 #include "iostream" 2 #include "queue" 3 using namespace std; 4 struct ma 5 { 6 char z; 7 int sum; 8 }; 9 struct person 10 { 11 int i; 12 int j; 13 int time; 14 }; 15 int v[4][2] ={1,0,-1,0,0,-1,0,1}; 16 ma maze[202][202]; 17 ma maze2[202][202]; 18 person fir,sec; 19 void scopy(int n,int m) 20 { 21 for (int i=0;i<=n+1;i++) 22 for (int j=0;j<=m+1;j++) 23 { 24 if (maze2[i][j].z == ‘@‘) 25 maze[i][j].z = ‘@‘; 26 else 27 maze[i][j]=maze2[i][j]; 28 } 29 } 30 void mbegin(int n,int m) 31 { 32 int i,j; 33 for (i=0;i<=n+1;i++) 34 for (j=0;j<=m+1;j++) 35 if (i*j == 0 || i == n+1 || j ==m+1) 36 maze2[i][j].z =‘#‘; 37 else 38 { 39 cin>>maze2[i][j].z; 40 maze2[i][j].sum=0; 41 maze[i][j].sum=0; 42 if (maze2[i][j].z == ‘Y‘) 43 { 44 fir.i = i; 45 fir.j = j; 46 } 47 if (maze2[i][j].z == ‘M‘) 48 { 49 sec.i =i; 50 sec.j =j; 51 } 52 } 53 } 54 void bfs(person &then) 55 { 56 queue<person> p; 57 person next; 58 then.time=0; 59 maze[then.i][then.j].z = ‘#‘; 60 p.push(then); 61 while (!p.empty()) 62 { 63 next = p.front(); 64 p.pop(); 65 for (int k=0;k<4;k++) 66 { 67 then.i = next.i+v[k][0]; 68 then.j = next.j+v[k][1]; 69 if (maze[then.i][then.j].z != ‘#‘) 70 { 71 then.time = next.time+11; 72 if (maze[then.i][then.j].z == ‘@‘) 73 maze[then.i][then.j].sum += then.time; 74 maze[then.i][then.j].z =‘#‘; 75 p.push(then); 76 } 77 } 78 } 79 } 80 int mintime(int n,int m) 81 { 82 int k=0,time; 83 for (int i=1;i<=n;i++) 84 for (int j=1;j<=m;j++) 85 if (maze[i][j].z == ‘@‘) 86 { 87 if (maze[i][j].sum == 0) 88 continue; 89 if (k++ == 0) 90 time=maze[i][j].sum; 91 if (time > maze[i][j].sum) 92 time=maze[i][j].sum; 93 } 94 return time; 95 } 96 int main() 97 { 98 int n,m; 99 while (cin>>n>>m) 100 { 101 mbegin(n,m); 102 scopy(n,m); 103 bfs(fir); 104 scopy(n,m); 105 bfs(sec); 106 scopy(n,m); 107 cout<<mintime(n,m)<<endl; 108 } 109 return 0; 110 }
暑假集训(1)第四弹 -----Find a way(Hdu2612)
标签:
原文地址:http://www.cnblogs.com/huas-zlw/p/5676814.html