• 1222. 可以攻击国王的皇后-力扣双百代码


    1222. 可以攻击国王的皇后-力扣双百代码

    在一个 8x8 的棋盘上,放置着若干「黑皇后」和一个「白国王」。

    给定一个由整数坐标组成的数组 queens ,表示黑皇后的位置;以及一对坐标 king ,表示白国王的位置,返回所有可以攻击国王的皇后的坐标(任意顺序)。

    示例 1:
    在这里插入图片描述

    输入:queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king = [0,0]
    输出:[[0,1],[1,0],[3,3]]
    解释:
    [0,1] 的皇后可以攻击到国王,因为他们在同一行上。
    [1,0] 的皇后可以攻击到国王,因为他们在同一列上。
    [3,3] 的皇后可以攻击到国王,因为他们在同一条对角线上。
    [0,4] 的皇后无法攻击到国王,因为她被位于 [0,1] 的皇后挡住了。
    [4,0] 的皇后无法攻击到国王,因为她被位于 [1,0] 的皇后挡住了。
    [2,4] 的皇后无法攻击到国王,因为她和国王不在同一行/列/对角线上。

    示例 2:
    在这里插入图片描述

    输入:queens = [[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[4,5]], king = [3,3]
    输出:[[2,2],[3,4],[4,4]]

    示例 3:
    在这里插入图片描述

    输入:queens = [[5,6],[7,7],[2,1],[0,7],[1,6],[5,1],[3,7],[0,3],[4,0],[1,2],[6,3],[5,0],[0,4],[2,2],[1,1],[6,4],[5,4],[0,0],[2,6],[4,5],[5,2],[1,4],[7,5],[2,3],[0,5],[4,2],[1,0],[2,7],[0,1],[4,6],[6,1],[0,6],[4,3],[1,7]], king = [3,4]
    输出:[[2,3],[1,4],[1,6],[3,7],[4,3],[5,4],[4,5]]

    解题代码如下:

    /**
     * Return an array of arrays of size *returnSize.
     * The sizes of the arrays are returned as *returnColumnSizes array.
     * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
     */
    int** queensAttacktheKing(int** queens, int queensSize, int* queensColSize, int* king, int kingSize, int* returnSize, int** returnColumnSizes){
        int d[8];
        int r[8][2];
        int rz[8];
        int q;
        int rol=king[0],col=king[1];
        *returnColumnSizes=(int *)malloc(sizeof(int)*12);
        int i;
        for(i=0;i<8;i++){
            d[i]=10000;
            rz[i]=0;
        }
       // printf("--%d %d",rol,col);
        for(i=0;i<queensSize;i++){
            int x=queens[i][0],y=queens[i][1];
            q=0;
           // printf("%d %d ",x,y);
            if(x==rol&&y<col){
                if(abs(y-col)<d[q]){
                    d[q]=abs(y-col);
                    rz[q]=1;
                    r[q][0]=x;
                    r[q][1]=y;
    
                }
            }
            q++;
            if(x==rol&&y>col){
             //   printf("fa asfd%d dpp %d ",y,d[q]);
                if(abs(y-col)<d[q]){
                     d[q]=abs(y-col);
                    rz[q]=1;
                    r[q][0]=x;
                    r[q][1]=y;
    
                }
            }
             q++;
            if(x>rol&&y==col){
            //    printf("-- %d %d %d",x,y,abs(x-rol));
             //   printf("| %d ",d[q]);
                if(abs(x-rol)<d[q]){
                     d[q]=abs(x-rol);
                    rz[q]=1;
                    r[q][0]=x;
                    r[q][1]=y;
    
                }
            }
             q++;
            if(x<rol&&y==col){
                if(abs(x-rol)<d[q]){
                     d[q]=abs(x-rol);
                    rz[q]=1;
                    r[q][0]=x;
                    r[q][1]=y;
    
                }
            }
             q++;
            if(x<rol&&y<col&&abs(x-rol)==abs(y-col)){
                if(abs(y-col)<d[q]){
                     d[q]=abs(y-col);
                    rz[q]=1;
                    r[q][0]=x;
                    r[q][1]=y;
    
                }
            }
             q++;
            if(x>rol&&y<col&&abs(x-rol)==abs(y-col)){
                if(abs(y-col)<d[q]){
                     d[q]=abs(y-col);
                    rz[q]=1;
                    r[q][0]=x;
                    r[q][1]=y;
    
                }
            }
            q++;
             if(x>rol&&y>col&&abs(x-rol)==abs(y-col)){
                if(abs(y-col)<d[q]){
                     d[q]=abs(y-col);
                    rz[q]=1;
                    r[q][0]=x;
                    r[q][1]=y;
    
                }
            }
               q++;
            if(x<rol&&y>col&&abs(x-rol)==abs(y-col)){
                if(abs(y-col)<d[q]){
                     d[q]=abs(y-col);
                    rz[q]=1;
                    r[q][0]=x;
                    r[q][1]=y;
    
                }
            }
           
    
    
    
    
        }
      
       int size=0;
            for(i=0;i<8;i++){
                if(rz[i]==1){
              queens[size][0]=r[i][0];
              queens[size][1]=r[i][1];
              (*returnColumnSizes)[size]=2;
            size++;
       
                }
            }
    
     
      // printf("%d ",size);
    *returnSize=size;
     return queens;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
  • 相关阅读:
    角色认知的理解
    报错-mmdet/cuda编译报错: fatal error: THC/THC.h: No such file or directory
    数字重塑客观世界,全空间GIS发展正当其时
    Redis面试题
    idea 如何在命令行快速打开项目
    看完抱你学会Exchanger
    谷歌计划从Chrome119起测试IP隐私保护功能
    百度搜索逐步恢复优质网站权限
    Python学习笔记(2)
    分布式任务调度框架XXL-JOB
  • 原文地址:https://blog.csdn.net/weixin_43327597/article/details/126067557