Squares
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 13089 | Accepted: 4840 |
Description
A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates.
Input
The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.
Output
For each test case, print on a line the number of squares one can form from the given stars.
Sample Input
4 1 0 0 1 1 1 0 0 9 0 0 1 0 2 0 0 2 1 2 2 2 0 1 1 1 2 1 4 -2 5 3 7 0 0 5 2 0
Sample Output
1 6 1
CODE:
package set1; //组合算法 //本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标 //代表的数被选中,为0则没选中。 //首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。 //然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为 //“01”组合,同时将其左边的所有“1”全部移动到数组的最左端。 //当第一个“1”移动到数组的m-n的位置,即n个“1”全部移动到最右端时,就得 //到了最后一个组合。 //例如求5中选3的组合: //1 1 1 0 0 //1,2,3 //1 1 0 1 0 //1,2,4 //1 0 1 1 0 //1,3,4 //0 1 1 1 0 //2,3,4 //1 1 0 0 1 //1,2,5 //1 0 1 0 1 //1,3,5 //0 1 1 0 1 //2,3,5 //1 0 0 1 1 //1,4,5 //0 1 0 1 1 //2,4,5 //0 0 1 1 1 //3,4,5 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Collection; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedList; import java.util.Map.Entry; import java.util.Queue; /** * ID = 2002 * @author Administrator * url : http://poj.org/problem?id=2002 */ public class Squares { BufferedReader in = null; String inStr = null; int[] flag = null; final boolean DEBUG = false; public Squares(){ in = new BufferedReader(new InputStreamReader(System.in)); try { inStr = in.readLine(); inStr = inStr.trim(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } while(!inStr.equals("0")){ try { System.out.println(calculate(inStr)); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { inStr = in.readLine(); inStr = inStr.trim(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] argv){ new Squares(); } @SuppressWarnings("unchecked") private Integer calculate(String len) throws IOException{ int length = Integer.parseInt(len); int square_num = 0; Queue<Star_pair>[] star_list = new LinkedList[length]; Posn[] stars = new Posn[length]; myHashtable lengthTable = new myHashtable(); // Store the star posn for(int i = 0; i < length; i++){ inStr = in.readLine(); inStr = inStr.trim(); String[] listStr = new String[2]; listStr = inStr.split(" "); stars[i] = new Posn(Integer.parseInt(listStr[0]),Integer.parseInt(listStr[1])); } // Sort stars in pairs and stor pairs ordered by length for(int i = 1; i < length; i++) { star_list[i] = new LinkedList<Star_pair>(); for(int j = 0; j < i; j++){ Star_pair sp = new Star_pair(stars[i],stars[j]); star_list[i].offer(sp); // insert these pair star in different slot by length lengthTable.put(cal_len(sp), sp); } } // Iterator iterator = lengthTable.entrySet().iterator(); while(iterator.hasNext()){ LinkedList<Star_pair> lt = null; Entry<Object,LinkedList> e =(Entry<Object, LinkedList>) iterator.next(); lt = e.getValue(); if(lt.size() < 4) continue; else square_num += look_for_square(lt); } return square_num; } private int look_for_square(LinkedList<Star_pair> lt){ int result = 0; flag = new int[lt.size()]; for(int i = 0; i < lt.size(); i++) flag[i] = 0; flag[0] = 1; flag[1] = 1; flag[2] = 1; flag[3] = 1; while(true){ if(isSquare(flag,lt)){ result ++; } if(!swap(lt)) return result; } } private boolean swap( LinkedList lt){ for(int i = 1; i < flag.length; i++){ if(flag[i - 1] == 1 && flag[i] == 0){ flag[i - 1] = 0; flag[i] = 1; int howManyOne = 0; for(int j = 0; j < i - 1; j++) if(flag[j] == 1) howManyOne ++; for(int j = 0; j < howManyOne; j++) flag[j] = 1; for(int j = howManyOne; j < i - 1; j++) flag[j] = 0; return true; } } return false; } private boolean isSquare(int[] flag, LinkedList<Star_pair> lt){ LinkedList squareCorner = new LinkedList<Posn>(); Star_pair star_pair = null; System.out.println(""); for(int i = 0; i < flag.length; i++) if(flag[i] == 1){ star_pair = lt.get(i); if(!squareCorner.contains(star_pair.a)) squareCorner.offer(star_pair.a); if(!squareCorner.contains(star_pair.b)) squareCorner.offer(star_pair.b); } if(squareCorner.size() == 4) return true; return false; } private float cal_len(Star_pair sp){ Posn a,b; a = sp.a; b = sp.b; float len = (float) Math.sqrt(Math.pow((a.x - b.x),2) + Math.pow((a.y - b.y),2)); return len; } private class Posn { int x,y; public Posn(int a, int b){ x = a; y = b; } } private class myHashtable extends Hashtable{ public boolean containsKey(float length){ return super.contains(length); } public LinkedList<Star_pair> get(Object key){ return (LinkedList)super.get(key); } public Object put(Object key, Object value){ Queue que = null; if(containsKey(key)){ que = this.get(key); que.offer(value); super.put(key, que); } else { que = new LinkedList<Star_pair>(); que.offer(value); super.put(key, que); } return null; } } private class Star_pair{ Posn a = null; Posn b = null; public Star_pair(Posn a, Posn b){ this.a = a; this.b = b; } } }