L i n k Link Link
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
直接暴力出每一种情况,判断是否有重复就好了
C o d e Code Code
#include<iostream>
#include<cstdio>
using namespace std;
int ans;
int a[6][6]; bool b[1000004];
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
bool check(int x, int y)
{
if (x < 1 || x > 5 || y < 1 || y > 5) return 0;
return 1;
}
void work(int x, int y, int sum, int num)
{
sum = sum * 10 + a[x][y];
if (num == 5) {
if (!b[sum]) {
b[sum] = 1;
ans++;
}
return;
}
for (int i = 0; i < 4; ++i)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (check(xx, yy)) {
work(xx, yy, sum, num + 1);
}
}
}
int main()
{
for (int i = 1; i <= 5; ++i)
for (int j = 1; j <= 5; ++j)
scanf("%d", &a[i][j]);
for (int i = 1; i <= 5; ++i)
for (int j = 1; j <= 5; ++j)
work(i, j, 0, 0);
printf("%d", ans);
}