# Poj第1083题–Moving Tables

## 原题

Moving Tables

Description

The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.

The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving.

### 解题思路

```将一组测试数据存放到一张二维数组m[1...n][2]中，其中m[1...n][0]存放搬桌子的起始点，m[1...n][1]存放搬桌子的终点。

### 算法实现

```import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {

public static void main(String[] args) throws NumberFormatException,
IOException {
int s;  //s张桌子将搬动
int[][] m; //存放几组搬桌子起点终点的房间号

int[] corridors = new int[200]; //过道下标为k(对应的房间号为2k-1和2k+2),其值表示桌子经过过道的次数
int start; //开始的过道编号
int len;   //搬桌子的距离
int max;
for (int i = 0; i < t; i++) {
Arrays.fill(corridors, 0);  //对每组数据都重新初始化
m = new int[s][2];
for (int j = 0; j < s; j++) {
}

//遍历所有桌子，更新corridors数组中的值
for (int j = 0; j < s; j++) {
//将房间号转化为对应的过道号
if (m[j][0] % 2 == 0) {
m[j][0] = m[j][0] / 2 - 1;
} else {
m[j][0] = m[j][0] / 2;
}
if (m[j][1] % 2 == 0) {
m[j][1] = m[j][1] / 2 - 1;
} else {
m[j][1] = m[j][1] / 2;

}

len = Math.abs(m[j][0] - m[j][1]) + 1;
start = Math.min(m[j][0], m[j][1]);
for (int k = 0; k < len; k++) {
corridors[start + k]++;
}
}

//找到某个搬桌子过程中经过次数最多的过道号，找到此最大的次数
max = corridors[0];
for (int j = 1; j < 200; j++) {
if (corridors[j] > max) {
max = corridors[j];
}
}
System.out.println(max * 10);
}
}
}```

43 篇文章27 人订阅

0 条评论

## 相关文章

44550

29630

### 函数式编程简介

1900年，Hilbert 提出了数学界悬而未决的10大问题，后续陆续添加成了23个问题，被称为著名的 Hilbert 23 Problem。针对其中第2个决定...

23040

38490

34370

21940

17470

30680

25680

5710