当前位置:网站首页>1222. 可以攻击国王的皇后-力扣双百代码
1222. 可以攻击国王的皇后-力扣双百代码
2022-07-30 14:20:00 【Mr Gao】
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;
}
边栏推荐
- PyQt5快速开发与实战 9.1 使用PyInstaller打包项目生成exe文件
- Still saying software testing doesn't have a midlife crisis?9 years of test engineers were eliminated
- 六面蚂蚁金服,抗住面试官的狂轰乱炸,前来面试复盘
- 机房布线的至高境界,美到窒息
- 深入浅出零钱兑换问题——背包问题的套壳
- Androd 跳转到google应用市场
- The website adds a live 2d kanban girl that can dress up and interact
- 有关收集箱的改进建议
- 网络安全——lcx的使用
- 00后测试员摸爬滚打近一年,为是否要转行或去学软件测试的学弟们总结出了以下走心建议
猜你喜欢

【回归预测-CNN预测】基于卷积神经网络CNN实现数据回归预测附matlab代码

Flask Framework - Flask-Mail Mail

Why did I switch from developer to testing, 3 years software testing engineer, tell you the secret of this

自动化办公|办公软件和亿图脑图MindMaster快捷键

开始学习C语言了

Smart Contract Security - Private Data Access

CVE-2022-33891 Apache Spark 命令注入复现

71页全域旅游综合整体解决方案2021 ppt

超T动力 盈运天下——中国重汽黄河/豪沃WP14T产品首发荣耀上市!

吃透Chisel语言.29.Chisel进阶之通信状态机(一)——通信状态机:以闪光灯为例
随机推荐
sql中ddl和dml(sql与access的区别)
[深入研究4G/5G/6G专题-46]: 5G Link Adaption链路自适应-2-常见缩略语
Baijiahao cancels the function of posting documents on the interface: the weight of the plug-in chain is blocked
SLF4J的使用
Redis6.0 source code learning (5) ziplist
元宇宙邮局AI航天主题系列数字藏品 将于7月30日10:00点上线“元邮数藏”
Could not acquire management access for administration
Digital signal processing course lab report (what foundation is needed for digital signal processing)
DDS Arbitrary Waveform Output Based on FPGA
EasyV数字孪生流域|宁波智慧水利整体智治综合应用
【Vue.js 3.0源码】KeepAlive 组件:如何让组件在内存中缓存和调度?
Container sorting case
PyQt5快速开发与实战 9.1 使用PyInstaller打包项目生成exe文件
canal抓取数据
记面试外包公司的一次经历,到底该不该去?
跳槽前,把自己弄成卷王
去腾讯面试,直接让人出门左拐 :幂等性都不知道!
六面蚂蚁金服,抗住面试官的狂轰乱炸,前来面试复盘
LoRaWAN网关源码分析(V1.0.2)
吃透Chisel语言.29.Chisel进阶之通信状态机(一)——通信状态机:以闪光灯为例