正文

Zju 1901 A Star not a Tree?2006-08-10 21:37:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/cruxd/17475.html

分享到:

2014083 2006-08-10 20:56:11 Accepted 1901 C++ 00:00.06 516K St.Crux 这个题如果用穷举则超时:总共10000*10000个点。因此考虑从中心点出发向四周扩散搜索。这还是上个学期做的题,一直wronganswer。 #include <cstdio>#include <cmath> int xi[100], yi[100];int dir[8][2] = {{-1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 0}, {0, -1}, {1, -1}, {-1, 1}};double min;int n; void dfs(int x, int y){ int i, k; double tm = 0; for(i = 0; i < n; i ++) {  tm += sqrt((x - xi[i]) * (x - xi[i]) + (y - yi[i]) * (y - yi[i])); } if(tm < min) {  min = tm;  for(k = 0; k < 8; k ++)  {   int xx = x + dir[k][0];   int yy = y + dir[k][1];   dfs(xx, yy);  } } else  return;}  int main(){ int i; //freopen("in.txt", "r", stdin); while(scanf("%d", &n) != EOF) {  int x0 = 10000, x1 = 0, y0 = 10000, y1 = 0;  for(i = 0; i < n; i ++)  {   scanf("%d%d", &xi[i], &yi[i]);   if(xi[i] < x0) x0 = xi[i];   if(yi[i] < y0) y0 = yi[i];   if(xi[i] > x1) x1 = xi[i];   if(yi[i] > y1) y1 = yi[i];  }  int x = (x0 + x1) / 2, y = (y0 + y1) / 2;  min = 99999999;  dfs(x, y);    //printf("%0.3f ", min);  int m = int(min);    if(min - m >= 0.50)   m ++;  printf("%d\n", m); } return 0;}

阅读(3847) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册