题意:
给定5x5的矩阵。矩阵中的每个点都可以作为起点,每个点可以跳到附近上、下、左、右四个位置。每个位置可以重复跳跃。
问:执行6次操作,可以组合出多少种不同的序列?
DFS简单题~ 把每次搜到的结果存到set容器内,最后输出set元素个数即可。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
//#define _Test
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int graph[10][10], dir[4][2] = {-1,0, 1,0, 0,1, 0,-1};
set<int> ss;
void dfs(int deep, int x, int y, int val)
{
if(deep == 6)
{
ss.insert(val);
}
else
for(int i = 0; i < 4; ++i)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if(graph[xx][yy] != -1)
{
dfs(deep + 1, xx, yy, val * 10 + graph[x][y]);
}
}
}
int main()
{
#ifdef _Test
freopen("test0.in", "r", stdin);
freopen("test0.out", "w", stdout);
srand(time(NULL));
#endif
memset(graph, -1, sizeof(graph));
while(~scanf("%d", &graph[1][1]))
{
for(int i = 1; i <= 5; ++i)
{
for(int j = 1; j <= 5; ++j)
{
if(j != 1 || i != 1)
scanf("%d", &graph[i][j]);
}
}
for(int i = 1; i <= 5; ++i)
{
for(int j = 1; j <= 5; ++j)
{
dfs(0, i, j, 0);
}
}
printf("%d\n", ss.size());
ss.clear();
}
return 0;
}