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.
这道题很皮,唉,很舒服的,不存在的。这道题重点不在深搜而在于#include<set>的使用,尤其是判重的用处实在是好用啊。
这里涉及到了.insert(), .size两个相关操作。
这里还有一个小小的深搜技巧。就是输入时从1开始,然后将数组初始化为-1,这样可以很方便的判断越界。
#include<iostream>
#include<set>
#include<string.h>
#include<stdlib.h>
using namespace std;
int a[7][7];
int re[100];
set<int> S;
void DFS(int x,int y,int k);
int main()
{
int i,j;
memset(a,-1,sizeof(a));
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
cin>>a[i][j];
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
DFS(i,j,0);
int ans;
ans=S.size();
printf("%d\n",ans);
return 0;
}
void DFS(int x,int y,int k)
{
if(a[x][y]<0)
return ;
if(k==6)
{
int i,j,num=0;
for(i=0;i<k;i++)
num=num*10+re[i];
S.insert(num);
return ;
}
re[k]=a[x][y];
DFS(x+1,y,k+1);
DFS(x-1,y,k+1);
DFS(x,y-1,k+1);
DFS(x,y+1,k+1);
}