[lintcode]909. Android Unlock Patterns

链接:http://www.lintcode.com/zh-cn/problem/android-unlock-patterns/


Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys.

Rules for a valid pattern:
1. Each pattern must connect at least m keys and at most n keys.
2. All the keys must be distinct.
3. If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed.
4. The order of keys used matters.
android unlock
Explanation:

| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |

Invalid move: 4 - 1 - 3 - 6
Line 1 - 3 passes through key 2 which had not been selected in the pattern.

Invalid move: 4 - 1 - 9 - 2
Line 1 - 9 passes through key 5 which had not been selected in the pattern.

Valid move: 2 - 4 - 1 - 3 - 6
Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern

Valid move: 6 - 5 - 4 - 1 - 9 - 2
Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.

样例

Given m = 1, n = 1, return 9.

这道题乍一看题目这么长以为是一个设计题,其实不是,这道题还是比较有意思的,起码跟实际结合的比较紧密。这道题说的是安卓机子的解锁方法,有9个数字键,如果密码的长度范围在[m, n]之间,问所有的解锁模式共有多少种,注意题目中给出的一些非法的滑动模式。那么我们先来看一下哪些是非法的,首先1不能直接到3,必须经过2,同理的有4到6,7到9,1到7,2到8,3到9,还有就是对角线必须经过5,例如1到9,3到7等。我们建立一个二维数组jumps,用来记录两个数字键之间是否有中间键,然后再用一个一位数组visited来记录某个键是否被访问过,然后我们用递归来解,我们先对1调用递归函数,在递归函数中,我们遍历1到9每个数字next,然后找他们之间是否有jump数字,如果next没被访问过,并且jump为0,或者jump被访问过,我们对next调用递归函数。数字1的模式个数算出来后,由于1,3,7,9是对称的,所以我们乘4即可,然后再对数字2调用递归函数,2,4,6,9也是对称的,再乘4,最后单独对5调用一次,然后把所有的加起来就是最终结果了。

class Solution {
public:
    int numberOfPatterns(int m, int n) {
        int res = 0;
        vector<bool> visited(10, false);
        vector<vector<int>> jumps(10, vector<int>(10, 0));
        jumps[1][3] = jumps[3][1] = 2;
        jumps[4][6] = jumps[6][4] = 5;
        jumps[7][9] = jumps[9][7] = 8;
        jumps[1][7] = jumps[7][1] = 4;
        jumps[2][8] = jumps[8][2] = 5;
        jumps[3][9] = jumps[9][3] = 6;
        jumps[1][9] = jumps[9][1] = jumps[3][7] = jumps[7][3] = 5;
        res += helper(1, 1, 0, m, n, jumps, visited) * 4;
        res += helper(2, 1, 0, m, n, jumps, visited) * 4;
        res += helper(5, 1, 0, m, n, jumps, visited);
        return res;
    }
    int helper(int num, int len, int res, int m, int n, vector<vector<int>> &jumps, vector<bool> &visited) {
        if (len >= m) ++res;
        ++len;
        if (len > n) return res;
        visited[num] = true;
        for (int next = 1; next <= 9; ++next) {
            int jump = jumps[num][next];
            if (!visited[next] && (jump == 0 || visited[jump])) {
                res = helper(next, len, res, m, n, jumps, visited);
            }
        }
        visited[num] = false;
        return res;
    }
}; 





manning.microservices.patterns是指微服务架构模式方面的内容。微服务是一种架构风格,它将一个应用程序拆分为一组小的、独立的服务,每个服务都可以独立部署、扩展和维护。manning.microservices.patterns通过提供一些常见的模式,帮助开发人员构建可靠、可伸缩、可维护的微服务架构。 首先,manning.microservices.patterns介绍了服务拆分的模式。它提供了一些指导原则,帮助开发人员确定应该将哪些功能拆分为独立的服务,以及如何定义服务边界。这样可以确保每个服务都专注于一个明确的业务领域,并且可以独立地开发和部署。 其次,manning.microservices.patterns讨论了服务间通信的模式。微服务架构中,各个服务需要相互通信来实现业务流程。这本书介绍了一些常见的通信模式,例如同步调用、异步消息传递和事件驱动架构。开发人员可以根据实际需求选择适合的通信方式。 此外,manning.microservices.patterns还介绍了服务发现和负载均衡的模式。由于微服务架构中的服务数量庞大,需要有一种方法来发现和跟踪可用的服务实例。这本书提供了一些模式,如服务注册与发现、负载均衡和断路器模式,帮助开发人员实现可靠的服务发现和负载均衡机制。 最后,manning.microservices.patterns还探讨了监控和故障处理的模式。微服务架构中,每个服务都可以独立部署和维护,因此需要一种方法来监控服务的健康状况,并及时处理故障。这本书介绍了一些常见的监控和故障处理模式,例如服务指标监控、日志聚合和异常处理模式。 总之,manning.microservices.patterns是一本关于微服务架构模式的书籍,它提供了一些常见的模式,帮助开发人员构建可靠、可伸缩、可维护的微服务架构。这些模式涵盖了服务拆分、服务间通信、服务发现和负载均衡、监控和故障处理等方面,可以帮助开发人员在构建微服务架构时更加高效地解决各种技术挑战。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值