当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
localhost与127.0.0.1
Cookie simulation login "recommended collection"
MaxWell抓取数据
记面试外包公司的一次经历,到底该不该去?
Understand Chisel language. 28. Chisel advanced finite state machine (2) - Mealy state machine and comparison with Moore state machine
Recommended open source tools: MegPeak, a high-performance computing tool
The main content of terrain analysis (the special effect level of the wandering earth)
What is the relationship between the construction of smart cities and 5G technology in the new era
Web消息推送之SSE
业内人士真心话:只会测试没有前途的,我慌了......
Remember an experience of interviewing an outsourcing company, should you go?
开始学习C语言了
基于5G的仓储信息化解决方案2022
[机缘参悟-53]:《素书》-3-修身养志[求人之志章第三]
The highest level of wiring in the computer room, the beauty is suffocating
71页全域旅游综合整体解决方案2021 ppt
双碳目标下:农田温室气体排放模拟
postgresql的普通字符串和转义字符串
Application of time series database in the field of ship risk management
查阅所连接过的WiFi所有信息(含密码)(访问历史所有WiFi连接)








