标签:
Hopscotch
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status Practice POJ 3050
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.
题意:
给出一个5x5的矩阵,矩阵的每一个单元包含一个数字,从任意一个单元格除法向上下左右四个方向可重复地走,依次记录每一次单元的数字,得到一个长为6的字符串。问一共能得到多少不同种类的字符串。
输入:
一个5x5的矩阵,每个单元格包含一个数字,单元格之间有空格分开。
输出:
不同字符串的种类数。
分析:
使用深度优先算法,以每一个单元格作为起点进行深搜。利用一个vis数组来存储最终得到的字符串是否访问过。
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <set> 7 #include <string> 8 using namespace std; 9 const int MAX_N = 5; 10 int G[MAX_N + 1][MAX_N + 1]; 11 bool vis[10000000]; 12 int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1}; 13 int ans; 14 void DFS(int x,int y,int len,int cur){ 15 if(len == 6){ 16 if(!vis[cur]){ 17 vis[cur] = 1; 18 ans++; 19 } 20 return; 21 } 22 for(int i = 0 ; i < 4 ; i++){ 23 int nx = x + dx[i]; 24 int ny = y + dy[i]; 25 if(nx >= 0 && nx < 5 && ny >= 0 && ny < 5) 26 DFS(nx,ny,len + 1,cur * 10 + G[nx][ny]); 27 } 28 return; 29 } 30 int main(){ 31 ans = 0; 32 memset(vis,0,sizeof vis); 33 for(int i = 0 ; i < 5 ; i++) 34 for(int j = 0 ; j < 5 ; j++) 35 scanf("%d",&G[i][j]); 36 //for(int i = 0 ; i < 5 ; i++){for(int j = 0 ; j < 5 ; j++)printf("%d%c",G[i][j],j == 4 ? ‘\n‘ : ‘ ‘);} 37 for(int i = 0 ; i < 5 ; i++) 38 for(int j = 0 ; j < 5; j++) 39 DFS(i,j,1,G[i][j]); 40 printf("%d\n",ans); 41 return 0; 42 }
标签:
原文地址:http://www.cnblogs.com/cyb123456/p/5782275.html