zoukankan      html  css  js  c++  java
  • Hopscotch POJ

    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

    要使用数据结构啊啊啊啊!
    学到了怎么写有深度限制的DFS!
    全局变量不在递归的栈中!!!!
    满满都是技巧啊!
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<set>
     6 using namespace std;
     7 
     8 set<string> a;
     9 char map[5][5];
    10 
    11 int dx[4]={0,1,0,-1};
    12 int dy[4]={1,0,-1,0};
    13 
    14 void DFS(int start,int end,int deep,string b){
    15     if(deep==5){
    16         a.insert(b);
    17         return;
    18     }
    19     for(int i=0;i<4;i++){
    20         int mx=start+dx[i],my=end+dy[i];
    21         if(mx<0||mx>4||my<0||my>4) continue;
    22         string tem=b+map[mx][my];
    23         DFS(mx,my,deep+1,tem);
    24     }
    25 }
    26 
    27 int main()
    28 {    
    29     for(int i=0;i<5;i++){
    30         for(int j=0;j<5;j++)
    31             cin>>map[i][j];
    32     }
    33     
    34     for(int i=0;i<5;i++){
    35         for(int j=0;j<5;j++){
    36             string ma;
    37             ma+=map[i][j];
    38             DFS(i,j,0,ma);
    39         }
    40     }
    41     
    42     cout<<a.size()<<endl;
    43 }
  • 相关阅读:
    修改tomcat的get方法的参数长度
    [转]修改hosts文件不起作用
    [转]solr DataImportHandler 解决mysql 表导入内存溢出问题
    plat模板修改记录
    Linux基本命令
    使用DTM ( Dynamic Topic Models )进行主题演化实验
    Stuts2的"struts.devMode"设置成true后,不起作用,仍需要重启tomcat
    R语言——绘图函数深入学习
    R语言——基本绘图函数
    Stem Cell 华人科学家
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7237106.html
Copyright © 2011-2022 走看看