Hopscotch
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2244 | Accepted: 1608 |
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.
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暴力枚举所有长度为6的排列 然后用set维护 - - 结果就是set的大小
AC代码如下:
//
// POJ 3050 Hopscotch
//
// Created by TaoSama on 2015-02-21
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
char a[8][8];
int d[4][2] = { -1, 0, 1, 0, 0, 1, 0, -1};
set<string> s;
void dfs(int x, int y, string cur, int k) {
if(k == 5) {
s.insert(cur);
return;
}
for(int i = 0; i < 4; ++i) {
int nx = x + d[i][0], ny = y + d[i][1];
if(nx < 1 || nx > 5 || ny < 1 || ny > 5) continue;
dfs(nx, ny, cur + a[nx][ny], k + 1);
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
for(int i = 1; i <= 5; ++i)
for(int j = 1; j <= 5; ++j)
cin >> a[i][j];
for(int i = 1; i <= 5; ++i) {
for(int j = 1; j <= 5; ++j) {
string cur; cur += a[i][j];
dfs(i, j, cur, 0);
}
}
cout << s.size() << endl;
return 0;
}

本文介绍了一种基于Hopscotch游戏的算法实现,通过在一个5x5的数字网格中进行五次移动来构造六位数,并使用深度优先搜索算法(DFS)遍历所有可能的组合,最终确定可以创建的不同整数的数量。
&spm=1001.2101.3001.5002&articleId=43900783&d=1&t=3&u=e1ec9483683f42adb78616ccf0b07f63)
215

被折叠的 条评论
为什么被折叠?



