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;
}