Hopscotch
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.
Source
题目类型:DFS
算法分析:直接搜索6步并判断,注意使用string会TLE!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 5e4 + 66; const int dx[] = {0, -1, 0, 1}; const int dy[] = {-1, 0, 1, 0}; int aa[10][10], res; set <int> se; void dfs (int x, int y, int t, int cur) { if (t == 6) { if (se.count (cur) == 0) { res++; se.insert (cur); } return ; } for (int i = 0; i < 4; i++) { int tx = x + dx[i], ty = y + dy[i]; if (tx >= 1 && tx <= 5 && ty >= 1 && ty <= 5) dfs (tx, ty, t + 1, cur * 10 + aa[tx][ty]); } } int main() { // CFF; while (scanf ("%d", &aa[1][1]) != EOF) { for (int i = 1; i <= 5; i++) for (int j = 1; j <= 5; j++) { if (i == 1 && j == 1) continue; scanf ("%d", &aa[i][j]); } se.clear (); res = 0; for (int i = 1; i <= 5; i++) for (int j = 1; j <= 5; j++) { dfs (i, j, 0, 0); } printf ("%d\n", res); } return 0; } |
- « 上一篇:poj3669