杭电2612

来源:互联网 发布:js继承面试题详解 编辑:程序博客网 时间:2024/05/29 14:53
Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
 

Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200). 
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’    express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
 

Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
 

Sample Input
4 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
 

Sample Output
668866
 


搜索

第一次看到这道题以为很难就先放下了今天又来写

写了一发bfs超时了

先贴下代码、

#include<iostream>#include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;int n,m,vis[201][201],d[4][2]={1,0,-1,0,0,1,0,-1};char mm[201][201];int vj(int x,int y){    if(x<0||x>=n||y<0||y>=m||vis[x][y])return 0;    return 1;}struct node {    int x,y,t;}k[40000],a,b;int main(){    while(cin>>n>>m){        for(int i=0;i<n;i++)            scanf("%s",mm[i]);        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                if(mm[i][j]=='Y'){                    a.x=i;                    a.y=j;                    a.t=0;                }            }        }        memset(vis,0,sizeof(vis));        queue<node>q;        q.push(a);        int len=0;        while(!q.empty()){            a=q.front();            q.pop();            if(mm[a.x][a.y]=='@'){                k[len++]=a;            }            for(int i=0;i<4;i++){                int xx=a.x+d[i][0];                int yy=a.y+d[i][1];                if(vj(xx,yy)&&mm[xx][yy]!='#'){                    b.x=xx;                    b.y=yy;                    b.t=a.t+11;                    vis[xx][yy]=1;                    q.push(b);                }            }        }        int t[40001];        int h=0;        for(int i=0;i<len;i++){            memset(vis,0,sizeof(vis));            q.push(k[i]);            while(!q.empty()){                a=q.front();                q.pop();                if(mm[a.x][a.y]=='M'){                    t[h++]=a.t;                    break;                }                for(int i=0;i<4;i++){                    int xx=a.x+d[i][0];                    int yy=a.y+d[i][1];                    if(vj(xx,yy)&&mm[xx][yy]!='#'){                        b.x=xx;                        b.y=yy;                        b.t=a.t+11;                        vis[xx][yy]=1;                        q.push(b);                    }                }            }        }        sort(t,t+h);        cout<<t[0]<<endl;    }    return 0;}

交了之后我都觉得自己蠢

第一次思路是从一个点去找肯德基,然后再从肯德基找另一个人

当肯德基数量跟多的时候就会超时

这样走了无数次重复的路


正解是从两个点搜索两次


#include<iostream>#include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;int n,m,vis[201][201],t[200][200],d[4][2]={1,0,-1,0,0,1,0,-1};char mm[201][201];int vj(int x,int y){    if(x<0||x>=n||y<0||y>=m||vis[x][y])return 0;    return 1;}struct node {    int x,y,t;}k[40000],a,b,c;int main(){    while(cin>>n>>m){        for(int i=0;i<n;i++)            scanf("%s",mm[i]);        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                t[i][j]=99999999;                if(mm[i][j]=='Y'){                    a.x=i;                    a.y=j;                    a.t=0;                }                if(mm[i][j]=='M'){                    c.x=i;                    c.y=j;                    c.t=0;                }            }        }        memset(vis,0,sizeof(vis));        queue<node>q;        q.push(a);        while(!q.empty()){            a=q.front();            q.pop();            if(mm[a.x][a.y]=='@'){                t[a.x][a.y]=a.t;            }            for(int i=0;i<4;i++){                int xx=a.x+d[i][0];                int yy=a.y+d[i][1];                if(vj(xx,yy)&&mm[xx][yy]!='#'){                    b.x=xx;                    b.y=yy;                    b.t=a.t+11;                    vis[xx][yy]=1;                    q.push(b);                }            }        }        memset(vis,0,sizeof(vis));        q.push(c);        while(!q.empty()){            a=q.front();            q.pop();            if(mm[a.x][a.y]=='@'){                t[a.x][a.y]+=a.t;            }            for(int i=0;i<4;i++){                int xx=a.x+d[i][0];                int yy=a.y+d[i][1];                if(vj(xx,yy)&&mm[xx][yy]!='#'){                    b.x=xx;                    b.y=yy;                    b.t=a.t+11;                    vis[xx][yy]=1;                    q.push(b);                }            }        }        int minn=99999999;        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                if(t[i][j]<minn)minn=t[i][j];            }        }        cout<<minn<<endl;    }    return 0;}


1 0