poj 3050 搜索

Hopscotch
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1462   Accepted: 1043

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
这道题的题意就是给一个5*5的矩阵,然后从中选出连续的6个数,问能构成多少个不同的数字。
wiking大神告诉我,这些提他瞬间就搞定了,我表示我简直要去撞墙了,弱菜真心不会,不过,唯一让我欣喜的是通过这个题,我又学会了一个stl的函数,就是set,表示stl真的很好用。
下面是代码;
 
 
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<algorithm>
#include<set>

using namespace std;

int map[10][10];
set<int> s;//其实就是相当于一个数组

void dfs(int x,int y,int r,int k){
     if(k==6){
         s.insert(r);//当找到第六个格子的时候,将这个数放入s的关联容器里
         return;
     }
     r=r*10+map[x][y];
     if(x>1)
        dfs(x-1,y,r,k+1);//当他在矩阵的范围内时,让他在4个方向不同的搜索
     if(x<5)
         dfs(x+1,y,r,k+1);
     if(y>1)
        dfs(x,y-1,r,k+1);
     if(y<5)
        dfs(x,y+1,r,k+1);
}

int main(){
     s.clear();//初始化
     for(int i=1;i<=5;i++)
       for(int j=1;j<=5;j++){
             scanf("%d",&map[i][j]);
       }
     for(int i=1;i<=5;i++)
         for(int j=1;j<=5;j++){
              dfs(i,j,0,0);
         }
     printf("%d\n",s.size());//其实答案就是这个数组的长度
     return 0;
}


你可能感兴趣的:(poj 3050 搜索)