hdu4584 Building bridgesa

水题..不过我更水..用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

 

转载于:https://www.cnblogs.com/xiaoniuniu/p/4458880.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值