杭电2612

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 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
 

Sample Output
 
   
66 88 66
 


搜索

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

写了一发bfs超时了

先贴下代码、

#include
#include
#include
#include
#include
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;iq;
        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

交了之后我都觉得自己蠢

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

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

这样走了无数次重复的路


正解是从两个点搜索两次


#include
#include
#include
#include
#include
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;iq;
        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


你可能感兴趣的:(杭电2612)