题目描述
奶牛们按不太传统的方式玩起了小孩子们玩的"跳房子"游戏。奶牛们创造了一个5x5的、由与x,y轴平行的数字组成的直线型网格,而不是用来在里面跳的、线性排列的、带数字的方格。然后他们熟练地在网格中的数字中跳:向前跳、向后跳、向左跳、向右跳(从不斜过来跳),跳到网格中的另一个数字上。他们再这样跳啊跳(按相同规则),跳到另外一个数字上(可能是已经跳过的数字)。一共在网格内跳过五次后,他们的跳跃构建了一个六位整数(可能以0开头,例如000201)。
求出所有能被这样创造出来的不同整数的总数。
输入
第1到5行: 这样的网格,一行5个整数。
输出
第1行: 能构建的不同整数的总数
输入样例
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 2 1
1 1 1 1 1
输出样例
15
.
.
.
.
.
.
分析
深搜
.
.
.
.
.
.
程序:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
int sz[10][10],tj=0;
int fx[4]={-1,1,0,0};
int fy[4]={0,0,1,-1};
map<int,bool>t;
void dfs(int s,int dep,int x,int y)
{
if (dep==6)
{
if (t.count(s)==0)
{
t.insert(pair<int,bool>(s,true));
tj++;
}
return;
}
for (int i=0;i<=3;i++)
if (x+fx[i]>=1&&x+fx[i]<=5&&y+fy[i]>=1&&y+fy[i]<=5) dfs(s*10+sz[x+fx[i]][y+fy[i]],dep+1,x+fx[i],y+fy[i]);
}
int main()
{
for (int i=1;i<=5;i++)
for (int j=1;j<=5;j++)
scanf("%d",&sz[i][j]);
for (int i=1;i<=5;i++)
for (int j=1;j<=5;j++)
dfs(sz[i][j],1,i,j);
printf("%d",tj);
return 0;
}