IOI2006 D2-3:A BLACK BOX GAME(带中文)

来源:互联网 发布:韩顺平php全套笔记 编辑:程序博客网 时间:2024/05/16 15:17

 IOI2006 D2-3

Blackbox Official version
IOI’06 English
Day 2 – Task 3 Version 1.6
Number of pages 1 IOI’06 Page 1

A BLACK BOX GAME(原文)


The Black Box Game is played with a square-shaped black box lying flat on a table. Each of its four
sides has n holes (for a total of 4n holes) into which a ball can be thrown. A thrown ball will eventually
exit from one of the 4n holes, potentially the same hole into which it was thrown.

The black box’s internals can be envisioned as an n x n grid. The holes in the sides are the starts and
ends of rows and columns. Each of the box’s squares is either empty or occupied by a deflector. A
deflector is a piece of hardware that changes the direction of the ball by 90 degrees. Consider this
example of a 5x5 box.

A ball thrown into the box follows a straight line until it either hits a deflector or exits the box. When a
ball hits a deflector, the ball changes direction and the deflector toggles its position (by “toggle” we
mean rotate 90 degrees). The examples below show the action of a deflector.

a) A ball is thrown through a hole; it hits a deflector and changes direction.
b) After the first ball was thrown, the deflector has toggled its position. A new ball is thrown into
the same hole, hits the deflector and is deflected in a direction opposite to that of the first ball.
c) The deflector toggles every time it is hit.


Whenever a deflector is hit, it makes a beep. The number of times the ball was deflected can be
deduced by counting the beeps. It can be proved that the ball always exits the box. The box has a
button that resets it to its original state and another button that toggles all of its deflectors.


TASK
You will be provided with an interface to 15 black boxes via a library of Pascal or C/C++ functions. You
must determine the internals of each one of them as best as possible and submit a file describing each.
You will also be provided with a method to define your own black boxes for testing.

CONSTRAINTS
1 ≤ n ≤ 30

OUTPUT
You must submit a file containing the following data for each one of the 15 black-boxes

blackboxK.out DESCRIPTION
#FILE blackbox K
.....
.../.
./...
.../.
.?.?.LINE 1: The file header. The file header must contain
#FILE blackbox K
where K (range 1..15) corresponds to the box being solved.
n LINES: Each line describes a row of the box, starting from the topmost row
to the bottom row. Each line must contain exactly n characters; each
character corresponds to a column (running from left to right).
• ‘.’ means that the square is empty.
• ‘/’ means the square contains a deflector with initial position ‘/’
• ‘/’ means the square contains a deflector with initial position ‘/’
• ‘?’ means that you were unable to determine the initial contents of
that square

LIBRARY
You are given a library that provides the following functions

FUNCTIONDescriptionPASCAL
function Initialize(box: integer): integer;

C/C++
int Initialize(int box);Initializes the library, must be called once at the start of
your program. It returns n, the number of holes on each
side of the box.
The parameter box must contain an integer between 1
and 15 indicating the box you want to use or 0 if you
want to use a box created by you.PASCAL
f
unction throwBall(holeIn, sideIn: integer;
var holeOut, sideOut: integer): longint;

C
int throwBall(int holeIn, int sideIn,
int *holeOut, int *sideOut);

C++
int throwBall(int holeIn, int sideIn,
int &holeOut, int &sideOut);Throws a ball into the box through hole number holeIn in
side sideIn, sides are numbered as 1 – Top, 2 – Right, 3
– Bottom and 4 – Left. Holes are numbered from left to
right and from top to bottom starting from number 1 in
each side. In holeOut and sideOut you’ll receive the hole
and side number where the ball exits the box. The
function throwBall returns the number of beeps caused
by the ball hitting a deflector.PASCAL
procedure ResetBox;

C/C++
void ResetBox();Resets every deflector in the box to its initial position.PASCAL
procedure ToggleDeflectors;

C/C++

void ToggleDeflectors();Toggles every deflector in the box.PASCAL
procedure Finalize;
C/C++
void Finalize();Gracefully ends the interaction with the box. It should be
called at the end of your program.


 To be able to use the library in your program do as follows:

• FreePascal: In the task directory you will find the files pbblib.o and pbblib.ppu to be
able to use them include the following statement.
uses pbblib;
The file pblackbox.pas gives an example of how to use the library.

• C: In the task directory you will find the files cbblib.o and cbblib.h, to be able to use them
include the following statement in your code.
#include “cbblib.h”
The file cblackbox.c gives an example of how to use the library. In order to compile your
code you will need to use the following command
gcc –o yourprogram cbblib.o yourprogram.c

• C++: In the task directory you will find the files cppbblib.o and cppbblib.h, to be able to
use them include the following statement in your code.
#include “cppbblib.h”
The file cppblackbox.cpp gives an example of how to use the library. In order to compile
your code you will need to use the following command
g++ –o yourprogram cppbblib.o yourprogram.cpp

NOTE: At any given time only one program using the library can be running.

SAMPLE INTERACTION
A sample interaction for the box in the previous figure could be:

FUNCTION CALLVALUE RETURNED BY FUNCTIONInitialize(0);Assuming that the box used is the one in the previous
figure. Returns 5 indicating that n = 5PASCAL
throwBall(3,4,holeOut,sideOut);
C
throwBall(3,4,&holeOut,&sideOut);
C++
throwBall(3,4,holeOut,sideOut);A ball is thrown into hole number 3 (third from the top) on
the left side. Returns 1, indicating that the ball hit 1
deflector. When the function returns, holeOut will equal 2
and sideOut will equal 3 indicating that the ball exited
through hole 2 (second from the left) of the bottom side
of the box.

 
EXPERIMENTATION
If you pass an integer value of 0 to the function Initialize, the library will read the box internals from
the file blackbox.in. In this way you can experiment with the library. The format for the file
blackbox.in is described below.

blackbox.in DESCRIPTION5 3
2 3 /
4 2 /
4 4 /LINE 1: Contains n, the number of holes on each side.
LINE 2: Contains an integer d that indicates the number of deflectors
in the box.
NEXT d LINES: There must be one line for every deflector in the box.
Each line contains two space-separated integers that represent
the column and the row of the deflector respectively, and a
character separated with a space from the second integer that
can be either ‘/’ or ‘/’ that represents the original position of the
deflector.

NOTE; The example blackbox.in describes the black box shown in the figure at the top of page 1.

ERROR MESSAGES
In the case of any anomalies the library will output an error message to the standard error. The possible
error message that you can get and their meanings are shown in the table below.

ERROR MESSAGEMEANINGERR 1 More than one app Only one application at a time can interact with the black boxes,
please restart all applications and start only one at a timeERR 2 Invalid boxThe box number you input is not in the range from 0 to 15ERR 3 Invalid deflectorThe file blackbox.in has a deflector in an invalid positionERR 4 Invalid symbolThe file blackbox.in has an invalid symbolERR 5 Invalid sizeThe size of the black box in blackbox.in is invalid.ERR 6 Invalid input hole Either the side or the input hole that you entered are invalid ERR 7 ALARMPlease call the technical staff



GRADING
For each box you must submit a text file that describes the internals of the black box as best as
possible. For each box:
• If your submission has a ‘.’, ‘/’ or ‘/’ character in an incorrect position, you’ll get zero points for
that box.
• Let Bm be the maximum number of discovered positions among all of the correct submissions,
and let By be the number of discovered positions in your submission, then percentage of your
score for that box will be

100 By / Bm

NOTE
: The official solution for this task can programmatically discover 100% of the initial contents of
any of the boxes in a time less than 8 minutes. 

 

 

黑箱子游戏(Black Box Game)(译文)

(IOI2006赛题2-03)


黑箱子游戏是在一个扁的方框中进行。该方框每边有n个孔(四边共4n孔),通过各个孔可以向
内投小球。被投进去的球最终会从
4n个孔的一个滚出,投入的球有可能从同一个孔滚出。

图1 黑箱子游戏用的方框

Hole:进球的孔,Deflector:偏转器)

 

黑箱子的内部可想象为n x n个的小方格。四个边上的孔是各行各列小方格的起点和终点。箱子的每个小方格或者是空的,或者被一偏转器(deflector)所占据。所谓偏转器就是一块硬片,它能让进入的球碰撞后产生方向为90度的偏转(图2a到图2b)。下面考察5x5方格的一个箱子作为例子。

 

球被掷入箱子后沿直线向前滚动,直到击中一个偏转器或退出箱子为止。当它击中一个偏转器时,球就改变方向,并且触发偏转器翻转位置("翻转"意味转动90)。下图所示例子显示了偏转器的动作。

    图2投入箱内的球使偏转器的方向改变过程

2 a,b,c意义如下:

a)    第一个小球通过箱子左边的一个孔抛入箱内,小球击中一偏转器后改变了前进方向,由箱子底边的
一个孔中离开了箱子。

b)    第一球的投入同时也使偏转器的方向切换成它的另一种位置。当第二个球投进同一孔击中同一个偏
转器时产生和第一球相反的前进方向,使它从顶部一孔弹出。

c)    偏转器每次被击中时候都翻转,故两次击中后就恢复成原始状态。

 

当偏转器被击中,产生一个beep声。球的偏转次数可由beep声的计数推出。可以证明,球最终总能
从箱子中退出。箱子有一按钮,可以将它重新设置成它的原始状态,另外有一个按钮,可以将它所有的
偏转器实行翻转。

 

任务

给定15个黑箱子,以及用作接口的PascalC/C++函数库。要您用最好的方法来确定每个箱子的
内部结构,并为每一个箱子提交一个描述文件。同时为您提供了用于测试你的黑箱子的方法。

 

限制

1<=n<=30

 

输出

你应为15个黑箱中的每一个提供一个文件,它包含下列数据:

blackboxK.out

描述

#FILE blackbox K

 

 

 

.....

.../.

./...

.../.

.?.?.

首行:文件头。文件头具有如下形式:

# 文件blackbox K

其中 K (范围为1..15)对应被求解箱子。

 

接着n行:每行描述箱子对应的一行,从最上面的行开始至最底下一行。各行必须恰好包含n字符,它们依次对应于箱子从左到右的各小方格。

’.’对应一个空方格,

’/’代表开始的时候内含/偏转器的方格

’/’代表开始的时候内含/偏转器的方格

’?’代表您无法确定其初始内含的方格

 

 

为您提供的函数库是

函  数

说  明

PASCAL

function Initialize

box: integer): integer;

C/C++

int Initialize(int box);

 

初始化数据库,当您的程序每次启动时需调用一次。返回箱子每边的孔数n

参数box是一个整数,代表你想使用的箱子的编号,如果box0,代表你想使用您自己创
建的箱子。

 

PASCAL

function throwBall(

holeIn, sideIn: integer;

var holeOut,sideOut:integer):longint;

C

int throwBall(int holeIn, int sideIn,

int *holeOut, int *sideOut);

C++

int throwBall(int holeIn, int sideIn,

int &holeOut, int &sideOut);

 

将一球投入箱子的sideIn边的第holeIn个孔,边sideIn的编号为:1 - Top, 2 –Right , 3–bottom,4 - Left。孔的编号
是从上到下或从左到右,每边起始
号都为1。在holeOutsideOut中您将接受球的退出的边号和孔的编号。函数throwBall返回球击中偏转器所产生的beep声的数量。

 

PASCAL

procedure ResetBox;

C/C++

void ResetBox();

 

将箱子中每个偏转器重设为它们最初位置。

 

PASCAL

procedure ToggleDeflectors;

C/C++

void ToggleDeflectors();

 

 

将箱子中每个偏转器的位置实行翻转。

 

PASCAL

procedure Finalize;

C/C++

void Finalize();

 

完成与箱子的交互作用。它应在每次程序
结尾处调用。

 

 

您的程序能使用的库如下:

FreePascal:

task目录中您将找到pbblib.opbblib.ppu两个文件,你可用下列的包含语句

uses pbblib;

来使用库中的函数。

文件pblackbox.pas给出了怎样使用这一个库的范例。

 

C: 

task目录中您将找到cbblib.o和 cbblib.h两个文件,你可用下列的包含语句

#include “cbblib.h”

来使用库中的函数。

文件cblackbox.c中给出了怎样使用这一库的一个范例。

 

C++: 

task目录中您将找到cppbblib.ocbblib.h两个文件,你可用下列的包含语句

#include “cppbblib.h”

来使用库中的函数。

文件cppblackbox.cpp中给出了怎样使用这一库的一个范例。为了编译您的代码,您需要使用
以下命令

g++ - o yourprogram cppbblib.o yourprogram.cpp

 

 

:在任何时候,使用库的程序只允许一个能运行。

 

赛题Blackbox的官方版

 

范例操作过程:

函 数 调 用

函 数 返 回 值

 

Initialize(0);

 

假设使用了我们前面图1所示的箱子,返回之值为5,意味n=5

PASCAL

throwBall(3,4,holeIn,holeOut);

C

throwBall(3,4,&holeIn,&holeOut);

C++

throwBall(3,4,&holeOut,&sideOut);

 

将球投入箱子左侧从上向下数第3个孔,

返回值为1,表示球击中1个偏转器,

当函数返回时,holeOut=2,sideOut=3,这表示球从底边(sideOut=3)从左向右数的第2孔(holeOut=2)退出黑箱。

 

 

试验

如果您把一个值=0的整数传给初始化函数initialize,函数库将从文件blackbox.in内来读区箱子的参数。这样您就能对库进行试验。文件blackbox.in的格式描述如下:

 

blackbox.in

说 明

5

3

2 3 /

4 2 /

4 4 /

 

Line1: 包含各边的孔数n.

Line2: 代表包含在箱内的偏转器数目d.

其余d:箱子中每一个偏转器都要1行,每一行包含由2个空格分隔开的3个参数,其中前2个分别代表相应偏转器所在的列和行,最后一个参数是符号’/'’/’,它们分别代表偏转器的两种不同初始位置。

 

 

注:

在文件blackbox.in中描述5之下3 2 3 / 4 2/4 4/线1:线2:包含表明偏转器的数量在箱子的一个整数d。下条d线:那里必须是一条线为每个偏转器在箱子。各条线包含代表专栏和偏转器列各自的二个空间被分离的整数,并且一个字符被分离以空间可能是或`/'的第二个代表偏转器的原始位置的整数或` / '