WY64. 整理房间
描述
输入描述
第一行一个数n(1 <= n <= 100),表示杂物的团数。输出描述
n行,每行1个数,表示最少移动次数。示例1
输入:
4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0
输出:
1 -1 3 3
说明:
对于第一团杂物,我们可以旋转第二个或者第三个物品1次。C++14 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-05-20
#include <stdio.h> #include <math.h> typedef struct node{ int x,y; }node; bool judge(int a1 ,int b1 ,int a2 ,int b2,int a3,int b3,int a4,int b4){ int dis[6]; dis[0] = (a1-a2)*(a1-a2)+(b1-b2)*(b1-b2); dis[1] = (a1-a3)*(a1-a3)+(b1-b3)*(b1-b3); dis[2] = (a1-a4)*(a1-a4)+(b1-b4)*(b1-b4); dis[3] = (a2-a3)*(a2-a3)+(b2-b3)*(b2-b3); dis[4] = (a2-a4)*(a2-a4)+(b2-b4)*(b2-b4); dis[5] = (a3-a4)*(a3-a4)+(b3-b4)*(b3-b4); int a = dis[0],b = -1; int cnt1 = 1,cnt2 = 0; for(int i=1;i < 6;++i){ if(dis[i] == a){ ++cnt1; }else if(dis[i] != a && b == -1){ b = dis[i]; ++cnt2; }else if(dis[i] == b){ ++cnt2; }else if(dis[i] != a && dis[i] != b){ return false; } } if((cnt1 == 4 && cnt2 == 2 && a != 0 && b != 0) || (cnt1 == 2 && cnt2 == 4 && a != 0 && b != 0)){ return true; }else{ return false; } } int main(){ int n; while(scanf("%d",&n) != EOF){ int ai[4],bi[4],ci[4],di[4]; while(n--){ for(int i=0;i < 4;++i){ scanf("%d %d %d %d",&ai[i],&bi[i],&ci[i],&di[i]); } int a1[4] = {ai[0]},b1[4] = {bi[0]}; int a2[4] = {ai[1]},b2[4] = {bi[1]}; int a3[4] = {ai[2]},b3[4] = {bi[2]}; int a4[4] = {ai[3]},b4[4] = {bi[3]}; for(int i=1;i < 4;++i){ a1[i] = -b1[i-1] + ci[0] + di[0]; b1[i] = a1[i-1] - ci[0] + di[0]; } for(int i=1;i < 4;++i){ a2[i] = -b2[i-1] + ci[1] + di[1]; b2[i] = a2[i-1] - ci[1] + di[1]; } for(int i=1;i < 4;++i){ a3[i] = -b3[i-1] + ci[2] + di[2]; b3[i] = a3[i-1] - ci[2] + di[2]; } for(int i=1;i < 4;++i){ a4[i] = -b4[i-1] + ci[3] + di[3]; b4[i] = a4[i-1] - ci[3] + di[3]; } int min = -1 , cnt = 0,cnt1 = 0,cnt2 = 0,cnt3 = 0,cnt4 = 0; for(int i1=0;i1 < 4;++i1,++cnt1){ cnt2 = 0; cnt3 = 0; cnt4 = 0; for(int i2 = 0;i2 < 4;++i2, ++cnt2){ cnt3 = 0; cnt4 = 0; for(int i3=0;i3 < 4;++i3,++cnt3){ cnt4 = 0; for(int i4 = 0;i4 < 4;++i4){ //printf("%d %d %d %d %d %d %d %d\n",a1[i1],b1[i1],a2[i2],b2[i2],a3[i3],b3[i3],a4[i4],b4[i4]); if(!judge(a1[i1],b1[i1],a2[i2],b2[i2],a3[i3],b3[i3],a4[i4],b4[i4])){ ++cnt4; }else{ cnt = cnt1 + cnt2 + cnt3 + cnt4; if(min > cnt || min == -1){ min = cnt; } } } } } } printf("%d\n",min); } } return 0; }
C++14 解法, 执行用时: 3ms, 内存消耗: 304KB, 提交时间: 2019-12-06
#include <stdio.h> #include <math.h> typedef struct node{ int x,y; }node; bool judge(int a1 ,int b1 ,int a2 ,int b2,int a3,int b3,int a4,int b4){ int dis[6]; dis[0] = (a1-a2)*(a1-a2)+(b1-b2)*(b1-b2); dis[1] = (a1-a3)*(a1-a3)+(b1-b3)*(b1-b3); dis[2] = (a1-a4)*(a1-a4)+(b1-b4)*(b1-b4); dis[3] = (a2-a3)*(a2-a3)+(b2-b3)*(b2-b3); dis[4] = (a2-a4)*(a2-a4)+(b2-b4)*(b2-b4); dis[5] = (a3-a4)*(a3-a4)+(b3-b4)*(b3-b4); int a = dis[0],b = -1; int cnt1 = 1,cnt2 = 0; for(int i=1;i < 6;++i){ if(dis[i] == a){ ++cnt1; }else if(dis[i] != a && b == -1){ b = dis[i]; ++cnt2; }else if(dis[i] == b){ ++cnt2; }else if(dis[i] != a && dis[i] != b){ return false; } } if((cnt1 == 4 && cnt2 == 2 && a != 0 && b != 0) || (cnt1 == 2 && cnt2 == 4 && a != 0 && b != 0)){ return true; }else{ return false; } } int main(){ int n; while(scanf("%d",&n) != EOF){ int ai[4],bi[4],ci[4],di[4]; while(n--){ for(int i=0;i < 4;++i){ scanf("%d %d %d %d",&ai[i],&bi[i],&ci[i],&di[i]); } int a1[4] = {ai[0]},b1[4] = {bi[0]}; int a2[4] = {ai[1]},b2[4] = {bi[1]}; int a3[4] = {ai[2]},b3[4] = {bi[2]}; int a4[4] = {ai[3]},b4[4] = {bi[3]}; for(int i=1;i < 4;++i){ a1[i] = -b1[i-1] + ci[0] + di[0]; b1[i] = a1[i-1] - ci[0] + di[0]; } for(int i=1;i < 4;++i){ a2[i] = -b2[i-1] + ci[1] + di[1]; b2[i] = a2[i-1] - ci[1] + di[1]; } for(int i=1;i < 4;++i){ a3[i] = -b3[i-1] + ci[2] + di[2]; b3[i] = a3[i-1] - ci[2] + di[2]; } for(int i=1;i < 4;++i){ a4[i] = -b4[i-1] + ci[3] + di[3]; b4[i] = a4[i-1] - ci[3] + di[3]; } int min = -1 , cnt = 0,cnt1 = 0,cnt2 = 0,cnt3 = 0,cnt4 = 0; for(int i1=0;i1 < 4;++i1,++cnt1){ cnt2 = 0; cnt3 = 0; cnt4 = 0; for(int i2 = 0;i2 < 4;++i2, ++cnt2){ cnt3 = 0; cnt4 = 0; for(int i3=0;i3 < 4;++i3,++cnt3){ cnt4 = 0; for(int i4 = 0;i4 < 4;++i4){ //printf("%d %d %d %d %d %d %d %d\n",a1[i1],b1[i1],a2[i2],b2[i2],a3[i3],b3[i3],a4[i4],b4[i4]); if(!judge(a1[i1],b1[i1],a2[i2],b2[i2],a3[i3],b3[i3],a4[i4],b4[i4])){ ++cnt4; }else{ cnt = cnt1 + cnt2 + cnt3 + cnt4; if(min > cnt || min == -1){ min = cnt; } } } } } } printf("%d\n",min); } } return 0; }