1. 程式人生 > >poj 3050 搜尋

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;
}