Appoint description:
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
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.
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矩阵要你找出6位不同的数有多少个。
AC代码:
/*
这道题真的是水爆了,用bfs轻松过了。
*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
using namespace std;
const int T=500;
#define inf 0x3f3f3f3fL
typedef long long ll;
int a[15][15];
int fx[][2] = {{1,0},{0,1},{-1,0},{0,-1}};
bool jugde(int x,int y)
{
if(x>=0&&x<5&&y>=0&&y<5)
return true;
return false;
}
struct node
{
int x,y;
int v,c;
node(){}
node(int _x,int _y,int _1,int _2):x(_x),y(_y),v(_1),c(_2){}
};
set<int> v;
void bfs()
{
queue<node> q;
node p;
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
q.push(node(i,j,0,0));
while(!q.empty())
{
p = q.front();q.pop();
if(p.c==6){
v.insert(p.v);
continue;
}
for(int k=0;k<4;++k){
int tx = p.x + fx[k][0];
int ty = p.y + fx[k][1];
if(jugde(tx,ty)){
q.push(node(tx,ty,p.v*10+a[p.x][p.y],p.c+1));
}
}
}
}
}
}
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
int i,j,k;
for(i=0;i<5;++i){
for(j=0;j<5;++j){
scanf("%d",&a[i][j]);
}
}
bfs();
printf("%d\n",v.size());
return 0;
}