• POJ 3050 Hopscotch 四方向搜索


    Hopscotch

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6761   Accepted: 4354

    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.

    题意:给一个5x5的矩阵,从矩阵的任意一个位置开始搜索6次,(允许方向为上下左右四个方向),问一可以产生多少个不同的序列

    #include<iostream>
    #include<string.h>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<set>
    using namespace std;
    char a[10][10];
    int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
    set<string>p;
    string s;
    
    int check(int x,int y)
    {
        if(x>=0&&x<5&&y>=0&&y<5)
            return 1;
        else
            return 0;
    }
    void dfs(int x,int y,int n)
    {
        if(n>=6)
        {
            p.insert(s);
            return ;
        }
        for(int i=0;i<4;i++)
        {
            int dx,dy;
            dx=x+dir[i][0];
            dy=y+dir[i][1];
            if(check(dx,dy)==0)
                continue;
            s=s+a[dx][dy];
            dfs(dx,dy,n+1);
            s.erase(n);
        }
        return ;
    }
    int main()
    {
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<5;j++)
                cin>>a[i][j];
        }
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<5;j++)
            {
                s.clear();
                dfs(i,j,0);
            }
        }
        cout<<p.size()<<endl;
        // set<string>::iterator it;
        // for(it=p.begin();it!=p.end();it++)
        // {
        //     cout<<*it<<endl;
        // }
    }
  • 相关阅读:
    烂泥:KVM虚拟机windows系统增加硬盘
    烂泥:KVM虚拟机克隆
    烂泥:KVM快照的创建与恢复
    烂泥:【解决】word复制windows live writer没有图片
    烂泥:ubuntu中使用virt-manager图形化新建虚拟机
    烂泥:ubuntu安装KVM虚拟机管理virt-manager
    烂泥:【解决】ubuntu使用远程NFS报错
    烂泥:kvm安装windows系统蓝屏
    烂泥:ubuntu安装vmtools
    烂泥: KVM虚拟机Linux系统增加硬盘
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11295587.html
Copyright © 2020-2023  润新知