/*********   Supercon 2009 1級認定問題 および 予選問題 雛形プログラム   2009/06/01 */ #include #include #include #define MAXSQUARES (50000) int a[MAXSQUARES]; int b[MAXSQUARES]; int n; int d; int result = -1; void initialize(); void finalize(); /*********   解答プログラムの作成方法   以下の main() を書き換え,プログラムを作成して下さい.   結果は result に代入して下さい.   注1)main() 内の変更・削除不可の行は変えないで下さい.   注2)参考のため,以下に全データを出力するプログラムを示していま      すが,これは削除,修正してかまいません.   注3)関数定義等は適宜加えてもよいです.ただし,1つのプログラム    ファイルとして提出して下さい. */ /*********   説明:   initialize(), finalize() 呼び出しの間のみ以下が利用可能となる   a[i]: i番目の正方形の左下頂点のx座標   b[i]: i番目の正方形の左下頂点のy座標   n: 正方形の総数   d: 正方形の辺の長さ   result: 結果保存先(int 型) */ int main(int argc, char** argv){ /* 変更・修正不可 */ int i, j, x, y, xe, ye, dx, dy; char Square[100][100]; initialize(); /* 変更・修正不可 */ result = 0; for(i = 0; i < d; i++) //計算用の正方形を初期化 for(j = 0; j < d; j++) Square[i][j] = 1; for(i = 0; i < n; i++) { for(j = 0; j < i; j++) //nC2通りの比較を行う { dx = a[i] - a[j]; //i番目とj番目の正方形の左下頂点の dy = b[i] - b[j]; //x, y変位をdx, dyに保存 if(2 * d * d <= dx * dx + dy * dy) continue; //2頂点の位置関係から、明らかに重ならない場合 //上のif文で弾かれなかった場合は、必ず重なる(辺を共有する場合も含む)ので、 //以下の計算によって重なった部分に対応する計算用長方形の要素を0にしていく if(dx <= 0) { if(dy >= 0) { for(x = d - 1, xe = d + dx; xe > 0; x--, xe--) for(y = 0, ye = d - dy; ye > 0; y++, ye--) Square[x][y] = 0; } else if(dy < 0) { for(x = d - 1, xe = d + dx; xe > 0; x--, xe--) for(y = d - 1, ye = d + dy; ye > 0; y--, ye--) Square[x][y] = 0; } } else if(dx > 0) { if(dy >= 0) { for(x = 0, xe = d - dx; xe > 0; x++, xe--) for(y = 0, ye = d - dy; ye > 0; y++, ye--) Square[x][y] = 0; } else if(dy < 0) { for(x = 0, xe = d - dx; xe > 0; x++, xe--) for(y = d - 1, ye = d + dy; ye > 0; y--, ye--) Square[x][y] = 0; } } } for(x = 0; x < d; x++) { for(y = 0; y < d; y++) { if(Square[x][y]) result++; //計算用正方形の最終的に残った部分を足す else Square[x][y] = 1; //0になった部分は元に戻す } } } finalize(); /* 変更・修正不可 */ return EXIT_SUCCESS; /* 変更・修正不可 */ } /*********   以下はすべて変更・修正不可 */ #define MAXBUF (256) void initialize() { char buffer[MAXBUF]; int i; fgets(buffer, MAXBUF, stdin); if(strchr(buffer, '\n') == NULL){ fprintf(stderr, "Data reading error: please report to the Supercon09 committee this message\n"); exit(EXIT_FAILURE); } else { sscanf(buffer, "%d %d\n", &n, &d); } for(i = 0; i < n; i++) { fgets(buffer, MAXBUF, stdin); if(strchr(buffer, '\n') == NULL){ fprintf(stderr, "Data reading error: please report to the Supercon09 committee this message\n"); exit(EXIT_FAILURE); } else { sscanf(buffer, "%d %d\n", &a[i], &b[i]); } } return; } void finalize() { printf("Final Answer: %d\n", result); return; }