Hopscotch
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2169 | Accepted: 1555 |
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.
题意:
跳房子游戏,一头母牛想到了一个新玩法,从任意一个点开始,上下左右跳,看能跳出来几种新花样。
思路:
深搜不解释,注意状态的改变与保存,把最后的结果当int存储最方便,开一个大点的数组标记 ,七位就足够用了。代码如下:
<span style="font-size:18px;">#include <iostream>
#include <cstring>
using namespace std;
int maze[6][6];//格子
int save[10000000];//储存结果信息
int sum,num;
int dir_x[4]={0,1,0,-1};//方向位移
int dir_y[4]={1,0,-1,0};
void dfs(int,int,int);
int main()
{
for(int i=1;i<6;i++)
{
for(int j=1;j<6;j++)
{
cin>>maze[i][j];
}
}
memset(save,0,sizeof(save));
sum=0;num=0;
for(int i=1;i<=6;i++)
{
for(int j=1;j<=6;j++)
{
dfs(i,j,0);//每个点都进去一次
}
}
cout<<sum<<endl;
return 0;
}
void dfs(int x,int y,int deep)
{
if(deep==6)
{
if(save[num]==0)
{
sum++;
save[num]=1;//标记状态为1,表示出现过这个结果
return;
}
else
return;
}
for(int i=0;i<4;i++)
{
int nx=x+dir_x[i];
int ny=y+dir_y[i];
if(nx>0&&nx<6&&ny>0&&ny<6)
{
num*=10;num+=maze[nx][ny];
dfs(nx,ny,deep+1);
num/=10;//注意把num变回来
}
}
return;
}
</span>