leetcode-剑指13-OK

// language c
// 剑指13
// https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/comments/


int movingCount(int m, int n, int k){
    // 先整一个算数位和的函数
    int shuweihe(int a,int b){
        int ans = 0;
        while(a>0){
            ans += a%10;
            a /=10;
        }
        while(b>0){
            ans += b%10;
            b /=10;
        }
        return ans;
    }
    bool ok[m][n];
    for(int i = 0; i<m; i++){
        for(int j = 0; j<n; j++){
            if(shuweihe(i,j)<=k)
                ok[i][j] = true;
            else
                ok[i][j] = false;
        }
    }
    // 设好初始的,要来判断孤岛了,忘了咋判断了
    // 或许可以从0,0开始递归
    bool done[m][n];
    for(int i = 0; i<m; i++){
        for(int j = 0; j<n; j++){
            done[i][j] = false;
        }
    }
    int ans=0;
    void taowa(int a, int b){
        if(!ok[a][b])
            return;
        done[a][b] =true;
        ans++;
        if((a-1>0)&&(!done[a-1][b]))
            taowa(a-1,b);
        if((b-1>0)&&(!done[a][b-1]))
            taowa(a,b-1);
        if((a+1<m)&&(!done[a+1][b]))
            taowa(a+1,b);
        if((b+1<n)&&(!done[a][b+1]))
            taowa(a,b+1);
    }
    taowa(0,0);
    return ans;
}
posted @ 2021-01-28 06:06  RougeBW  阅读(37)  评论(0编辑  收藏  举报