题意:
给一个5x5的矩阵,求由多少个由连续6个元素组成的不一样的字符的个数。
解析:
dfs + set去重搞定。
代码:
#include <iostream>
#include <cstdio>
#include <set>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long
using namespace std;
const int maxn = 300 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);
int dir[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
set<int> ans;
int g[10][10];
void dfs(int x, int y, int sum, int dep)
{
if (dep == 6)
{
ans.insert(sum);
return;
}
for (int i = 0; i < 4; i++)
{
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (1 <= nx && nx <= 5 && 1 <= ny && ny <= 5)
{
dfs(nx, ny, sum * 10 + g[x][y], dep + 1);
}
}
return;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ans.clear();
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
scanf("%d", &g[i][j]);
}
}
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
dfs(i, j, 0, 0);
}
}
printf("%d\n", ans.size());
return 0;
}