Hopscotch
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6738 Accepted: 4338
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.
Source
USACO 2005 November Bronze
在5*5的格子里,填充着各种个位数,现在可以从任意点出发,上下左右移动,可以重复走过的点,每次只能走5步。问经过的格子中组成的6位数(允许有前导零的存在)有多少种?
/*--------- Hongjie ----------*/
// #include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int ,int> P;
const int INF = 0x3f3f3f3f;
const int MAXN = 17;
const int N = 5;
int a[MAXN][MAXN];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
map<int, bool> m;
bool check(int x, int y) {
return x>=0 && x<N && y>=0 &&y<N;
}
void dfs(int x, int y, int s, int l) {
if(l==5) {
m[s] = 1;
return ;
}
for(int i=0;i<4;++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if(check(nx,ny)) {
dfs(nx, ny, s*10 + a[nx][ny], l+1);
}
}
}
int main(){
// freopen("../in.txt","r",stdin);
// freopen("../out.txt","w",stdout);
m.clear();
for(int i=0;i<N;++i)
for(int j=0;j<N;++j)
cin>>a[i][j];
for(int i=0;i<N;++i)
for(int j=0;j<N;++j)
dfs(i,j,a[i][j],0);
int sum = 0;
for(map<int,bool>::iterator it = m.begin();it!=m.end();++it) {
sum ++;
// cout<<it->first<<endl;
}
// cout<<endl;
printf("%d\n",sum);
return 0;
}