Grandpa’s Walk UVALive - 6038
题目链接:https://cn.vjudge.net/problem/UVALive-6038
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 55;
int maps[maxn][maxn];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int cnt;
void dfs(int x, int y)
{
bool isend = true;
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (maps[nx][ny] && maps[nx][ny] < maps[x][y])
{
dfs(nx, ny);
isend = false;
}
}
if (isend)
cnt++;
}
int main(void)
{
int t, kase = 0;
int n, m;
cin >> t;
while (t--)
{
cout << "Case #";
cout << ++kase << ": ";
cnt = 0;
memset(maps, 0, sizeof(maps));
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
scanf("%d", &maps[i][j]);
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
bool isstart = true;
for (int k = 0; k < 4; k++)
{
if (maps[i][j] < maps[i + dx[k]][j + dy[k]])
{
isstart = false;
break;
}
}
if (isstart)
dfs(i, j);
}
}
cout << cnt << endl;
}
return 0;
}