Building bridges_hdu_4584(排序).java
Building bridges
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 301 Accepted Submission(s): 189
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.
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.
x1 y1 x2 y2
x1,y1 is the coordinate of Hululu island, x2 ,y2 is the coordinate of Cululu island.
CHCH
HCHC
CCCO
COHO
5 4
OHCH
HCHC
CCCO
COHO
HCHC
0 0
0 1 0 2
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的更多相关文章
- 希尔排序及希尔排序java代码
原文链接:http://www.orlion.ga/193/ 由上图可看到希尔排序先约定一个间隔(图中是4),然后对0.4.8这个三个位置的数据进行插入排序,然后向右移一位对位置1.5.9进行插入排序 ...
- 算法练习5---快速排序Java版
基本思想:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成 ...
- ElasticSearch中设置排序Java
有用的链接:http://stackoverflow.com/questions/12215380/sorting-on-several-fields-in-elasticsearch 有的时候,需要 ...
- 初识指令重排序,Java 中的锁
本文是作者原创,版权归作者所有.若要转载,请注明出处.本文只贴我觉得比较重要的源码 指令重排序 Java语言规范JVM线程内部维持顺序化语义,即只要程序的最终结果与它顺序化情况的结果相等,那么指令的执 ...
- 希尔排序(java)
时间复杂度为O( n^(3/2) )不是一个稳定的排序算法 如何看一个算法是否稳定:{("scala",12),("python",34),("c++ ...
- 基本排序算法——shell排序java实现
shell排序是对插入排序的一种改进. package basic.sort; import java.util.Arrays; import java.util.Random; public cla ...
- 基本排序算法——选择排序java实现
选择排序与冒泡排序有很大的相同点,都是一次遍历结束后能确定一个元素的最终位置,其主要思路是,一次遍历选取最小的元素与第一个元素交换,从而使得一个个元素有序,而后选择第二小的元素与第二个元素交换,知道, ...
- 选择排序-java
排序-选择排序 基本思想:在待排序子表中找出最大(小)元素, 并将该元素放在子表的最前(后)面. 平均时间:O(n2) 最好情况:O(n2) 最坏情况:O(n2) 辅助空间:O(1) 稳定性:不稳定 ...
- 排序-java
今天座右铭----每天的学习会让我们不断地进步! 往往面试中都会让我们用一种排序方法做一道排序题,下面我就罗列出快速排序.冒泡排序.插入排序.选择排序的java代码! 1.快速排序 public cl ...
随机推荐
- 设计模式17---设计模式之模板方法模式(Template Method)(行为型)
1.场景模拟 使用软件模拟登录控制,普通用户和工作人员用户,工作人员的密码在数据库中是加密的. 步骤大致如下: 前台提交,后台获取登录信息,同数据库中的登陆信息进行比较,只不过工作人员是加密的,普通用 ...
- java.lang.NoSuchMethodError: org.apache.commons.io.FileUtils.getTempDirectory()Ljava/io/File;
我出现这个问题的原因是使用ueditor上传图片 如果不是commons.io的jar包缺失,就是jar包有冲突 另外:最新的ueditor(1.4.3.1)使用的是commons-io-2.4.ja ...
- 设置开机启动时指定非ROOT用户执行相应的脚本
[root@MSJTVL-MJSP-A01 sm01]# vim /etc/rc.d/rc.local #!/bin/sh # # This script will be executed *afte ...
- CentOS6.7 常用操作命令
centos 安装py环境 1.安装wget工具: yum install wget 2.安装Python-2.7.8: wget --no-check-certificate https://www ...
- .net程序开发人员必看的变量的命名规则
(1)类名.属性名.方法名采用Pascal命名,如 class User { } interface IEditable { } bool ValidateInput() public int Age ...
- new的原理
先来个构造函数的例子: function Prince(name,age){ this.name=name; this.age=age; } var prince=new Prince("c ...
- EasyUI 1.4.4 DataGrid(大数据量) bufferview滚动时不加载下一页数据解决方案
在使用Easyui DataGrid 过程中,发现若单页数据量超过300,IE浏览器加载速度很慢.也通过网上找寻了很多解决方案,最典型的就是去掉datagrid的自动列宽以及自动行高判断. 1.解决自 ...
- OC基础 代理和协议
OC基础 代理和协议 1.协议 (1)oc语言中得协议:一组方法列表,不需要我们自己实现,由遵守协议的类来实现协议所定制的方法. (2)协议的使用步骤:制定协议-->遵守协议-->实现协议 ...
- Android小试牛刀之遇到的问题
1.运行出错 创建项目时没有使用Empty Activity,创建. 2.创建第一个工程 选择Empty Activity才会自动创建Hello Word代码块 3.appcompat_v7的说明 在 ...
- h5标签canvas关于getImageData跨域的问题
h5标签canvas关于getImageData跨域的问题 在学习h5的时候,canvas标签中getImageData()报错:security error! 具体代码如下(chrome浏览器): ...