zoukankan      html  css  js  c++  java
  • 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

    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;
    	}
    }



  • 相关阅读:
    Spring boot 使用多个RedisTemplate
    Spring boot 连接Redis实现HMSET操作
    Spring boot 工具类静态属性注入及多环境配置
    向量空间模型(Vector Space Model)的理解
    双数组Trie树中叶子结点check[t]=t的证明
    谈谈我对隐马尔可夫模型的理解
    Information Retrieval 倒排索引 学习笔记
    朴素贝叶斯文本分类简单介绍
    Python Thrift 简单示例
    迭代器模式(Iterator)
  • 原文地址:https://www.cnblogs.com/pangblog/p/3265418.html
Copyright © 2011-2022 走看看