上海源泰汽配怎么样:google中国编程赛的俩题 NO1!请高手做!

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/28 00:30:42
NO1.
Problem Statement
????
You are given a String[] cityMap representing the layout of a city. The city consists of blocks. The first element of cityMap represents the first row of blocks, etc. A 'B' character indicates a location where there is a bus stop. There will be exactly one 'X' character, indicating your location. All other characters will be '.'. You are also given an int walkingDistance, which is the maximum distance you are willing to walk to a bus stop. The distance should be calculated as the number of blocks vertically plus the number of blocks horizontally. Return the number of bus stops that are within walking distance of your current location.
Definition
????
Class:
BusStops
Method:
countStops
Parameters:
String[], int
Returns:
int
Method signature:
int countStops(String[] cityMap, int walkingDistance)
(be sure your method is public)
????

Constraints
-
cityMap will contain between 1 and 50 elements, inclusive.
-
Each element of cityMap will contain between 1 and 50 characters, inclusive.
-
Each element of cityMap will contain the same number of characters.
-
Each character of each element of cityMap will be 'B', 'X', or '.'.
-
There will be exactly one 'X' character in cityMap.
-
walkingDistance will be between 1 and 100, inclusive.
Examples
0)

????
{"...B.",
".....",
"..X.B",
".....",
"B...."}
3
Returns: 2
You can reach the bus stop at the top (3 units away), or on the right (2 units away). The one in the lower left is 4 units away, which is too far.
1)

????
{"B.B..",
".....",
"B....",
".....",
"....X"}
8
Returns: 3
A distance of 8 can get us anywhere on the map, so we can reach all 3 bus stops.
2)

????
{"BBBBB",
"BB.BB",
"B.X.B",
"BB.BB",
"BBBBB"}
1
Returns: 0
Plenty of bus stops, but unfortunately we cannot reach any of them.
3)

????
{"B..B..",
".B...B",
"..B...",
"..B.X.",
"B.B.B.",
".B.B.B"}
3
Returns: 7

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)2003, TopCoder, Inc. All rights reserved.

没分不好意思!!

为了方便大家编程,我翻译了一下,E文不够好,自己想了之后翻译的,但是基本意决对够正确。刚掉线了,到现在才发上来。

问题描述

现在给你一个字符串数组cityMap来表示一个城市的布局。这个城市由多个街区组成。字符串数组cityMap的第一个元素表示这些街区的第一行,等等。用字符'B'表示巴士站的位置。使用字符'X'表示你的位置。剩下的用字符'.'表示。

给你一个整型变量walkingDistance,用来表示你走到巴士站的最大距离。距离由竖直的街区数加上水平的街区数计算得到。返回在你当前位置的walkingDistance范围内巴士站的个数。

定义下列结构:
类 BusStops
它的方法有
countStops
参数
String[], int
返回值
int
使用方法
int countStops(String[] cityMap, int walkingDistance)
(保证这个方法是公共的Public)

规定
-
字符串数组cityMap的元素个数在1-50以内(包括1,50)
-
cityMap的每个元素要有1-50个字符(包括1,50)
-
cityMap的每个数组元素的字符个数要相同
-
每个元素的字符集为{'B', 'X', '.'}(就是只字符只能是'B', 'X', '.')
-
数组cityMap中,一共只能有一个'X'
-
walkingDistance 取值为1-100,包括1, 100

例1)
cityMap =
{". . . B .",
". . . . .",
". . X . B",
". . . . .",
"B . . . ."}

walkingDistance = 3

那么返回的值就是2,即,
你能到达在你上方(不是你的头顶)的那个巴士站(3步远),或者到你右边的那个(2步远)。在你的左下方的那个站有4步远,大于3步。

例2)
cityMap =
{"B . B . .",
". . . . .",
"B . . . .",
". . . . .",
". . . . X"}

walkingDistance = 8

返回值为3,也就是说
8步可以走遍这个地图,所以我们能到达全部的3个巴士站。

例3)
cityMap =
{"B B B B B",
"B B . B B",
"B . X . B",
"B B . B B",
"B B B B B"}

walkingDistance = 1

返回值为0,
很多巴士站,不过很可惜,一个也去不了。

例4)
cityMap =
{"B . . B . .",
". B . . . B",
". . B . . .",
". . B . X .",
"B . B . B .",
". B . B . B"}

walkingDistance = 3

共能到达7个站。

程序说明所有权和解释权归TopCoder公司。任何未经授权、没有得到TopCoder公司的签署的授权文件而使用或者更改此信息的行为,都将受到起诉。©2003, TopCoder, Inc. All rights reserved.

断网一天多了,到现在才能上!No.1做出来了,不过是C++版的,原题目可能是让用Java的吧。不管怎么样,做出来了:

#include <iostream>
#include <cstring>
using namespace std;
int indexOf(string str, char chr)
{
for(int i =0; str[i]; i++)
{
if(str[i] == chr) return i;
}
return -1;
}
int getStringLen(string str)
{
int i;
for(i = 0; str[i]; i++);
return i;
}
int getArrayLen(string cM[])
{
int i = 0;
for(i = 0; cM[i] != ""; i++);
return i;
}
void printMap(string cM[])
{
int gI = getArrayLen(cM), i;
int gJ = getStringLen(cM[0]), j;
for(i = 0; i < gI; i++)
{
for(j = 0; j < gJ; j++)
{
cout << cM[i][j] << " ";
}
cout << endl;
}
}
int getLocationX(string cM[])
{
int gI = getArrayLen(cM), i;
for(i = 0; i < gI; i++)
{
if(indexOf(cM[i], 'X') >= 0) return i;
}
return -1;
}
int getLocationY(string cM[])
{
int gI = getArrayLen(cM), i;
for(i = 0; i < gI; i++)
{
if(indexOf(cM[i], 'X') >= 0)
{
return indexOf(cM[i], 'X');
}
}
return -1;
}

class BusStops
{
public:
int countStops(string[], int);
};
int BusStops::countStops(string cM[], int wD)
{
int gI = getArrayLen(cM);
int gJ = getStringLen(cM[0]);
int iL = getLocationX(cM);
int jL = getLocationY(cM);
int retValue = 0, i, j, t;
for(i = 0; i < gI; i++)
{
for(j = 0; j < gJ; j++)
{
t = 0;
if(cM[i][j] == 'B')
{
t += abs(i - iL) + abs(j - jL);
if(t <= wD) retValue++;
}
}
}
return retValue;
}
int main(int argc, char *argv[])
{
BusStops BS;
string cityMap[] =
{".......B",
".B.B....",
".B.B....",
"..XBB...",
".B.B....",
".B.B....",
"...BBBBB",
"......B."};
int walkingDistance = 3;
printMap(cityMap);
cout << "\nwalkingDistance = "; cin >> walkingDistance;
cout << "Number of Bus Stops: ";
cout << BS.countStops(cityMap, walkingDistance) << endl;

system("Pause");
return 0;
}

运行结果
. . . . . . . B
. B . B . . . .
. B . B . . . .
. . X B B . . .
. B . B . . . .
. B . B . . . .
. . . B B B B B
. . . . . . B .

walkingDistance = 3
Number of Bus Stops: 10
Press any key to continue...

唉,纯英文的阿,看得头都大了......

非常实用的问题,虽然很简单。
先找出 X 坐标设为(a,b ) 则(x,y) 与X的距离= |x-a|+|y-b|, 遍历所有点如果距离<=distance 且是B(us stop),count++