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 }

时间: 2024-10-20 10:18:24

hdu4584 Building bridgesa的相关文章

(转载)解决AndroidStudio导入项目在 Building gradle project info 一直卡住

源地址http://blog.csdn.net/yyh352091626/article/details/51490976 Android Studio导入项目的时候,一直卡在Building gradle project info这一步,主要原因还是因为被墙的结果.gradle官网虽然可以访问,但是速度连蜗牛都赶不上... 解决办法主要有两种,一是直接下载gradle离线包,二是修改项目的gradle-wrapper.properties里的gradle版本为自己电脑已有的版本. 离线包下载导

ZOJ 3820 Building Fire Stations

Building Fire Stations Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on ZJU. Original ID: 382064-bit integer IO format: %lld      Java class name: Main Special Judge Marjar University is a beautiful and peaceful place. There a

hdu 5033 Building(斜率优化)

Building Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1237    Accepted Submission(s): 350 Special Judge Problem Description Once upon a time Matt went to a small town. The town was so sma

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

Building Block HDU - 2818 (并查集)

Building Block HDU - 2818 题意:搬砖...每一次可以把a所在的那一堆放到b所在的那一堆上面,问第x号砖下面有几块砖. 记录一下到根节点的距离(dw),以及根节点上方有几块砖(up). 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=30010; 5 int f[maxn],up[maxn],dw[maxn]; 6 7 int gf(int x){ 8 if(x==f[x]){ 9

JVM building

http://hg.openjdk.java.net/jdk7/jdk7/raw-file/tip/README-builds.html#ant http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html Building OpenJDK 9 on Ubuntu http://www.tuicool.com/articles/v2aaEv 图文解析在Linux下搭建Hotspot JVM源码调试环境 | How to

Building Maintainable Software-java篇之Keep Architecture Components Balanced

Building encapsulation boundaries is a crucial skill in software architecture. -George H. Fairbanks in Just Enough Architecture Guideline: ? Balance the number and relative size of top-level components in your code. ? Do this by organizing source cod

2015ACM/ICPC亚洲区长春站 L hdu 5538 House Building

House Building Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 145    Accepted Submission(s): 123 Problem Description Have you ever played the video game Minecraft? This game has been one of t

ACM学习历程—BNUOJ3685 Building for UN(构造)

The United Nations has decided to build a new headquarters in Saint Petersburg, Russia. It will have a form of a rectangular parallelepiped and will consist of several rectangular floors, one on top of another. Each floor is a rectangular grid of the