2024年6月22日 星期六

[2013] Detect Squares

好像想的太難了!正方形有長寬相等的限制,不用像長方形考慮的那麼多(吧)

沒有人用C寫,真是太歡樂了。
寫出來很慢,沒時間了XD 快前進下一題Orz
喔喔加了一些判斷式來減少計算長寬的部份,可有效穫得加速 XD
typedef struct {
int x[1001];
int y[1001];
int matrix[1001][1001];
} DetectSquares;


DetectSquares* detectSquaresCreate() {
DetectSquares* ret = calloc (1, sizeof(DetectSquares));
return ret;
}

void detectSquaresAdd(DetectSquares* obj, int* point, int pointSize) {
int x = point[0];
int y = point[1];
obj->x[x]++;
obj->y[y]++;
obj->matrix[x][y]++;
return;
}

void printDebug(DetectSquares* obj)
{
for (int i=0; i<1001; i++)
{
if (obj->x[i] > 0 || obj->y[i] >0)
printf("i %d x, y %d %d\n", i,obj->x[i], obj->y[i]);
for (int j=0;j<1001;j++)
{
if (obj->matrix[i][j]>0)
printf(" %d %d metrix %d\n", i,j, obj->matrix[i][j]);
}
}
}
int count(DetectSquares* obj, int x, int y, int x1, int y1) {
//printf("count %d %d %d %d\n",x,y,x1,y1);
if (x==x1 && y==y1)
return 0;
int a = obj->matrix[x1][y1];
int b = obj->matrix[x][y1];
int c = obj->matrix[x1][y];
//printf("ret %d %d %d\n", a,b,c);
return a*b*c;
}

int detectSquaresCount(DetectSquares* obj, int* point, int pointSize) {
//printDebug(obj);
//printf("New count\n");
int x = point[0];
int y = point[1];
int ret = 0;
if (obj->x[x] ==0 || obj->y[y] == 0)
return ret;
for (int i=0; i<1001; i++)
{
if (obj->y[i] >0)
{
int delta = abs(y-i);
for (int j=0;j<1001; j++)
{
if (j-x > delta)
break;
if (obj->x[j]>0 && (abs(x-j) == delta))
ret += count(obj, x,y , j,i);
}
}
}
return ret;
}

void detectSquaresFree(DetectSquares* obj) {
free(obj);
return;
}

/**
* Your DetectSquares struct will be instantiated and called as such:
* DetectSquares* obj = detectSquaresCreate();
* detectSquaresAdd(obj, point, pointSize);
* int param_2 = detectSquaresCount(obj, point, pointSize);
* detectSquaresFree(obj);
*/

沒有留言:

張貼留言