10分,还有两个TLE。
#include<bits/stdc++.h>
using namespace std;
int a,b,s1,s2,max1=-1;
int fx[]={0,0,0,1,-1};
int fy[]={0,1,-1,0,0};
char s[30][30];
void dfs(int x,int y,int step)
{
max1=max1<step?step:max1;
for(int i=1;i<=4;i++)
{
int tx=x+fx[i],ty=y+fy[i];
if(s[tx][ty]=='.')
{
s[x][y]='#';
dfs(tx,ty,step+1);
s[x][y]='.';
}
}
}
int main()
{
// freopen("1.txt","r",stdin);
cin>>b>>a;
for(int i=1;i<=a;i++)
{
for(int j=1;j<=b;j++)
cin>>s[i][j];
}
for(int i=1;i<=a;i++)
{
for(int j=1;j<=b;j++)
{
if(s[i][j]=='@')
{
s1=i;s2=j;
break;
}
}
}
dfs(s1,s2,1);
cout<<max1;
return 0;
}