poj 3050 Hopscotch 【DFS】

Hopscotch

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2156   Accepted: 1548

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.

数据太水了,简单深搜

代码:

#include <cstdio>
#include <cstring>

const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int ans, num, map[10][10];
bool vis[1000000];

void dfs(int x, int y, int cur, int step){
    if(step == 6){
        if(!vis[cur]){
        vis[cur] = 1;
        ++ans;
        }
        return ;
    }
    int i;
    for(i = 0; i < 4; i ++){
        int nx = x+dx[i]; int ny = y+dy[i];
        if(nx<0||nx>=5||ny<0||ny>=5) continue;
        dfs(nx, ny, cur*10+map[nx][ny], step+1);
    }
}

void sol(){
    ans = 0;
    int i, j;
    for(i = 0; i < 5; i ++){
        for(j = 0; j < 5; j ++){
            //printf("fdjkl");
            dfs(i, j, map[i][j], 1);
        }
    }
}

int main(){
    int i, j;
    for(i = 0; i < 5; i ++)
        for(j = 0; j < 5; j ++)
            scanf("%d", &map[i][j]);

   // printf("dfgjk\n");
    sol();
    printf("%d\n", ans);
    return 0;
}
时间: 2024-08-01 21:29:42

poj 3050 Hopscotch 【DFS】的相关文章

poj 1011 Sticks 【DFS】+【剪枝】

题意:有未知根(长度一样)木棒(小于等于n),被猪脚任意的截成n段,猪脚(脑抽了)想知道被截之前的最短长度(我推测猪脚得了健忘症). 这道题光理解题意就花了好久,大意就是任意根被截后的木棒拼到一起,能不能组成s(<=n)根的相同的木棒, 例:数据 9  5 1 2 5 1 2 5 1 2 可以组成最短为6 的(5+1, 2+2+2)3根木棒. 策略:深搜. 不过要是传统的深搜的话,TLE妥妥的.所以要剪枝(所谓剪枝,就是多加几个限制条件). 话不多说直接上代码. 代码1: #include <

poj 3050 Hopscotch【搜索、去重】

点击打开题目 Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2126   Accepted: 1524 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 cow

POJ3050 Hopscotch 【DFS】

Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2113   Accepted: 1514 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 creat

poj 3009 Curling 2.0 【DFS】

题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者3)处才能停止,(就是反射来反射去知道反射经过3).如果反射10次还不能到达3,就输出-1. 策略:深搜. 易错点,方向不容易掌握,并且,出题人把n, m顺序反了. 代码: #include<stdio.h> #include<string.h> int map[25][25]; int ans, n, m; const int dir[4][2] = {

poj 1321 棋盘问题 【DFS】

题意:... 策略:深搜. 仔细分析我们发现,我们只需要对列进行标记,对于行我们考虑放棋子还是不放就行了. 代码: #include<stdio.h> #include<string.h> char s[10][10]; int n, m; int vis[10]; int ans; void dfs(int cur, int step) { if(step == m){ ans ++; return; } if(cur > n-1) return ; int i, j; f

poj 1088 滑雪 【记忆化搜索】+【DFS】

策略:如题 题目链接:http://poj.org/problem?id=1088 代码: #include<stdio.h> #include<string.h> int map[105][105], dp[105][105], n, m; const int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向 int limit(int x, int y) //判断是不是越界了 { if(x<1||x>n||y<1||

poj 3258 River Hopscotch 【二分】

题目真是不好读,大意如下(知道题意就很好解了) 大致题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都有唯一的距离 问现在要移除m块石头(S和E除外),每次移除的是与当前最短距离相关联的石头,要求移除m块石头后,使得那时的最短距离尽可能大,输出那个最短距离. //Memory Time //420K 391MS #include<iostream> #include<algorithm> using

POJ--1753--Flip Game【DFS】

链接:http://poj.org/problem?id=1753 题意:一个4*4的方格,有白棋或者黑棋,每次操作是一个位置的颜色翻转,即白变黑.黑变白,并且与它相邻的四个位置的颜色也都跟着改变,问最少要变化多少次才能使所有棋子都是白色或者都是黑色. 思路:不难看出一个棋子翻偶数次和不翻的效果是一样的,并且如果选定了一些棋子翻,翻的顺序对最后的结果是没有影响的,所以可以用DFS去枚举每个棋子的状态:翻或不翻,最多2^16的复杂度就能求出,65536次. 不过我WA了一发,因为每次我在dfs最后

POJ 2442 Sequence【堆】

题目链接:http://poj.org/problem?id=2442 题目大意:给出一个m*n的矩阵,从每一行中取出一个数相加,能得到n^m个不同的结果,要求输出其中前n项. 建立一个以n元数组为底层数组的堆,在这里,利用stl中的make_heap,pop_heap,push_heap等函数解决. 1.将第一组数据输入arr1数组,升序排序. 2.将接下来的数据输入到arr2数组中,并且heap[i]=arr1[0]+arr2[0...n-1],make_heap(heap,heap+n).