您的位置:首页 > 编程语言 > Go语言

hdu 1312 Red and Black

2015-07-19 16:13 603 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312

题       意:@所位于的由‘。’组成的块的大小(包括@)

思       路:从@的位置开始DFS遍历所有可以到达的‘。’

代码如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <algorithm>
using namespace std;
char vis[40][40];
int n, m, ans;
int dx[4]= {1,-1,0,0},dy[4]= {0,0,1,-1};
void DFS( int x, int y )
{
if(x > 0 && x <= n && y > 0 && y <= m && vis[x][y]=='.' )
{
ans++;
vis[x][y]='#';
for ( int i = 0; i < 4; i ++ )
{
int xx=x+dx[i];
int yy=y+dy[i];
DFS( xx, yy );
}
}
}
int main()
{
while( scanf ( "%d %d", &m, &n ) != EOF )
{
int x1, y1;
if( n == 0 && m == 0 ) break;
for ( int i = 1; i <= n; i ++ )
for( int j = 1; j <= m; j ++ )
{
scanf ( " %c", &vis[i][j] );
if( vis[i][j] == '@' )
{
x1=i;
y1=j;
}
}
ans=0;
//cout<<x1<<" "<<y1<<endl;
vis[x1][y1]='.';
DFS(x1,y1);
printf("%d\n",ans);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  DFS algorithm