这题一定要广搜吗......

P1649 [USACO07OCT] Obstacle Course S

Seauy @ 2018-06-05 17:23:36

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

int n,ans=9999999;
char mapn[110][110];
int direx[5]={0,0,1,-1};
int direy[5]={1,-1,0,0};
bool wrong=1,visit[110][110];

bool in_scp(int bef,int ob,int aft)
{
     return bef<=ob && ob<=aft;     
}

struct Point
{
       int x,y;       
}A;

void DFS(int x,int y,int turn,int dire)
{
     if(mapn[y][x]=='B') wrong=0,ans=min(ans,turn);
     else
         for(int i=0;i<=3;i++)
                 if(in_scp(1,x+direx[i],n) && in_scp(1,y+direy[i],n) && mapn[y+direy[i]][x+direx[i]]!='x' && !visit[y+direy[i]][x+direx[i]])
                            visit[y+direy[i]][x+direx[i]]=1,DFS(x+direx[i],y+direy[i],turn+(dire!=i),i),visit[y+direy[i]][x+direx[i]]=0;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                    cin>>mapn[i][j];
                    if(mapn[i][j]=='A') A.x=j,A.y=i;
            }
    int dire=0;
    for(int i=0;i<=3;i++)
            if(in_scp(1,A.x+direx[i],n) && in_scp(1,A.y+direy[i],n) && mapn[A.y+direy[i]][A.x+direx[i]]!='x')
                    dire=i,DFS(A.x+direx[i],A.y+direy[i],0,i);//0 up,1 down,2 right,3 left
    cout<<(wrong ? -1 : ans);
    if(0) system("pause");
    return 0;
}

50分,超时超得死死的,难道BFS这道题比DFS快?为什么勒?


by Seauy @ 2018-06-09 10:54:15

好吧,问了一句废话......


by LYqwq @ 2022-07-13 15:20:32

考古


|