POJ--3050 Hopscotch(暴搜)
记录
21:36 2023-4-16
http://poj.org/problem?id=3050
reference:《挑战程序设计竞赛(第2版)》第二章练习题索引 p135
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.
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.
暴搜,直接dfs遍历所有可能的结果,用stl中的set记录下产生的set就可以(没有减枝啥的就能过了
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<set>
#define MAX_N 5
using namespace std;
typedef long long ll;
typedef unsigned int uint;
const int INF = 0x3f3f3f3f;
int arr[MAX_N + 1][MAX_N + 1];
int num_count = 0;
int total = 0;
//4个方向移动的向量
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
set<int> s;
void dfs(int x, int y) {
if(num_count == 5) {
s.insert(total * 10 + arr[x][y]);
return ;
}
num_count += 1;
total = total * 10 + arr[x][y];
for(int k = 0; k < 4; k++) {
int new_x = x + dx[k], new_y = y + dy[k];
if(0 <= new_x && new_x < MAX_N && 0 <= new_y && new_y < MAX_N) {
dfs(new_x, new_y);
}
}
num_count -= 1;
total = (total - arr[x][y]) / 10;
}
void solve() {
for(int i = 0; i < MAX_N; i++) {
for(int j = 0 ; j < MAX_N; j++) {
num_count = 0;
total = 0;
dfs(i, j);
}
}
printf("%d\n", s.size());
}
int main() {
for(int i = 0; i < MAX_N; i++) {
for(int j = 0 ; j < MAX_N; j++) {
scanf("%d", &arr[i][j]);
}
}
solve();
}