D e s c r i p t i o n Description Description
奶牛们按不太传统的方式玩起了小孩子们玩的"跳房子"游戏。奶牛们创造了一个5x5的、由与x,y轴平行的数字组成的直线型网格,而不是用来在里面跳的、线性排列的、带数字的方格。然后他们熟练地在网格中的数字中跳:向前跳、向后跳、向左跳、向右跳(从不斜过来跳),跳到网格中的另一个数字上。他们再这样跳啊跳(按相同规则),跳到另外一个数字上(可能是已经跳过的数字)。一共在网格内跳过五次后,他们的跳跃构建了一个六位整数(可能以0开头,例如000201)。
求出所有能被这样创造出来的不同整数的总数。
I n p u t Input Input
第1到5行: 这样的网格,一行5个整数。
O u t p u t Output Output
第1行: 能构建的不同整数的总数
S a m p l e Sample Sample I n p u t Input Input
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
S a m p l e Sample Sample O u t p u t Output Output
15
T r a i n Train Train o f of of T h o u g h t Thought Thought
直接暴力
时间复杂度
O
(
25
∗
4
6
)
O(25\ *\ 4 ^ 6)
O(25 ∗ 46)吧
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int dx[5] = {0, 0, 0, 1, -1};
const int dy[5] = {0, 1, -1, 0, 0};
int Map[10][10], Bol[1000005], A[10];
int Ans;
bool Check(int x, int y)
{
if(x < 1 || x > 5 || y < 1 || y > 5)return 0;
return 1;
}
void Dfs(int x, int y, int k, int l)
{
k = k * 10 + Map[x][y];
if(l == 5)
{
if(!Bol[k])
{
Bol[k] = 1;
Ans++;
}
return;
}
for(int i = 1; i <= 4; ++i)
if(Check(x + dx[i], y + dy[i]))
Dfs(x + dx[i], y + dy[i], k, l + 1);
}
int main()
{
for(int i = 1; i <= 5; ++i)
for(int j = 1; j <= 5; ++j)
scanf("%d", &Map[i][j]);
for(int i = 1; i <= 5; ++i)
for(int j = 1; j <= 5; ++j)
Dfs(i, j, 0, 0);
printf("%d", Ans);
return 0;
}