AvoidRoads

来源:互联网 发布:天刀女捏脸数据 编辑:程序博客网 时间:2024/06/05 00:21

Problem Statement
Problem contains images. Plugin users can view them in the applet.

In the city, roads are arranged in a grid pattern. Each point on the grid represents a corner where two blocks meet. The points are connected by line segments which represent the various street blocks. Using the cartesian coordinate system, we can assign a pair of integers to each corner as shown below.

这里写图片描述

You are standing at the corner with coordinates 0,0. Your destination is at corner width,height. You will return the number of distinct paths that lead to your destination. Each path must use exactly width+height blocks. In addition, the city has declared certain street blocks untraversable. These blocks may not be a part of any path. You will be given a String[] bad describing which blocks are bad. If (quotes for clarity) “a b c d” is an element of bad, it means the block from corner a,b to corner c,d is untraversable. For example, let’s say

width = 6
length = 6
bad = {“0 0 0 1”,”6 6 5 6”}

The picture below shows the grid, with untraversable blocks darkened in black. A sample path has been highlighted in red.

这里写图片描述

Definition

Class: AvoidRoads
Method: numWays
Parameters: int, int, String[]
Returns: long
Method signature: long numWays(int width, int height, String[] bad)
(be sure your method is public)

Constraints
- width will be between 1 and 100 inclusive.
- height will be between 1 and 100 inclusive.
- bad will contain between 0 and 50 elements inclusive.
- Each element of bad will contain between 7 and 14 characters inclusive.
- Each element of the bad will be in the format “a b c d” where,

a,b,c,d are integers with no extra leading zeros,a and c are between 0 and width inclusive,b and d are between 0 and height inclusive,and a,b is one block away from c,d.
  • The return value will be between 0 and 2^63-1 inclusive.

Examples
0)

6

6

{“0 0 0 1”,”6 6 5 6”}

Returns: 252

Example from above.
1)

1

1

{}

Returns: 2

Four blocks aranged in a square. Only 2 paths allowed.
2)

35

31

{}

Returns: 6406484391866534976

Big number.
3)

2

2

{“0 0 1 0”, “1 2 2 2”, “1 1 2 1”}

Returns: 0

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.
This problem was used for:
2003 TCO Semifinal Round 4 - Division I, Level One


看到BigInter就懒得写了
除了BigInter以外就是洛谷P1002 过河卒了,感觉就是一道水题啊,所以程序还是不写了,就写写思路吧,跟过河卒一样
f(i,j)表示坐标为(i,j)时的方案数,由题意可得f(i,j)=f(i1,j)+f(i,j1),如果要实现的话还是比较容易的。
给出伪代码:

     //读入障碍     f[0][0] = 1;     for i=0 -> n      for j=0 -> n       if a[i][j] != false        f[i][j] = f[i-1][j] + f[i][j-1];//前提是合法
原创粉丝点击