思路:
一道非常板的深搜题(但我还是求机房大佬把我的屎山代码修改后才完成的。呜呜我太蒻了)。
提供一个不同的思路。
由于本蒟蒻觉得用字符类型的地图进行深搜太麻烦了,所以在输入时将每个南瓜的位置用它们的价值替换,干草块用 0 表示,就将此题转化成普通的深搜了。
再将深搜板子上加一段统计价值的代码就可以愉快地 AC 了。
AC 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include<bits/stdc++.h> using namespace std; int r,c,xx[4]={0,0,-1,1},yy[4]={1,-1,0,0},cont,s,e; char ma[10005][10005]; int mapp[10005][10005]; void dfs(int x,int y){ cont+=mapp[x][y]; mapp[x][y]=0; int nx,ny; for(int i=0;i<=3;i++){ nx=x+xx[i],ny=y+yy[i]; if(mapp[nx][ny]!=0){ dfs(nx,ny); } } } int main(){ cin>>r>>c; for(int i=1;i<=r;i++){ for(int j=1;j<=c;j++){ cin>>ma[i][j]; if(ma[i][j]=='L'){ mapp[i][j]=10; } if(ma[i][j]=='M'){ mapp[i][j]=5; } if(ma[i][j]=='S'){ mapp[i][j]=1; } } } cin>>s>>e; dfs(s+1,e+1); cout<<cont; return 0; }
|