Hopscotch(POJ 3050 DFS)

Hopscotch
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2845   Accepted: 1995

Description

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.
 
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <set>
 5 #include <cstring>
 6 using namespace std;
 7 int a[5][5];
 8 int n;
 9 int vis[5][5];
10 int dx[]={0,0,-1,1},dy[]={-1,1,0,0};
11 set<int> se;
12 int dfs(int x,int y,int s,int num)
13 {
14     if(s==6)
15     {
16         //cout<<num<<endl;
17         se.insert(num);
18         return 0;
19     }
20     for(int i=0;i<4;i++)
21     {
22         int nx=x+dx[i];
23         int ny=y+dy[i];
24         if(nx>=0&&ny>=0&&nx<=4&&ny<=4&&vis[nx][ny]==0)
25         {
26         //    vis[nx][ny]=1;
27             dfs(nx,ny,s+1,num*10+a[nx][ny]);
28         //    vis[nx][ny]=0;
29         }
30     }
31     return 0;
32 }
33 int main()
34 {
35     int i,j;
36     freopen("in.txt","r",stdin);
37     memset(vis,0,sizeof(vis));
38     for(i=0;i<5;i++)
39         for(j=0;j<5;j++)
40             scanf("%d",&a[i][j]);
41     for(i=0;i<5;i++)
42         for(j=0;j<5;j++)
43         {
44             //vis[i][j]=1;
45             dfs(i,j,1,a[i][j]);
46             //vis[i][j]=0;
47         }
48     printf("%d\n",se.size());
49 //    set<int>::iterator a=se.begin();
50     /*for(;a!=se.end();a++)
51     {
52         cout<<*a<<endl;
53     }*/
54 }

 

posted @ 2016-01-22 14:55  御心飞行  阅读(337)  评论(0编辑  收藏  举报