B - Red and Black+dfs

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int w,h;
char f[21][21];
int k,kk; 


int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};




int main()
{

int dfs(int r,int c);


int i,j;
while(scanf("%d%d",&w,&h))
{




getchar();
if (w==0 && h==0 ) break;



memset(f,0,sizeof(f)); 
 


int startx,starty;

for (i=1;i<=h;i++)
{
for (j=1;j<=w;j++)
{
scanf("%c",&f[i][j]);
if (f[i][j]=='@') {startx=i; starty=j;f[i][j]=1;}


if (f[i][j]=='#') { f[i][j]=1;}




}
getchar();
}



k=1; 
dfs(startx,starty);
 



printf("%d\n",k);


 

}

return 0;





int is_board(int r, int c)
{

if ( r>=1 && r<=h && c>=1 && c<=w) return 1;
return 0;
}




int dfs(int r,int c)

 
int i;
for (i=0;i<4;i++)
{
if (  is_board(r+dx[i],c+dy[i]) && f[r+dx[i]][c+dy[i]]=='.' )
{
f[r+dx[i]][c+dy[i]]=1;
 
k++;

dfs(r+dx[i],c+dy[i]); 
 

}
}


  









return 0;
}

你可能感兴趣的:(B - Red and Black+dfs)