题目

July 6, 2021 · View on GitHub

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.

输入描述

  • Lines 1..5: The grid, five integers per line

输出描述

  • Line 1: The number of distinct integers that can be constructed

输入例子

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

输出例子

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.

参考答案

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
#define ls id<<1,l,mid
#define rs id<<1|1,mid+1,r
#define CLR(x) memset(x,0,sizeof x)
#define MEM(x) memset(x,0x3f,sizeof x)
#define rep(i,a,b) for (int i=a;i<b;i++)
#define Rep(i,a,b) for (int i=a;i>=b;i--)
typedef long long ll ;
typedef pair<int,int> pii ;
const int maxn = 1e5+50 ;
const int inf = 0x3f3f3f3f ;
const int MOD = 1e9+7 ;
set<string>s; 
char a[5][5];
int d[3]={0,1,-1};
void dfs(int x,int y,string cur,int k) {
	if (k==5) {
		s.insert(cur);
		return ;
	}
	rep(i,0,3) rep(j,0,3) {
		if ((i==0&&j==0)||(i&&j)) continue ;
		int nx=x+d[i],ny=y+d[j];
		if (nx>4||nx<0||ny<0||ny>4) continue ;
		dfs(nx,ny,cur+a[nx][ny],k+1); 
	}
}
int main () {
	rep(i,0,5) rep(j,0,5) cin >> a[i][j] ;
	rep(i,0,5) rep(j,0,5) {
		string t;t+=a[i][j];
		dfs(i,j,t,0);
	} 
	cout << s.size() << endl ;
	return 0;
}