本文主要是介绍poj 3050 dfs + set的妙用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:
给一个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 longusing 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 LOCALfreopen("in.txt", "r", stdin);
#endif // LOCALans.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;
}
这篇关于poj 3050 dfs + set的妙用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!