POJ 3050 Hopscotch (exhaustion Search)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=3050


Surface:

Hopscotch
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2563 Accepted: 1823

Description

The cows play the child's game of hopscotch in a non-traditional. 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 has leading zeroes like 000201).

Determine the count of the number of distinct integers that can is created in this manner.

Input

* Lines 1..5:the grid, five integers per line

Output

* Line 1:the number of distinct integers so can be constructed

Sample Input

1 1 1 1 11 1 1 1 11 1 1 1 11 1 1 2 11 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 is possible.

Source

Usaco 2005 November Bronze


Main topic:

A cow in the 5*5 can start jumping from any point, each can jump in 4 directions, Hop 5 times, according to walk through the square to form a length of 6 sequence, asked how many of these sequences? (squares can be skipped repeatedly.) )


Solving:

The topic hangs when writes is the exhaustion search, sees the picture so small, estimates is the violent search means. The enumeration starts at each point, jumps to 5 steps, stops, and records the path that is formed at this point, using the set row weight count.


Code:

#include <iostream> #include <cstdio> #include <string> #include <set> #include <vector> Using namespace Std;char map[6][6],path[10];//Four directions int dir[4][2]={0,1,-1,0,0,-1,1,0};//count set <string> cnt; String tmp;//determines if the square inside bool inside (int x,int y) {if (x>=1&&x<=5&&y>=1&&y<=5) return True;else return false;} Deep search void dfs (int x,int y,int step) {int tx,ty;path[step]=map[x][y];if (step==5) {//record path   tmp= "";       for (int i=0;i<=5;i++)   tmp+=path[i];   Cnt.insert (TMP);   return;} Jump in four directions for (int i=0;i<4;i++) {tx=x+dir[i][0];ty=y+dir[i][1];if (inside (tx,ty)) DFS (tx,ty,step+1);}} int main () {//read in    for (int i=1;i<=5;i++) for (int j=1;j<=5;j++) scanf ("%c", &map[i][j]);//enumeration starts at each point for (int i =1;i<=5;i++) for (int j=1;j<=5;j++) DFS (i,j,0);//Output printf ("%d\n", Cnt.size ()); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3050 Hopscotch (exhaustion search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.