首页 > 其他 > 详细

hdu4584 Building bridgesa

时间:2015-04-27 07:02:41      阅读:120      评论:0      收藏:0      [点我收藏+]

水题..不过我更水..用BFS爆搜结果爆栈..搞不懂原因...

后来分析了一下..其实暴力搜索也能过..复杂度n^5次方..好害怕..

去网上搜了一下大神的代码..确实吊..

分析一下原因: 感觉还是做题太少,没有足够的经验..毕竟平常人..拼不过智商.

技术分享
 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 using namespace std;
 6 const int N = 50;
 7 const int M = 2005;
 8 const int INF = 0x3f3f3f3f;
 9 
10 struct state {
11     int x, y;
12 }c[M], h[M];
13 
14 int n, m, cntC, cntH;
15 char s[N];
16 
17 void init () {
18     cntC = cntH = 0;
19     for (int i = 0; i < n; i++) {
20         scanf("%s", s);
21 
22         for (int j = 0; j < m; j++) {
23             if (s[j] == C) {
24                 c[cntC].x = i;
25                 c[cntC].y = j;
26                 cntC++;
27             } else if (s[j] == H) {
28                 h[cntH].x = i;
29                 h[cntH].y = j;
30                 cntH++;
31             }
32         }
33     }
34 }
35 
36  int Cal (int a, int b) {
37     return abs(h[a].x - c[b].x) + abs(h[a].y - c[b].y);
38 }
39 
40 bool cmp (const state& a, const state& b) {
41     if (a.x != b.x) return a.x < b.x;
42     return a.y < b.y;
43 }
44 
45 void solve () {
46     int ans = INF, ansC, ansH;
47 
48     sort (c, c + cntC, cmp);
49     sort (h, h + cntH, cmp);
50     //不用排序也能用..默认已经排好序了;
51     for (int i = 0; i < cntH; i++) {
52         for (int j = 0; j < cntC; j++) {
53             int d = Cal(i, j);
54             if (d < ans) {
55                 ans = d;
56                 ansH = i;
57                 ansC = j;
58             }
59         }
60     }
61     printf("%d %d %d %d\n", h[ansH].x, h[ansH].y, c[ansC].x, c[ansC].y);
62 }
63 
64 int main () {
65     while (!scanf("%d%d", &n, &m), n ,m) {
66         init ();
67         solve ();
68     }
69     return 0;
70 }
View Code

 

hdu4584 Building bridgesa

原文:http://www.cnblogs.com/xiaoniuniu/p/4458880.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!