题意:给你一个n*m的矩阵,上面的'#'代表你不能走的地方,'.'表示你能走的地方,'@'表示你的起点,问你最多能走多少格。
题目链接:点击打开链接
难度:水
分析:从起点开始dfs走一遍,用ans记录结果,并用vis[x][y]记录map[x][y]是否已经走过,如果没走过便走过去且ans++
直接上代码
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define Max 20+5
char a[Max][Max];
bool b[Max][Max];
int ans = 1; //算上起点
struct point
{
int first, second;
}p[4] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
void dfs(int line, int row)
{
for (int i = 0; i < 4; i++) //枚举4个方向
{
int x = line + p[i].first, y = row + p[i].second;
if (!b[x][y] && a[x][y] == '.')
{
b[x][y] = true;
ans++;
dfs(x, y);
}
}
return;
}
int main()
{
int W, H;
while (scanf("%d%d", &W, &H))
{
if (W == H&&W == 0) break;
int line, row;
ans = 1;
memset(a, false, sizeof(a));
memset(b, false, sizeof(b));
for (int i = 1; i <= H; i++)
for (int j = 1; j <= W; j++)
{
cin >> a[i][j];
if (a[i][j] == '@') { line = i; row = j; }
}
b[line][row] = true;
dfs(line, row);
cout << ans << endl;
}
return 0;
}