Red and Black(HD1312)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312
代码:
#include <iostream>
using namespace std;
int row,col;
char map[101][101];
int dir[8][2]={{-1,0},{1,0},{0,-1},{0,1}};
int startX,startY;
int count;
void dfs(int i,int j)
{
int newr,newc;
map[i][j]='#';
for(int k=0;k<8;k++)
{
newr=i+dir[k][0];
newc=j+dir[k][1];
if(newr>=0&&newr<row&&newc>=0&&newc<col&&map[newr][newc]=='.')
{
dfs(newr,newc);
count++;
}
}
}
int main()
{
while(cin>>col>>row&&row!=0&&col!=0)
{
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
startX=i;
startY=j;
}
}
count=1;
dfs(startX,startY);
cout<<count<<endl;
}
return 0;
}

浙公网安备 33010602011771号