描述
有一间长方形的房子,地上铺了红色、黑色两种颜色的正方形瓷砖。你站在其中一块黑色的瓷砖上,只能向上下左右四个方向的相邻的黑色瓷砖移动。请写一个程序,计算你总共能够到达多少块黑色的瓷砖。
这题是一道深搜题,只需要注意一下方位数组就行了;
下面出示代码:
#include <bits/stdc++.h>
using namespace std;
int a[4]={0,0,-1,1},b[4]={-1,1,0,0},cnt,m,n,c,d;
char s[25][25];
void shensourumenhongyuhei(int x,int y){
cnt++;
int e,f;
s[x][y]='@';
for(int i=0;i<4;i++){
e=x+a[i];
f=y+b[i];
if(e<=m&&f<=n&&e>=0&&f>=0&&s[e][f]=='.')shensourumenhongyuhei(e,f);
}
}
int main(){
cin>>n>>m;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>s[i][j];
if(s[i][j]=='@'){
c=i;
d=j;
}
}
}
shensourumenhongyuhei(c,d);
cout<<cnt;
return 0;
}
1957

被折叠的 条评论
为什么被折叠?



