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

 

上一篇:《Invisible Inc.》游戏分析:如何在回合制中塑造紧张刺激的体验?


下一篇:第三周homework