• 【题解】跳房子-C++


    Description
    奶牛按不太传统的方式玩起小朋友玩的跳房子游戏,现给出一个5*%的由数字组成的网格。它们在格子中向前前跳,向后跳,向左跳,向右跳,跳到网格中另一个数字后,又这样继续跳(可能跳到某个已跳过的数字)。一共在网格中跳过五次后,它们经过的格子数组成一个六位数(可能是0开始头的,例如000201).现问所有可以被跳出来的不同整数的总数。
    Input
    输入共五行,每行五个数字.
    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, and 212121 can be constructed.

    No other values are possible.

    今天考试2题…
    又是一道DFS,分别从每个点出发,往四周拓展,不打标记,深度超限就判断当前情况是否出现过,没出现过就标记然后cnt++,查找工作可以交给set来完成,这样就不需要打标记这些了…
    ov.

     1 /*
     2 这道题看题目描述感觉ans用int存,前导0存不下,所以干脆就用字符串一位一位加
     3 */ 
     4 #include<bits/stdc++.h>
     5 using namespace std;
     6 int cnt;
     7 string ans;
     8 char mp[11][11];
     9 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    10 bool in(int x,int y)
    11 {
    12     return 1<=x&&x<=5&&1<=y&&y<=5;
    13 }
    14 set<string> s;
    15 void dfs(int x,int y,int dep,string an)
    16 {
    17     an+=mp[x][y];
    18     if(dep==6)
    19     {
    20         if(!s.count(an))
    21         {
    22             s.insert(an);
    23             cnt++;
    24         }
    25         return;
    26     }
    27     for(int i=0;i<4;i++)
    28     {
    29         int nx=x+dir[i][0],ny=y+dir[i][1];
    30         if(in(nx,ny))
    31         {
    32             dfs(nx,ny,dep+1,an);
    33         }
    34     }
    35     return;
    36 }
    37 int main()
    38 {
    39     for(int i=1;i<=5;i++)
    40         for(int j=1;j<=5;j++)
    41             cin>>mp[i][j];
    42     for(int i=1;i<=5;i++)
    43     {
    44         for(int j=1;j<=5;j++)
    45         {
    46             dfs(i,j,1,"");
    47         }
    48     }
    49     cout<<cnt<<endl;
    50     return 0;
    51 }
    个人博客地址: www.moyujiang.com 或 moyujiang.top
  • 相关阅读:
    python基础篇 08 文件操作
    python基础篇 07set集合 深浅拷贝
    python 基础篇 06 编码 以及小知识点补充
    python基础篇 05字典
    钉钉中设置代码提交提醒--Github机器人(转)
    Spring Boot 之FilterRegistrationBean --支持web Filter 排序的使用(转)
    Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置(转)
    为什么添加了@Aspect 还要加@Component(转)
    Servlet 服务器 HTTP 响应
    Servlet 客户端 HTTP 请求
  • 原文地址:https://www.cnblogs.com/moyujiang/p/11213368.html
Copyright © 2020-2023  润新知