【POJ - 3050】Hopscotch (dfs+回溯)

-->Hopscotch

直接写中文了

Descriptions:

奶牛们以一种独特的方式玩孩子们的跳房子游戏。 奶牛们创造了一个5x5的格子 

他们熟练地跳上其中的一个格子,可以前后左右地跳(不能对角)到另一个格子上。之后继续跳(可能跳到曾经跳过的格子上)。 

他们总共跳5次,路径可以看作一个六位数 (准确的说是一个六位序列,如000201是可行的). 

请你找到这样的六位序列的总数

Input

* 输入一个5x5的地图

Output

* 所有可能六位序列的总数

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

输出说明 
111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111,和 212121 是可行的. 没有其他可行的了
 
这题简单粗暴,直接对每一个位置走5步得出一个6位序列数,因为序列要求不同,直接借助ser容器即可,把得到的6位序列数存入set,最后输出set的大小即可
 
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 10
using namespace std;
int n=5;//5*5的地图
int dt[][2]= {{1,0},{-1,0},{0,1},{0,-1}};//4个方向
set<int>number;//存不同的序列
int mp[Maxn][Maxn];//地图
void dfs(int x,int y,int step,int num)//在(x,y)处,序列长度,这个时候的数字
{
   if(step==6)//序列为6,放入ser容器
   {
       number.insert(num);
       return;
   }
   for(int i=0;i<4;i++)//四个方向走路
   {
       int tx=x+dt[i][0];
       int ty=y+dt[i][1];
       if(tx>=0&&ty>=0&&tx<n&&ty<n)//在地图内
       {
           step++;
           dfs(tx,ty,step,num*10+mp[tx][ty]);//更新状态
           step--;//回溯
       }
   }
}
int main()
{
    for(int i=0;i<n;i++)//输入
        for(int j=0;j<n;j++)
            cin>>mp[i][j];
    for(int i=0;i<n;i++)//从每一个点开始dfs走5步
        for(int j=0;j<n;j++)        
            dfs(i,j,1,mp[i][j]);        
    cout<<number.size()<<endl;
}

 

posted on 2019-07-15 22:55  Sky丨Star  阅读(452)  评论(0编辑  收藏  举报

导航