poj 3050 搜尋

來源:互聯網
上載者:User
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 11 1 1 1 11 1 1 1 11 1 1 2 11 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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.