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 }
时间: 2024-12-31 05:53:12

Hopscotch POJ - 3050的相关文章

Hopscotch POJ 3050(搜索)

原题 题目链接 题目分析 bfs限制步数后爆搜就行了,答案用set维护(用来去重). 代码 1 #include <iostream> 2 #include <algorithm> 3 #include <utility> 4 #include <cstdio> 5 #include <cmath> 6 #include <cstring> 7 #include <string> 8 #include <vector

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

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

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【搜索、去重】

点击打开题目 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 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

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

River Hopscotch POJ - 3258

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L uni