(简单) 搜索 HOJ 1085 Finding Rectangles

Finding Rectangles

My Tags  (Edit)
Source : ACM ICPC Mid-Central USA 1998
Time limit : 1 secMemory limit : 32 M

Submitted : 258, Accepted : 118

Consider the point sets in figures 1a, 2a, and 3a. Using only those points as vertices, figures 1b, 2b, and 3b show all the rectangles that can be formed with horizontal and vertical sides. No rectangles can be formed from the points in figure 4.

(简单) 搜索 HOJ 1085 Finding Rectangles - 恶魔仁 - 恶魔仁

"""


Your task is to write a program that can find all rectangles that can be formed from a given set of points. The example input and output given below correspond to the figures above.


Input

The input contains one or more point sets, followed by a line containing the number 0 that signals the end of the file. Each point set begins with a line containing n, the number of points, and is followed by n lines that describe the points. Each point description contains a capital letter that is the label of the point, then a space, the horizontal coordinate, a space, and the vertical coordinate. Within each set, points labels occur in alphabetical order.

Note that since each point is labelled with a capital letter there can be at most 26 points. All coordinates are nonnegative integers less than 50. Points within a set are unique.


Output

The output for each point set starts with ``Point set ", followed by the number of the point set and a colon. If there are no rectangles, `` No rectangles" appears after the colon. If there are rectangles, they are listed starting on the next line. A blank precedes each rectangle. Each rectangle is given by its vertex labels, in clockwise order from the upper left, so the order is upper left, upper right, lower right, lower left. The rectangles are listed ten per line, except for the last line, where there may be as few as one. The rectangles are listed in alphabetical order.

Sample Input

7
A 1 1 
B 2 1 
C 3 1 
D 2 3 
E 3 3 
F 1 4 
G 3 4 
8 
B 1 1 
D 2 1 
F 4 1 
J 4 4 
L 2 4
M 2 3
N 4 3 
P 1 2 
12
A 1 5
B 2 5
C 1 4
D 2 4
E 1 3
F 2 3
G 1 2
H 2 2
I 1 1
J 2 1
K 1 0
L 2 0
5
B 1 1
D 2 1
L 2 4
N 2 3
P 1 2
0


Sample Output

Point set 1:
 DECB FGCA
Point set 2:
 LJFD LJNM MNFD
Point set 3:
 ABDC ABFE ABHG ABJI ABLK CDFE CDHG CDJI CDLK EFHG
 EFJI EFLK GHJI GHLK IJLK
Point set 4: No rectangles

题意:给一系列的点,找出所有的矩形
思路:数据量不大,直接枚举,检查是否是矩形就行了 , 最后注意输出的格式

代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<deque>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define MAX 100+10
#define MOD 99997
const int inf = 0xfffffff;

struct Point
{
int x,y;
}point[MAX];

bool operator<(const Point& p1,const Point& p2)
{
if (p1.x==p2.x) return p1.y<p2.y;
return p1.x<p2.x;
}
string ans[MAX];
char match[MAX][MAX];
int n;
int ans_sz;
bool flag;
void check_output(stack<Point> rect)
{
vector<Point> tem;
while (rect.size())
{
tem.push_back(rect.top());
rect.pop();
}
sort(tem.begin(),tem.end());
if (tem[0].x!=tem[1].x) return;
if (tem[2].x!=tem[3].x) return;
if (tem[2].x==tem[0].x) return;
if (tem[0].y>=tem[1].y) return;
if (tem[0].y!=tem[2].y) return;
if (tem[1].y!=tem[3].y) return;
ans[ans_sz] = "";
ans[ans_sz] += match[tem[1].x][tem[1].y];
ans[ans_sz] += match[tem[3].x][tem[3].y];
ans[ans_sz] += match[tem[2].x][tem[2].y];
ans[ans_sz++] += match[tem[0].x][tem[0].y];
flag = true;
}
void dfs(int cur,stack<Point> rect)
{
if (rect.size()==4)
{
check_output(rect);
return;
}
for (int i = cur ; i < n ; ++i)
{
rect.push(point[i]);
dfs(i+1,rect);
rect.pop();
}
}

int main()
{
int cas = 0;
while (scanf("%d",&n),n)
{
++cas;
for (int i = 0 ; i < n ; ++i)
{
char ch;
scanf(" %c%d%d",&ch,&point[i].x,&point[i].y);
match[point[i].x][point[i].y] = ch;
}
stack<Point> emty;
printf("Point set %d:",cas);
ans_sz = 0;
flag = false;
dfs(0,emty);
if (ans_sz==0)
{
printf(" No rectangles\n");
continue;
}
sort(ans,ans+ans_sz);
int cnt = 0;
printf("\n");
for (int i = 0 ; i < ans_sz ; ++i)
{
printf(" %s",ans[i].c_str());
++cnt;
if (cnt%10==0)
printf("\n");
}
if (cnt%10) printf("\n");
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值