Hopscotch POJ

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.
 
翻译:

给出一个5*5的矩阵,每一点都有一个数字,让你分别从任意一个数字出发,向上下左右移动,移动6次,所走的路线组成一个字符串。路径可以返回。求:一共可以组成多少个不同的字符串?

思路:以一个整数存放路线,搜索所有情况,用set去重。最后得出结果。要注意只能走六步。

 1 #include <cstdio>
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <string>
 9 #include <cstring>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <sstream>
14 #include <iostream>
15 #define mod 1000000007
16 #define eps 1e-6
17 #define ll long long
18 #define INF 0x3f3f3f3f
19 using namespace std;
20 
21 int fx[4]={1,-1,0,0},fy[4]={0,0,1,-1};
22 int sz[6][6];
23 set<int> se;
24 
25 void dfs(int i,int j,int s,int num)
26  {
27      if(s==6)
28      {
29          if(!se.count(num))
30          {
31              se.insert(num);
32          }
33          return ;
34      }
35      for(int k=0;k<4;k++)
36      {
37          int x=i+fx[k];
38          int y=j+fy[k];
39          if(x>=0&&y>=0&&x<5&&y<5&&s<6)
40          {
41              dfs(x,y,s+1,num*10+sz[x][y]);
42          }
43      }
44  }
45 int main()
46 {
47     for(int i=0;i<5;i++)
48     {
49         for(int j=0;j<5;j++)
50         {
51             scanf("%d",&sz[i][j]);
52         }
53     }
54     for(int i=0;i<5;i++)
55     {
56         for(int j=0;j<5;j++)
57         {
58             dfs(i,j,1,sz[i][j]);
59         }
60     }
61     cout<<se.size()<<endl;
62 
63 }

 

原文地址:https://www.cnblogs.com/mzchuan/p/11199242.html