洛谷 P1683 入门(dfs)

阅读 52

2022-04-20

https://www.luogu.com.cn/problem/P1683?contestId=67264icon-default.png?t=M3C8https://www.luogu.com.cn/problem/P1683?contestId=67264简单的DFS就可以了

AC代码:

#include<bits/stdc++.h>
using namespace std;
char g[21][21];
int bk[21][21],n,m,sx,sy,ans=1;
int dx[4]= {1,-1,0,0},dy[4]= {0,0,1,-1};
void dfs(int x,int y)
{
	for(int i=0; i<4; i++)
	{
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(g[tx][ty]=='.'&&!bk[tx][ty])
		{
			bk[tx][ty]=1;
			ans++;
			dfs(tx,ty);
		}
	}
	return ;
}
int main()
{
	cin>>n>>m;
	for(int i=1; i<=m; i++)
	{
		for(int j=1; j<=n; j++)
		{
			cin>>g[i][j];
			if(g[i][j]=='@')
			{
				sx=i,sy=j;
			}
		}
	}
	dfs(sx,sy);
	cout<<ans<<endl;
	return 0;

}

精彩评论(0)

0 0 举报