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

时间: 2024-10-12 09:25:42

POJ 3050 Hopscotch DFS的相关文章

poj 3050 Hopscotch 【DFS】

Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2156   Accepted: 1548 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 creat

POJ 3050 Hopscotch (穷竭搜索)

题目链接:http://poj.org/problem?id=3050 题面: Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2563   Accepted: 1823 Description The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of numbered

POJ 3050 Hopscotch 水~

http://poj.org/problem?id=3050 题目大意: 在一个5*5的格子中走,每一个格子有个数值,每次能够往上下左右走一格,问走了5次后得到的6个数的序列一共同拥有多少种?(一開始站的位置算一个,能够走回去) 思路: 近期我就是在做水题... 直接DFS就可以..我用map推断序列是否反复的. #include<cstdio> #include<cstring> #include<map> #include<string> #includ

poj 3050 Hopscotch【搜索、去重】

点击打开题目 Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2126   Accepted: 1524 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 cow

poj 3050 Hopscotch

水题,顺带学个stl的set怎么用. set用法 http://www.cnblogs.com/luosuo10/p/5479668.html 1 #include <iostream> 2 #include <set> 3 #include <cstdio> 4 using namespace std; 5 int temp; 6 set <int> s; 7 int dx[]={0,0,-1,1}; 8 int dy[]={1,-1,0,0}; 9 in

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

[POJ 1011]Sticks(DFS剪枝)

Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were original

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 creat

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