POJ 题目 Hopscotch(dfs)

来源:互联网 发布:java发起get请求 编辑:程序博客网 时间:2024/05/19 16:06
Hopscotch
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2147 Accepted: 1542

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 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 are possible.

Source

USACO 2005 November Bronze
比赛时题目都没理解,,,,真的只想弱弱的说一句真的不会做搜索题~
ac代码
#include<stdio.h>#include<string>#include<set>#include<iostream>using namespace std;int x[5]={0,1,0,-1};int y[5]={1,0,-1,0};int map[5][5];set<int>p;void dfs(int i,int j,int v,int num){int k;if(v==6){p.insert(num);return;}if(i<0||i>=5)return;if(j<0||j>=5)return;for(k=0;k<4;k++){dfs(i+x[k],j+y[k],v+1,num*10+map[i][j]);}}int main(){int i,j;for(i=0;i<5;i++){for(j=0;j<5;j++){scanf("%d",&map[i][j]);}}for(i=0;i<5;i++){for(j=0;j<5;j++){dfs(i,j,0,0);}}printf("%d\n",p.size());}


0 0
原创粉丝点击