Hopscotch POJ - 3050

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!
全局变量不在递归的栈中!!!!
满满都是技巧啊!
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<set>
 6 using namespace std;
 7 
 8 set<string> a;
 9 char map[5][5];
10 
11 int dx[4]={0,1,0,-1};
12 int dy[4]={1,0,-1,0};
13 
14 void DFS(int start,int end,int deep,string b){
15     if(deep==5){
16         a.insert(b);
17         return;
18     }
19     for(int i=0;i<4;i++){
20         int mx=start+dx[i],my=end+dy[i];
21         if(mx<0||mx>4||my<0||my>4) continue;
22         string tem=b+map[mx][my];
23         DFS(mx,my,deep+1,tem);
24     }
25 }
26 
27 int main()
28 {    
29     for(int i=0;i<5;i++){
30         for(int j=0;j<5;j++)
31             cin>>map[i][j];
32     }
33     
34     for(int i=0;i<5;i++){
35         for(int j=0;j<5;j++){
36             string ma;
37             ma+=map[i][j];
38             DFS(i,j,0,ma);
39         }
40     }
41     
42     cout<<a.size()<<endl;
43 }

 

posted @ 2017-07-25 23:24  天之道,利而不害  阅读(324)  评论(0编辑  收藏  举报