首页 > 其他分享 >POJ--3050 Hopscotch(暴搜)

POJ--3050 Hopscotch(暴搜)

时间:2023-04-16 21:44:06浏览次数:44  
标签:digit set Hopscotch -- int POJ hop grid total

记录
21:36 2023-4-16

http://poj.org/problem?id=3050

reference:《挑战程序设计竞赛(第2版)》第二章练习题索引 p135

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.

暴搜,直接dfs遍历所有可能的结果,用stl中的set记录下产生的set就可以(没有减枝啥的就能过了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<set>
#define MAX_N 5
using namespace std;
typedef long long ll;
typedef unsigned int uint;
const int INF = 0x3f3f3f3f;
int arr[MAX_N + 1][MAX_N + 1];
int num_count = 0;
int total = 0;
//4个方向移动的向量
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
set<int> s;

void dfs(int x, int y) {
    if(num_count == 5) {
        s.insert(total * 10 + arr[x][y]);
        return ;
    }
    num_count += 1;
    total = total * 10 + arr[x][y];
    for(int k = 0; k < 4; k++) {
        int new_x = x + dx[k], new_y = y + dy[k];
        if(0 <= new_x && new_x < MAX_N && 0 <= new_y && new_y < MAX_N) {
            dfs(new_x, new_y);
        }
    }
    num_count -= 1;
    total = (total - arr[x][y]) / 10;
}

void solve() {
    for(int i = 0; i < MAX_N; i++) {
        for(int j = 0 ; j < MAX_N; j++) {
            num_count = 0;
            total = 0;
            dfs(i, j);
        }
    }
    printf("%d\n", s.size());
}

int main() {
    for(int i = 0; i < MAX_N; i++) {
        for(int j = 0 ; j < MAX_N; j++) {
            scanf("%d", &arr[i][j]);
        }
    }
    solve();
}

标签:digit,set,Hopscotch,--,int,POJ,hop,grid,total
From: https://www.cnblogs.com/57one/p/17324179.html

相关文章

  • C++20 Advent of Code 可见树 Day 8: Treetop Tree House
    C++20AdventofCode可见树Day8:TreetopTreeHouseDay8-AdventofCode2022#include<iostream>#include<vector>#include<fstream>#include<string>#include<ranges>#include<numeric>#include<algorithm>#in......
  • C++ 20 协程总结
    C++20协程总结介绍C++20提供的是非对称的、一等对象、无栈的协程(CoroutinesinC++20areasymmetric,first-class,andstackless)所谓协程,即用户级线程,一种用于将异步代码同步化的编程机制,使得程序的执行流可以在多个并行事务之间切换但又不必承担切换带来的过高的性能损耗。......
  • ES的Java API 操作(五)
    我看到希望,哪怕只有微小的一束光,我也会拼尽全力去寻找.上一章简单介绍了ES聚合查询(四),如果没有看过,请观看上一章我们之前都是使用Postman请求来操作索引,操作文档,查询数据的,这一章节,老蝴蝶使用JavaApi进行处理.一.简单的JavaAPIES环境搭建一.一添加pom.xml依赖......
  • layer的嵌套打开弹出层
    当打开了一个layer.open()之后,如果在open的页面上面还有一个layer.open()去再次打开一个弹出层,这时候第二个打开的弹出层是在最早打开的基础上,然后镶嵌在里面的。如果第一个弹出层很大,而第二个弹出层比较小,可能不会太影响用户体验;但是如果第一个弹出层很小,而第二个弹出层却很大,这......
  • SpringMvc CRUD
    1.前期准备1.1.配置欢迎页在webapp下添加/home/index.html,再在WEB-INF目录下创建index.jsp(真正的欢迎页面)在web.xml中配置<welcome>标签,并加入/home/index.html<welcome-file-list><welcome-file>/home/index.html</welcome-file></welcome-file-list>通过Controller......
  • python中的魔术方法
    魔法方法(MagicMethod)是python内置方法,格式为:“__方法名__”,不需要主动调用,存在的目的是为了给python的解释器进行调用,几乎每个魔法方法都有一个对应的内置函数,或者运算符,当我们对这个对象使用这些函数或者运算符时就会调用类中的对应魔法方法,可以理解为重写这些python的内置函......
  • 学习JavaScript 一
    文件引用在一个单独的js文件中也可以编写JavaScript代码,然后在HTML文件中使用script标签进行引用,以下是一个简单演示。   遍历对象枚举遍历对象中的属性,可以使用for…in语句循环,对象中有几个属性,循环体就会执行几次。语法格式:for(var变量in对象){}案例演示:......
  • 嵌入式软件架构设计协议定义
    在嵌入式软件架构设计中,协议定义是非常重要的。协议定义规定了通信双方之间的消息格式以及通信方式,保证了系统之间的可靠性、安全性和互操作性。以下是一些常见的嵌入式软件架构设计协议定义:UART协议:UART是一种简单的串行通信协议,适用于低速、短距离的通信。UART不需要外部时钟信号......
  • TCP三次握手和四次挥手
    文章目录TCP三次握手TCP四次挥手TCP三次握手序列号:建立连接时计算机随机生成的随机数作为初始值,通过SYN包传给接收端主机,每发送一次数据就累加一次该数据字节数的大小。用来解决网络包乱序问题。确认应答号:指下一次期望收到的数据的序列号,发送端收到这个确认应答以后认为在这个序......
  • HDLBits(1)——Modules:Hierarchy
    HDLBits——Modules:Hierarchy目录HDLBits——Modules:Hierarchy问题19Module将信息连接到端口BypositionByname问题20Connectingportsbyposition(Modulepos)问题21Connectingportsbyname(Modulename)问题22Threemodules(Moduleshift)问题23Modulesandvectors(Mod......