POJ 2900 Griddy Hobby

Griddy Hobby
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 414 Accepted: 146

Description

Attending a boring weekly session, our professor started drawing on a grid in a page of his calendar. He started at a boundary grid point P; note that P is a corner of one or two grid cells. He drew a diameter of one of those cells and continued on a straight line until reaching point Q on another edge of the grid. Then he started another line from Q, perpendicular to the line PQ until hitting another edge at point R. He kept drawing lines as above, until he could not draw a new line, either because a perpendicular line would not start with a cell diameter or it would fall on an already drawn line. Then he was puzzling how he would be able to count the number of minimal rectangles he has created on the grid. At this time, the chair of the session noticed him and asked him what he was doing. "Sorry, I was designing a problem for the ACM-ICPC Tehran site," he said.

You are to write a program to, given the dimensions R and C of a grid, the coordinates x and y of a point P on one of the edges of the grid, and the direction (up, left), (down, left), (up, right), or (down, right) of the first line, help the professor to find out the number of minimal rectangles.

Input

The input file contains multiple test cases. The first line of the input, contains t, the number of test cases that follow. Each of the following t blocks, describes a test case.

The first line of each block, contains two positive integers R and C, which are the number of horizontal and vertical gridlines, respectively (2 <= R,C <= 1000). The coordinates of the starting point comes on the next line, as two positive integers y and x, (1 <= y <= R, 1 <= x <= C ); for the upper-left point of the grid, we have x = y = 1. The third line of each test case, contains a two-character code which is one of the following:
  • "DR", the first move is in the down-right direction,
  • "DL", the first move is in the down-left direction,
  • "UL", the first move is in the up-left direction,
  • "UR", the first move is in the up-right direction.

Output

For each test case, output one line containing the number of non-overlapping rectangles formed.

Sample Input

2
4 5
2 1
DR
4 5
4 1
UR

Sample Output

1
3

Source

 

/*

Keep fighting.

计算几何题,仔细思考不难发现,所有直线交点(转折点除外,即反射点),所有当前

新生成的直线与其相邻直线外所有直线交点的个数和即为所求,所以在模拟过程中可以

存储所有的直线当处理某一条直线时,只需遍历所有已经生成的直线(其相邻直线除外)

统计交点个数即可。终止条件的判断用了个技巧,每个点都有1,2,3,4四个方向,如果

有哪个点的方向被重复或者到底矩形的四个顶点则访问终止

*/

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值