POJ-3050

Hopscotch
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4385   Accepted: 2924

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.

 

题意:

对于给定的5x5矩阵,从任意一点开始可重复地走5步所形成的6个数组成数列,问总共可以走多少种。

 

dfs所有的点,当ans==6时将数列存入set中。

此处将数列转换为6位整数可方便存储。

 

AC代码:

 1 //#include<bits/stdc++.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<set>
 8 using namespace std;
 9 
10 int a[5][5];
11 set<int> st;
12 int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
13 
14 void dfs(int x,int y,int ans,int num){
15     if(ans==6){
16         st.insert(num);
17         return ;
18     }
19     for(int i=0;i<4;i++){
20         int nx=x+dx[i];
21         int ny=y+dy[i];
22         if(nx>=0&&nx<5&&ny>=0&&ny<5){
23             ans++;
24             dfs(nx,ny,ans,num*10+a[nx][ny]);
25             ans--;
26         }
27     }
28 }
29 
30 int main(){
31     ios::sync_with_stdio(false);
32     for(int i=0;i<5;i++){
33         for(int j=0;j<5;j++){
34             cin>>a[i][j];
35         }
36     }
37     for(int i=0;i<5;i++){
38         for(int j=0;j<5;j++){
39             dfs(i,j,1,a[i][j]);
40         }
41     }
42     cout<<st.size()<<endl;
43     return 0;
44 }

 

posted @ 2017-07-27 11:08  Kiven#5197  阅读(250)  评论(0编辑  收藏  举报