zoukankan      html  css  js  c++  java
  • POJ-3050 Hopscotch---DFS

    题目链接:

    https://vjudge.net/problem/POJ-3050

    题目大意:

    给定一个5*5的地图,每个格子上有一个数字。从一个格子出发(上下左右4个方向),走5步将数字连起来可以构造出一个6位数。问该地图可以构造出多少个不同的6位数。

    思路:

    可以对每个格子做深度优先遍历,构造出所有数字,但要注意不要重复计数。在这里,我使用了set来保存已构造出的数字,结果就是set中的元素个数。

     1 #include<iostream>
     2 #include<vector>
     3 #include<queue>
     4 #include<algorithm>
     5 #include<cstring>
     6 #include<cstdio>
     7 #include<set>
     8 #include<map>
     9 #include<cmath>
    10 using namespace std;
    11 typedef pair<int, int> Pair;
    12 typedef long long ll;
    13 const int INF = 0x3f3f3f3f;
    14 int T, n, m, d;
    15 const int maxn = 1e5 + 10;
    16 int Map[10][10];
    17 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
    18 set<int>s;
    19 void dfs(int x, int y, int d, int num)
    20 {
    21     if(d == 6)
    22     {
    23         s.insert(num);
    24         return;
    25     }
    26     for(int i = 0; i < 4; i++)
    27     {
    28         int xx = x + dir[i][0];
    29         int yy = y + dir[i][1];
    30         if(xx >= 0 && xx < 5 && yy >= 0 && yy < 5)
    31         {
    32             dfs(xx, yy, d + 1, num * 10 + Map[xx][yy]);
    33         }
    34     }
    35 }
    36 int main()
    37 {
    38     for(int i = 0; i < 5; i++)for(int j = 0; j < 5; j++)cin >> Map[i][j];
    39     for(int i = 0; i < 5; i++)
    40         for(int j = 0; j < 5; j++)
    41         dfs(i ,j , 1, Map[i][j]);
    42     cout<<s.size()<<endl;
    43     return 0;
    44 }
  • 相关阅读:
    Java 1 (JVM、JRE、JDK之间的关系)
    Java 0 (jdk下载安装及环境配置)
    推荐之链接
    idea 2019激活码
    Mock数据使用的Util
    mybatis慢查询配置
    logback参考配置
    Linux网络实时监控配置
    jmeter插件JMeterPlugins-Standard 压力测试
    ZoneDateTime 转换Date
  • 原文地址:https://www.cnblogs.com/fzl194/p/8823669.html
Copyright © 2011-2022 走看看