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.
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.
首先读这个题目的时候发现一个问题,题目没有规定从什么地方开始跳,从另一个角度讲就是任意一个地方开始都可以,,,我就这样试了试,暴力深搜,发现可以ac的,果然要多刷题才有更多经验
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
#define N 10003
#define PI 3.14159265358979323
#define INF 0x3f3f3f3f
#define MOD 100000007
#define ll long long
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
int n,a[N];
int gp[10][10];
int W,H;
int cnt=0;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int us[22][22];
int ans[10000];
void dfs ( int x,int y,int dp,int sum )
{
int i;
int nx,ny;
if ( dp>=5 ) // 已经跳了5次就结束了
{
int f;
f=1;
for ( i=0;i<cnt;i++)
{
if ( ans[i]==sum ) //看是否已经存在这样的解
f=0;
}
if ( f )
ans[cnt++]=sum;
//cout<<cnt<<endl;
return ;
}
for ( i=0;i<4;i++ )
{
nx=x+dx[i];
ny=y+dy[i];
if ( nx>-1&&ny>-1&&nx<5&&ny<5 )
{
dfs ( nx,ny,dp+1,sum*10+gp[nx][ny]);
}
}
}
//template < class T> void read ( T &x ){ char c;int sign=1;x=0;while ( c=getchar(),(c>'9'||c<'0')&&c!='-') ;if ( c=='-' ){sign=-1;}else x=c-'0';while ( c=getchar(),c<='9'&&c>='0' ){x=x*10+c-'0';}x=x*sign;}
int main ( )
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int i,j;
while ( cin>>gp[0][0] )
{
int sx,sy;
memset ( ans,0,sizeof(ans));
cnt=0;
cin>>gp[0][1]>>gp[0][2]>>gp[0][3]>>gp[0][4];
for ( i=1;i<5;i++ )
{
for (j=0;j<5;j++ )
{
cin>>gp[i][j];
}
}
/*cout<<"\n"<<endl;
for ( i=0;i<5;i++ )
{
for ( j=0;j<5;j++)
cout<<gp[i][j];
cout<<endl;
} */
for ( i=0;i<5;i++ )
{
for ( j=0;j<5;j++ )
{
dfs(i,j,0,gp[i][j] ); //每一个点都要试试 ,一次也没跳,所以dp为0
}
}
printf ( "%d\n",cnt);
}
return 0;
}