2014年3月3日 星期一

NTHU OJ - 7648 PD - Mine Threat

The following program is my ACcepted code for NTHU-7648 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃NTHU 7648 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )

點這裡看題目 Click here to see this Problem!

NTHU 7653 PD - Pick Up Thumbtacks 簡直是一模一樣
有興趣可以點上面連結
本題是找遠到近 ~

//This program is for NTHU 7648 PD - Mine Threat
//題目來源 Problem link: http://acm.cs.nthu.edu.tw/problem.php?pid=7648

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1001
using namespace std;

typedef struct
{
    int x;
    int y;
}point;

point person;
point thumbtack[MAX];
bool visit[MAX];
/*
find the smallest distance from thumbtack to person
return the number of thumbtack;
*/
int calDis(int n)
{
    int num = 0;
    int distance = 0, tmp;
   
    for(int i = 1; i <= n; i++)
    {
        if(visit[i])
            continue;
       
        int tmp_x = person.x - thumbtack[i].x;
        int tmp_y = person.y - thumbtack[i].y;
        tmp  =  (tmp_x * tmp_x) + (tmp_y * tmp_y);
       
        if(tmp > distance)
        {
            num = i;
            distance = tmp;
        }
    }
   
    visit[num] = true;
    return num;
}

int main()
{
    int testCase, n, number;
   
    while(scanf("%d", &testCase)!=EOF)
    {
        while(testCase)
        {
            memset(visit, false, sizeof(visit));
           
            scanf("%d %d\n", &person.x, &person.y);
            scanf("%d", &n);
           
            for(int i = 1; i <= n; i++)
                scanf("%d %d", &thumbtack[i].x, &thumbtack[i].y);
           
            for(int i = 1; i <= n; i++)
            {
                number = calDis(n);
                printf("#%d: %d %d\n", number, thumbtack[number].x, thumbtack[number].y);
            }
           
            if(testCase != 1)
                printf("\n");
            testCase--;
    }
}
    return 0;
}


Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D

歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

沒有留言:

張貼留言

請留下您的任何想法或建議!
Please leave any thought or comment!