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. HDU2841 Visible Trees (容斥原理)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意: 一个人在(0,0)点,然后前面有一个m*n的格子 ,每一个格子的节点上有一棵树.问这个人 ...

  2. Python3.2官方文档翻译--继承

    6.5 继承 当然,一门语言特性假设不支持继承那么名称类就失去了价值.子类继承父类的方法例如以下: class DerivedClassName(BaseClassName): <stateme ...

  3. FIR滤波器设计

    FIR滤波器的优越性: 相位对应为严格的线性,不存在延迟失真,仅仅有固定的时间延迟: 因为不存在稳定性问题,设计相对简单: 仅仅包括实数算法,不涉及复数算法,不须要递推运算,长度为M,阶数为M-1,计 ...

  4. -bash: mysql: command not found 解决办法 (转)

    root@DB-02 ~]# mysql -u root-bash: mysql: command not found 原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下 ...

  5. mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法

    1. 问题背景         InnoDB是新版MySQL(v5.5及以后)默认的存储引擎,之前版本的默认引擎为MyISAM,因此,低于5.5版本的mysql配置文件.my.cnf中,关于InnoD ...

  6. python课程第一天作业1-模拟登录

    第一周作业: 作业1:编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 流程图: 代码:后来修改过一次: #!/usr/bin/env python # -*-conding:ut ...

  7. InstallShield常用prq文件的下载地址

    VC 2010 redist X86: http://saturn.installshield.com/is/prerequisites/microsoft visual c++ 2010 redis ...

  8. oracle rac 数据库常用命令

    oracle rac 数据库常用命令:1.所有实例和服务的状态srvclt status database -d orcl单个实例的状态:srvctl status instance -d orcl ...

  9. 武汉科技大学ACM :1002: 零起点学算法28——判断是否闰年

    Problem Description 输入年份,判断是否闰年 Input 输入一个整数n(多组数据) Output 如果是闰年,输出yes,否则输出no(每组数据一行) Sample Input 2 ...

  10. hdu5392 Infoplane in Tina Town(LCM)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Infoplane in Tina Town Time Limit: 14000/ ...