*:使用

 

 

 

Sample Input
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Sample Output
66
88
66

 

 

**************************************************

 

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<queue>
 6 #include<stdlib.h>
 7 #include<map>
 8 #include<cmath>
 9 
10 using namespace std;
11 
12 #define N 350
13 #define INF 0x3f3f3f3f
14 
15 struct node
16 {
17     int x,y,step;
18 };
19 
20 int w[4][2]= { {0,1},{0,-1},{1,0},{-1,0} };
21 int n,m,vis[N][N], v[N][N];
22 char str[N][N];
23 
24 void bfs(node s)
25 {
26     int i;
27 
28     queue<node>Q;
29     node q;
30     s.step=0;
31     Q.push(s);
32     vis[s.x][s.y]=1;
33 
34     while(Q.size())
35     {
36         q=Q.front();
37         Q.pop();
38 
39         if(str[q.x][q.y]=='@')
40             v[q.x][q.y]+=q.step;
41 
42         for(i=0; i<4; i++)
43         {
44             s.x=q.x+w[i][0];
45             s.y=q.y+w[i][1];
46             if(s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&vis[s.x][s.y]==0&&str[s.x][s.y] != '#')
47             {
48                 vis[s.x][s.y]=1;
49                 s.step=q.step+1;
50                 Q.push(s);
51             }
52         }
53     }
54 }
55 
56 int main()
57 {
58     int i,j;
59     node s1,s2;
60 
61     while(scanf("%d %d", &n,&m) != EOF)
62     {
63         memset(v,0,sizeof(v));
64         memset(vis,0,sizeof(vis));
65         memset(str,0,sizeof(str));
66 
67         for(i=0; i<n; i++)
68         {
69             scanf("%s", str[i]);
70             for(j=0; j<m; j++)
71             {
72                 if(str[i][j]=='Y')
73                     s1.x=i,s1.y=j;
74                 if(str[i][j]=='M')
75                     s2.x=i,s2.y=j;
76             }
77         }
78 
79         bfs(s1);
80         memset(vis,0,sizeof(vis));
81         bfs(s2);
82 
83 
84         int ans=INF;
85         for(i=0; i<n; i++)
86             for(j=0; j<m; j++)
87                 if(v[i][j] != 0)
88                     ans=min(ans,v[i][j]);
89 
90         printf("%d\n", ans*11);
91     }
92     return 0;
93 }

 

posted on 2016-07-20 09:39  惟愿。。。  阅读(150)  评论(0编辑  收藏  举报