N - Find a way

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<queue>
 5 using namespace std;
 6 int mark[205][205];
 7 char map[205][205];
 8 int fx[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
 9 int num[205][205],num1[205][205],num2[205][205];
10 struct node
11 {
12     int x,y;
13 }last,now;
14 int n,m;
15 queue<node>q;
16 void bfs(int a,int b)
17 {
18     while(!q.empty())
19     q.pop();
20     last.x=a;last.y=b;
21     q.push(last);
22     while(!q.empty())
23     {
24         last=q.front();
25         q.pop();
26         for(int i=0;i<4;i++)
27         {
28             now.x=last.x+fx[i][0];
29             now.y=last.y+fx[i][1];
30             if(now.x>=0&&now.x<=n&&now.y>=0&&now.y<=m&&mark[now.x][now.y]==0&&map[now.x][now.y]!='#')
31             {
32                 q.push(now);
33                 mark[now.x][now.y]=1;
34                 num[now.x][now.y]=num[last.x][last.y]+1;
35             }
36         }
37     }
38 }
39 int main()
40 {
41     while(~scanf("%d %d",&n,&m))
42     {
43         int i,j,a,b,c,d;
44         for(i=0;i<n;i++)
45         scanf("%s",&map[i]);
46         for(i=0;i<n;i++)
47         {
48             for(j=0;j<m;j++)
49             {
50                 if(map[i][j]=='Y')
51                 {
52                     a=i;b=j;
53                 }
54                 if(map[i][j]=='M')
55                 {
56                     c=i;d=j;
57                 }
58             }
59         }
60         memset(mark,0,sizeof(mark));
61         memset(num,0,sizeof(num));
62         memset(num1,0,sizeof(num1));
63         bfs(a,b);
64         for(i=0;i<n;i++)
65         {
66             for(j=0;j<m;j++)
67             {
68                 num1[i][j]=num[i][j];
69             }
70         }
71         memset(num,0,sizeof(num));
72         memset(mark,0,sizeof(mark));
73         memset(num2,0,sizeof(num2));
74         bfs(c,d);
75         for(i=0;i<n;i++)
76         {
77             for(j=0;j<m;j++)
78             {
79                 num2[i][j]=num[i][j];
80             }
81         }
82         memset(num,0,sizeof(num));
83         memset(mark,0,sizeof(mark));
84         int min=10000000;
85         for(i=0;i<n;i++)
86         {
87             for(j=0;j<m;j++)
88             {
89                 num[i][j]=num1[i][j]+num2[i][j];
90                 if(map[i][j]=='@'&&min>num[i][j])
91                 min=num[i][j];
92             }
93         }
94         printf("%d\n",min*11);
95     } 
96     return 0;
97 }

 

posted @ 2018-04-29 15:14  ouyang_wsgwz  阅读(130)  评论(0编辑  收藏  举报