0
点赞
收藏
分享

微信扫一扫

E - E (dfs)

爪哇驿站 2022-08-10 阅读 68



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



题目大概:

在地图上走五次,求有多少中不同的六位数。

思路:

dfs

代码:

#include <iostream>

using namespace std;

int a[6][6],b[10][10][10][10][10][10];
int map[10][10];
int sum;
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int dp[7];

int dfs(int x,int y,int t)

{
if(t==6)
{
if(!b[dp[1]][dp[2]][dp[3]][dp[4]][dp[5]][dp[6]])
{sum++;
b[dp[1]][dp[2]][dp[3]][dp[4]][dp[5]][dp[6]]=1;
}
return 0;
}
int xx,yy;
for(int i=0;i<4;i++)
{
xx=x+dx[i];
yy=y+dy[i];
if(xx>0&&xx<=5&&yy>0&&yy<=5)
{ dp[t+1]=a[xx][yy];
dfs(xx,yy,t+1);
}
}

}

int main()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
cin>>a[i][j];

}
}
sum=0;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{ dp[1]=a[i][j];
dfs(i,j,1);

}
}

cout<<sum<<endl;



return 0;
}



举报

相关推荐

0 条评论