Building bridges

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 301    Accepted Submission(s): 189

Problem Description
Hululu and Cululu are two pacific ocean countries made up of many islands. These two country has so many years of friendship so they decide to build bridges to connect their islands. Now they want to build the first bridge to connect an island of Hululu and an island of Culuu .

Their world can be considered as a matrix made up of three letters 'H','C' and 'O'.Every 'H' stands for an island of Hululu, every 'C' stands for an island of Cululu, and 'O' stands for the ocean. Here is a example:

The coordinate of the up-left corner is (0,0), and the down-right corner is (4, 3). The x-direction is horizontal, and the y-direction is vertical.

There may be too many options of selecting two islands. To simplify the problem , they come up with some rules below:

1. The distance between the two islands must be as short as possible. If the two islands' coordinates are (x1,y1) and (x2,y2), the distance is |x1-x2|+|y1-y2|.

2. If there are more than one pair of islands satisfied the rule 1, choose the pair in which the Hululu island has the smallest x coordinate. If there are still more than one options, choose the Hululu island which has the smallest y coordinate.

3.After the Hululu island is decided, if there are multiple options for the Cululu island, also make the choice by rule 2. 

Please help their people to build the bridge.

 
Input
There are multiple test cases.

In each test case, the first line contains two integers M and N, meaning that the matrix is M×N ( 2<=M,N <= 40).

Next M lines stand for the matrix. Each line has N letters.

The input ends with M = 0 and N = 0.

It's guaranteed that there is a solution.

 
Output
For each test case, choose two islands, then print their coordinates in a line in following format:

x1 y1 x2 y2

x1,y1 is the coordinate of Hululu island, x2 ,y2 is the coordinate of Cululu island.

 
Sample Input
4 4
CHCH
HCHC
CCCO
COHO
5 4
OHCH
HCHC
CCCO
COHO
HCHC
0 0
 
Sample Output
0 1 0 0
0 1 0 2
 
Source
 
import java.io.BufferedReader;
import java.io.InputStreamReader; public class Main{
public static void main(String[] args) {
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
try {
while(true){
String s[]=bf.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int m=Integer.parseInt(s[1]);
if(m==0&&n==0)
break;
char a[][]=new char[n][m];
int x1=0,y1=0,x2=0,y2=0;
Point fh[]=new Point[n*m+1];
Point fc[]=new Point[n*m+1];
int x=0,y=0;
for(int i=0;i<n;i++){
String str=bf.readLine();
for(int j=0;j<m;j++){
a[i][j]=str.charAt(j);
if(a[i][j]=='H'){
fh[x++]=new Point(i,j);
}
else if(a[i][j]=='C'){
fc[y++]=new Point(i,j);
}
}
}
int max=Integer.MAX_VALUE;
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
int dd=DD(fh[i],fc[j]);
if(dd<max){
x1=fh[i].x;
y1=fh[i].y;
x2=fc[j].x;
y2=fc[j].y;
max=dd;
}
}
}
System.out.println(x1+" "+y1+" "+x2+" "+y2);
}
} catch (Exception e) {
e.printStackTrace();
}
} private static int DD(Point a, Point b) {
return Math.abs(a.x-b.x)+Math.abs(a.y-b.y);
}
}
class Point{
int x,y; public Point(int x, int y) {
this.x = x;
this.y = y;
}
}


Building bridges_hdu_4584(排序).java的更多相关文章

  1. 希尔排序及希尔排序java代码

    原文链接:http://www.orlion.ga/193/ 由上图可看到希尔排序先约定一个间隔(图中是4),然后对0.4.8这个三个位置的数据进行插入排序,然后向右移一位对位置1.5.9进行插入排序 ...

  2. 算法练习5---快速排序Java版

    基本思想:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成 ...

  3. ElasticSearch中设置排序Java

    有用的链接:http://stackoverflow.com/questions/12215380/sorting-on-several-fields-in-elasticsearch 有的时候,需要 ...

  4. 初识指令重排序,Java 中的锁

    本文是作者原创,版权归作者所有.若要转载,请注明出处.本文只贴我觉得比较重要的源码 指令重排序 Java语言规范JVM线程内部维持顺序化语义,即只要程序的最终结果与它顺序化情况的结果相等,那么指令的执 ...

  5. 希尔排序(java)

    时间复杂度为O( n^(3/2) )不是一个稳定的排序算法 如何看一个算法是否稳定:{("scala",12),("python",34),("c++ ...

  6. 基本排序算法——shell排序java实现

    shell排序是对插入排序的一种改进. package basic.sort; import java.util.Arrays; import java.util.Random; public cla ...

  7. 基本排序算法——选择排序java实现

    选择排序与冒泡排序有很大的相同点,都是一次遍历结束后能确定一个元素的最终位置,其主要思路是,一次遍历选取最小的元素与第一个元素交换,从而使得一个个元素有序,而后选择第二小的元素与第二个元素交换,知道, ...

  8. 选择排序-java

    排序-选择排序 基本思想:在待排序子表中找出最大(小)元素, 并将该元素放在子表的最前(后)面. 平均时间:O(n2) 最好情况:O(n2) 最坏情况:O(n2) 辅助空间:O(1) 稳定性:不稳定 ...

  9. 排序-java

    今天座右铭----每天的学习会让我们不断地进步! 往往面试中都会让我们用一种排序方法做一道排序题,下面我就罗列出快速排序.冒泡排序.插入排序.选择排序的java代码! 1.快速排序 public cl ...

随机推荐

  1. zabbix监控应用连接数

    zabbix使用用户自定义键值来监控应用系统连接数: 1.修改配置文件zabbix_agentd.conf 格式: UserParameter=<key>,<shell comman ...

  2. Python函数对象

    秉承着一切皆对象的理念,我们再次回头来看函数(function).函数也是一个对象,具有属性(可以使用dir()查询).作为对象,它还可以赋值给其它对象名,或者作为参数传递. lambda函数 在展开 ...

  3. 颜色矩阵 滤镜 ColorMatrix

    颜色矩阵原理 色彩的三要素 1.色相.色相通俗的说就是"颜色",色相的改变就是颜色的改变,色相的调节伴随着红橙黄绿蓝紫的变化. 2.亮度.明度通俗的说就是"光照度&quo ...

  4. css实现垂直居中6种方法

    在一次次笔试,一次次的面试中,问到垂直居中的问题太多太多,但是我每一次回答,都好像都不能让面试官太满意,今天特意花点时间,整理一下css垂直居中问题. 1.如果是单行文本.看代码: <!DOCT ...

  5. Linux下修改字符集,转自

    以下转自http://blog.csdn.net/cyuyan112233/article/details/6539122 Linux下修改字符集 locale -a 查询系统支持的字符集 expor ...

  6. JS正则验证格式

    function test() { var temp = document.getElementById("text1"); //对电子邮件的验证 var myreg = /^([ ...

  7. Java Se 基础系列(笔记) -- Exception && Array

    Exception 1.java 异常是java提供的用于处理程序中错误(指在程序运行的过程中发生的一些异常事件)的一种机制 2.java程序的执行过程中如果发生异常事件则自动生产一个异常类对象,该对 ...

  8. 15--Box2D使用(一、创建物理世界)

    创建工程Box2DTest,去掉背景和精灵对象等.首先在HelloWorldScene.h头文件定义一个屏幕像素与物理世界长度转换宏,并引入box2D头文件 #define PIXEL_TO_METE ...

  9. Activiti工作流学习-----基于5.19.0版本(6)

    七. BPMN的简介 读者了解到这里,应付一般的工作流开发已经足够了.此处应该有华丽的分割线,在工作流项目中核心开发人员主要是对工作流业务设计以及实现,而初级开发人员是对业务功能的代码实现.以后将主要 ...

  10. Codeforces 13C Sequence

    http://codeforces.com/contest/13/problem/C 题目大意 给定一个含有N个数的序列,要求你对一些数减掉或者加上某个值,使得序列变为非递减的,问你加减的值的总和最少 ...