码迷,mamicode.com
首页 > 其他好文 > 详细

Hopscotch(POJ 3050 DFS)

时间:2016-01-22 17:00:28      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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 }

 

Hopscotch(POJ 3050 DFS)

标签:

原文地址:http://www.cnblogs.com/a1225234/p/5151266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!