Hopscotch
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2777 | Accepted: 1943 |
Description
The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of numbered boxes into which to hop, the cows create a 5x5 rectilinear grid of digits parallel to the x and y axes.
They then adroitly hop onto any digit in the grid and hop forward, backward, right, or left (never diagonally) to another digit in the grid. They hop again (same rules) to a digit (potentially a digit already visited).
With a total of five intra-grid hops, their hops create a six-digit integer (which might have leading zeroes like 000201).
Determine the count of the number of distinct integers that can be created in this manner.
They then adroitly hop onto any digit in the grid and hop forward, backward, right, or left (never diagonally) to another digit in the grid. They hop again (same rules) to a digit (potentially a digit already visited).
With a total of five intra-grid hops, their hops create a six-digit integer (which might have leading zeroes like 000201).
Determine the count of the number of distinct integers that can be created in this manner.
Input
* Lines 1..5: The grid, five integers per line
Output
* Line 1: The number of distinct integers that can be constructed
Sample 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
Sample Output
15
Hint
OUTPUT DETAILS:
111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, and 212121 can be constructed. No other values are possible.
111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, and 212121 can be constructed. No other values are possible.
恩,题目大意就是说,一个5*5的方格网,然后每格有一数字,每次只能走向当前格子的上下左右四个方向上的一步,可以重复走同一格,问6步之后形成的数的种类数。
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int cnt,a[7][7];
bool rec[1000000];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int judge(int x,int y)
{
if(x<1||x>5||y<1||y>5)
return 0;
return 1;
}
void dfs(int x,int y,int c,int v)
{
if(c==6)
{
if(!rec[v])
{
cnt++;
rec[v]=true;
}
return ;
}
for(int i=0;i<4;++i)
{
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(judge(nx,ny))
dfs(nx,ny,c+1,v*10+a[nx][ny]);
}
}
int main()
{
while(~scanf("%d",&a[1][1]))
{
cnt=0;
for(int i=2;i<=5;++i)
scanf("%d",&a[1][i]);
for(int i=2;i<=5;++i)
{
for(int j=1;j<=5;++j)
scanf("%d",&a[i][j]);
}
memset(rec,false,sizeof(rec));
for(int i=1;i<=5;++i)
{
for(int j=1;j<=5;++j)
dfs(i,j,0,0);
}
printf("%d\n",cnt);
}
return 0;
}