求助一道队内题
  • 板块题目总版
  • 楼主ZBAA_MKC
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/4/2 21:37
  • 上次更新2023/11/5 01:08:56
查看原帖
求助一道队内题
228770
ZBAA_MKC楼主2021/4/2 21:37

题目链接

#include <bits/stdc++.h>
using namespace std;

char c[25][25];
bool vis[25][25];

const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
int w, h;
void dfs(int x, int y)
{
	if (x < 1 || y < 1 || x > w || y > h || c[x][y] == '#')
	{
		return;
	}
	for (int i = 0; i < 4; i++)
	{
		int xx = x + dx[i];
		int yy = y + dy[i];
		vis[xx][yy] = true;
		dfs(xx, yy);
	}
} 

int main()
{
	while(1) 
	{
		cin >> w >> h;
		if (w == 0 && h == 0)
		{
			break;
		} 
		int tmpi, tmpj;
		for (int i = 1; i <= h; i++)
		{
			for (int j = 1; j <= w; j++)
			{
				cin >> c[i][j];
				if (c[i][j] == '@')
				{
					tmpi = i;
					tmpj = j;
				}
			}
		}
		memset(vis, false, sizeof(vis));
		dfs(tmpi, tmpj);
		int ans = 1;
		for (int i = 1; i <= w; i++)
		{
			for (int j = 1; j <= h; j++)
			{
				if (vis[i][j] == true)
				{
					ans++;
				}
			} 
		}
		cout << ans << "\n";
	}
	return 0;
}

稳定输出1

2021/4/2 21:37
加载中...