{"id":2940,"date":"2020-02-15T10:59:32","date_gmt":"2020-02-15T02:59:32","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2940"},"modified":"2020-02-15T10:59:32","modified_gmt":"2020-02-15T02:59:32","slug":"%e3%80%90leetcode%e3%80%91%e5%89%91%e6%8c%87offer-13-%e6%9c%ba%e5%99%a8%e4%ba%ba%e7%9a%84%e8%bf%90%e5%8a%a8%e8%8c%83%e5%9b%b4","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2940\/","title":{"rendered":"\u3010LeetCode\u3011\u5251\u6307Offer 13 \u673a\u5668\u4eba\u7684\u8fd0\u52a8\u8303\u56f4"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/ji-qi-ren-de-yun-dong-fan-wei-lcof\/\">https:\/\/leetcode-cn.com\/problems\/ji-qi-ren-de-yun-dong-fan-wei-lcof\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution:\n    def movingCount(self, m: int, n: int, k: int) -> int:\n        def judge(x, y):\n            str_x = str(x)\n            str_y = str(y)\n            sum = 0\n            for each in str_x:\n                sum += int(each)\n            for each in str_y:\n                sum += int(each)\n            if sum &lt;= k:\n                return True\n            return False\n        def dfs(x, y, res):\n            for ii, jj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:\n                if 0 &lt;= x + ii &lt; m and 0 &lt;= y + jj &lt; n and init_map[x + ii][y + jj] == 0 and judge(x + ii, y + jj):\n                    init_map[x + ii][y + jj] = 1\n                    res = dfs(x + ii, y + jj, res + 1)\n            return res\n        init_map = [[0 for _ in range(n)] for _ in range(m)]\n        init_map[0][0] = 1\n        res_output = dfs(0, 0, 1)\n        return res_output\nif __name__ == '__main__':\n    m = 2\n    n = 3\n    k = 1\n    print(Solution().movingCount(m, n, k))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u6df1\u641c\uff0c\u56db\u4e2a\u65b9\u5411\u641c\u7d22\uff0c\u53ea\u4e0d\u8fc7\u4e4b\u524d\u7684\u6df1\u641c\u5224\u65ad\u903b\u8f91\u662f\u80fd\u4e0d\u80fd\u8fbe\u5230\u554a\u4ec0\u4e48\u7684\uff0c\u8fd9\u6b21\u7684\u903b\u8f91\u662f\u68c0\u67e5\u5750\u6807\u5404\u4f4d\u548c\u8d85\u4e0d\u8d85\u8fc7k\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/ji-qi-ren-de-yun- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":2045,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2940"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2940"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2940\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2940"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}