hdu2612 又是迷宫系列

题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2612/

题意:有两个人在地图上不同的位置,地图上由若干个餐厅,求两人能同时到达一个餐厅所用最少的总时间。

代码如下:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned int ui;
 4 typedef long long ll;
 5 typedef unsigned long long ull;
 6 #define pf printf
 7 #define mem(a,b) memset(a,b,sizeof(a))
 8 #define prime1 1e9+7
 9 #define prime2 1e9+9
10 #define pi 3.14159265
11 #define lson l,mid,rt<<1
12 #define rson mid+1,r,rt<<1|1
13 #define scand(x) scanf("%llf",&x) 
14 #define f(i,a,b) for(int i=a;i<=b;i++)
15 #define scan(a) scanf("%d",&a)
16 #define dbg(args) cout<<#args<<":"<<args<<endl;
17 #define inf 0x3f3f3f3f
18 #define maxn 205
19 int n,m,t;
20 char Map[maxn][maxn];
21 bool vis[maxn][maxn];
22 int a[2][2];
23 struct node{
24     int x,y,step;
25 };
26 node st,cur,nxt;
27 vector<node>p1;
28 vector<node>p2;
29 int dir[][2]={0,1,0,-1,1,0,-1,0};
30 void bfs(int x,int y,vector<node> &p)
31 {
32     mem(vis,false);//记住放在bfs里面,该函数将会多次调用 
33     queue<node>q;
34     st.x=x,st.y=y,st.step=0;
35     q.push(st);
36     vis[x][y]=true;
37     while(!q.empty())
38     {
39         cur=q.front();
40         q.pop();
41         if(Map[cur.x][cur.y]=='@')
42         {
43             p.push_back(cur);//每个点只有一次机会进入队列,故vector中只有不同的'@'位置 
44         }
45         f(i,0,3)
46         {
47             nxt=cur;
48             nxt.x+=dir[i][0];
49             nxt.y+=dir[i][1];
50             nxt.step++;
51             if(nxt.x<1||nxt.x>n||nxt.y<1||nxt.y>m||Map[nxt.x][nxt.y]=='#')continue;
52             if(vis[nxt.x][nxt.y])continue;
53             vis[nxt.x][nxt.y]=true;
54             q.push(nxt);
55         }
56     }
57 }
58 int main()
59 {
60     //freopen("input.txt","r",stdin);
61     //freopen("output.txt","w",stdout);
62     std::ios::sync_with_stdio(false);
63     while(scanf("%d%d",&n,&m)==2)
64     {
65         char c;
66         f(i,1,n)
67             f(j,1,m)
68             {
69                 scanf(" %c",&Map[i][j]);
70                 if(Map[i][j]=='Y')a[0][0]=i,a[0][1]=j;
71                 if(Map[i][j]=='M')a[1][0]=i,a[1][1]=j;
72             }
73             p1.clear();
74             p2.clear();
75             bfs(a[0][0],a[0][1],p1);
76             bfs(a[1][0],a[1][1],p2);
77             int ans=inf;
78             f(i,0,p1.size()-1)
79                 f(j,0,p2.size()-1)
80                 {
81                     if((p1[i].x==p2[j].x)&&(p1[i].y==p2[j].y))//到达同一个点 
82                     {
83                         ans=min(ans,p1[i].step+p2[j].step);
84                     //    dbg(ans);
85                     }
86                 }
87             pf("%d\n",ans*11);
88             
89      } 
90  } 

 

 

posted @ 2020-03-17 09:10  WA自动机~  阅读(137)  评论(0编辑  收藏  举报