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. [RxJS] Observables can complete

    The Observer object has the functions next() and error(). In this lesson we will see the other (and ...

  2. [Angular 2] Transclusion in Angular 2

    Link: Blog Single transclude: <ng-content></ng-content> Multi-translcude: <ng-content ...

  3. 数据持久化之CoreData

    再次回归博客园, 已经实属不易了, 面临这近期忙忙碌碌的项目开发, 虽然并不是完全的没有闲暇时间, 但是怎么说呢, 也有着各种的无奈与曲折, 面临这产品需求的不断变化和页面的不断更新, 对于一个程序员 ...

  4. php 多维数组如何用foreach遍历修改其中的一个值

    数组: array(6) { [0]=> array(11) { ["id"]=> string(2) "76" ["topic_id&q ...

  5. mybatis知识总结

    基于昨天的mybatis入门详解,今天我们再来看看mybatis稍微高深些的知识点. 1.解决Model属性和数据库字段不一致的问题 1),开启驼峰命名 2),使用resultMap进行映射, < ...

  6. 2:url有规律的多页面爬取

    举例网站:http://www.luoo.net/music/期刊号 e.g:http://www.luoo.net/music/760 打算爬取其title:Hello World:pic:desc ...

  7. KM算法专题

    原文:http://972169909-qq-com.iteye.com/blog/1184514 题目地址:这里. 1)求图中所有环的总长度(环的长度不唯一)的最小值.当无法得到完备匹配时说明环不存 ...

  8. XP 安装

    提供一下裝系統的詳細步驟,盡量詳細到每一步都有,希望能對樓主有所幫助,不盡之處還請樓主不吝指出!謝謝 装XP的步骤如下: 开机时,按del键, 进入bios界面,一般选左侧第二项,(Advanced ...

  9. 解决JavaScript中如何输出空格

    在写JS代码的时候,大家可以会发现这样现象:document.write("   1      2                3  ");结果: 1 2 3无论在输出的内容中什 ...

  10. 关于ajax直接提交表单jQuery .validator验证不起作用问题

    之前用$.ajax(function(){});直接提交表单,而表单验证不通过也能提交. $(document).ready(function(){ $.ajax({       url:" ...