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
Examples
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.
题意:
在5*5的格子里,填充着各种个位数,现在可以从任意点出发,上下左右移动,可以重复走过的点,每次只能走5步。问经过的格子中组成的6位数(允许有前导零的存在)有多少种?
题解:
简单Dfs哦, set去重一下
#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1 << 30;
const int maxn = 10;
char maze[maxn][maxn];
int act[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};
set<string> se;
bool judge(int x, int y){return x>0&&x<=5&&y>0&&y<=5;}
void Dfs(int x, int y, string s){
if(s.length() == 6){
se.insert(s);
return;
}
int cx, cy;
for(int i = 0; i < 4; ++i){
cx = x+act[i][0], cy = y+act[i][1];
if(judge(cx, cy))
Dfs(cx, cy, s+maze[cx][cy]);
}
}
int main() {
for(int i = 1; i <= 5; ++i)
for(int j = 1; j <= 5; ++j)
cin >> maze[i][j];
string tmp;
for(int i = 1; i <= 5; ++i)
for(int j = 1; j <= 5; ++j)
Dfs(i, j, tmp=maze[i][j]);
cout << se.size() << endl;
return 0;
}