Hopscotch(POJ 3050 DFS)

Hopscotch

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2845   Accepted: 1995

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

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.

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <set>
 5 #include <cstring>
 6 using namespace std;
 7 int a[5][5];
 8 int n;
 9 int vis[5][5];
10 int dx[]={0,0,-1,1},dy[]={-1,1,0,0};
11 set<int> se;
12 int dfs(int x,int y,int s,int num)
13 {
14     if(s==6)
15     {
16         //cout<<num<<endl;
17         se.insert(num);
18         return 0;
19     }
20     for(int i=0;i<4;i++)
21     {
22         int nx=x+dx[i];
23         int ny=y+dy[i];
24         if(nx>=0&&ny>=0&&nx<=4&&ny<=4&&vis[nx][ny]==0)
25         {
26         //    vis[nx][ny]=1;
27             dfs(nx,ny,s+1,num*10+a[nx][ny]);
28         //    vis[nx][ny]=0;
29         }
30     }
31     return 0;
32 }
33 int main()
34 {
35     int i,j;
36     freopen("in.txt","r",stdin);
37     memset(vis,0,sizeof(vis));
38     for(i=0;i<5;i++)
39         for(j=0;j<5;j++)
40             scanf("%d",&a[i][j]);
41     for(i=0;i<5;i++)
42         for(j=0;j<5;j++)
43         {
44             //vis[i][j]=1;
45             dfs(i,j,1,a[i][j]);
46             //vis[i][j]=0;
47         }
48     printf("%d\n",se.size());
49 //    set<int>::iterator a=se.begin();
50     /*for(;a!=se.end();a++)
51     {
52         cout<<*a<<endl;
53     }*/
54 }
时间: 2024-10-19 17:55:55

Hopscotch(POJ 3050 DFS)的相关文章

Oil Deposits(poj 1526 DFS入门题)

http://poj.org/problem?id=1562 Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12595   Accepted: 6868 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp wor

poj1753 Flip Game(枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1753 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the o

HDU 1016 Prime Ring Problem (素数筛+DFS)

题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... 1 //1016 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 6 using namespace std ; 7 8 bool vis[21]; 9 int prime[42] ,cs[21]; 10 int n ; 11

hdu1881 毕业bg(深搜dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1881 Problem Description 每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为"bg".参加不同团体的bg会有不同的感觉,我们可以用一个非负整数为每个bg定义一个"快乐度".现给定一个bg列表,上面列出每个bg的快乐度.持续长度.bg发起人的离校时间,请你安排一系列bg的时间使得自己可以获得最大的快乐度. 例如有4场bg: 第1场

zoj 3811 Untrusted Patrol(bfs或dfs)

Untrusted Patrol Time Limit: 3 Seconds      Memory Limit: 65536 KB Edward is a rich man. He owns a large factory for health drink production. As a matter of course, there is a large warehouse in the factory. To ensure the safety of drinks, Edward hir

hdu 1518 Square(深搜dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 --------------------------------------------------------------------------------------------------------------------------------------------

hdu 4771 Stealing Harry Potter&#39;s Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: 目的:让你从 '@' 点出发,然后每个点只能走一次,求出最小的距离: 解题思路:先用 bfs 求解出任意两点之间的距离,用 ans[i][j],表示点 i 到点  j 的距离: 然后用 dfs 递归求出从起点经过所有点的距离中,比较出最小的: AC代码: 1 #include<iostream>

图的遍历(bfs 和dfs)

BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个图. 由此可以看出,用BFS进行搜索所搜索的顶点都是按深度进行扩展的,先找到到V0距离为1的所有顶点,然后找到距离V0为2的顶点……所以BFS所搜索到的都是最短的路径. 由于要将距离V0为d(d>0)的且未被方位的点都记录起来,我们采用队列这种数据结构.队列的特点是先进先出(FIFO),从某个顶点出

CodeForces 707D Persistent Bookcase ——(巧妙的dfs)

一个n*m的矩阵,有四种操作: 1.(i,j)处变1: 2.(i,j)处变0: 3.第i行的所有位置1,0反转: 4.回到第k次操作以后的状态: 问每次操作以后整个矩阵里面有多少个1. 其实不好处理的操作只有第四个,但是这题的思路很巧妙,123三种操作全部建立顺边,第四种操作将k和这次操作的序号建边,然后dfs进行操作即可,遇到尽头,则退回到前一个分岔点,并且回溯的过程中将操作反转. 具体见代码: 1 #include <stdio.h> 2 #include <algorithm>