POJ 3050 Hopscotch DFS

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的方格里跳房子,起点是任意位置。将跳过的数连起来组成一个5位数(前导零可能),问一共能组成多少个数字?

 1 #include<iostream>
 2 #include <set>
 3 #include<cmath>
 4 using namespace std;
 5 
 6 int HS[10][10];
 7 int ans;
 8 int xs[] = {1,-1,0,0};
 9 int ys[] = {0,0,1,-1};
10 set<int> s;
11 
12 void process(int row,int col,int count,int temp)
13 {
14     int x,y;
15 
16     temp += HS[row][col]*pow(10*1.0,count);
17     
18     if(count == 5)
19     {
20         if(s.find(temp) == s.end())
21         {
22             ans++;
23             s.insert(temp);
24         }
25             
26         return;
27     }
28 
29     for(int i = 0; i < 4; i++)
30     {
31         y = row + xs[i];
32         x = col + ys[i];
33             if(y < 0 || y > 4 || x < 0 || x > 4)
34             continue;
35         else
36             process(y,x,count+1,temp);
37     }
38 }
39 
40 int main()
41 {
42     for (int i = 0; i < 5; ++i)
43     {
44         for (int j = 0; j < 5; j++)
45             scanf("%d",&HS[i][j]);
46     }
47     s.clear();
48     ans = 0;
49 
50     for(int i = 0; i < 5; i++)
51         for(int j = 0; j < 5; j++)
52             process(i,j,0,0);
53 
54     cout<<ans<<endl;  //这里可以更换为s.size()作为答案
55     return 0;
56 }
57 
58 //set的使用方法:http://blog.csdn.net/yas12345678/article/details/52601454
59 //
原文地址:https://www.cnblogs.com/jj81/p/8378904.html