Algorithm Practice for 2002

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. 

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电子图书资源服务系统是一款基于 Java Swing 的 C-S 应用,旨在提供电子图书资源一站式服务,可从系统提供的图书资源中直接检索资源并进行下载。.zip优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人系统开发经验充足,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(若有),项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注计算机领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能 【无积分此资源可联系获取】 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。积分/付费仅作为资源整理辛苦费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值