第十届河南省acm省赛 Intelligent Parking Building

H: Intelligent Parking Building

时间限制: 1 Sec  内存限制: 128 MB
提交: 6  解决: 5
[提交][状态][讨论版]

题目描述

There is a new revolution in the parking lot business: the parking  building. The concept is simple: you drive your car into the elevator at the entrance of the building, and the elevator and conveyor belts drag the car to an empty parking spot, where the car remains until you pick it up. When you return, the elevator and conveyor belts move your car back to the entrance and you’re done.

The layout of the building is simple. There is one central elevator that transports the cars between the different floors. On each floor there is one giant circular conveyor belt on which the cars stand. This belt can move in clockwise and counterclockwise direction. When the elevator arrives on a floor, it becomes part of the belt so that cars can move through it.

At the end of the day the building is usually packed with cars and a lot of people come to pick them up. Customers are processed in a first come first serve order: the elevator is moved to the floor of the first car, the conveyor belt moves the car on the elevator, the elevator is moved down again, and so on. We like to know how long it takes before the last customer gets his car. Moving the elevator one floor up- or downwards takes 10 seconds and moving  the conveyor belt one position in either direction takes 5 seconds. 

输入

On the first line one positive number: the number of testcases, at most 30.  Each test case specifies:

  • One line with two integers h and l with 1 ≤ h ≤ 50 and 2 ≤ l ≤ 50: the height of the parking tower and the length of the conveyor belts.
  • h lines with l integers: the initial placement of the cars. The jth number on the ith line describes the jth position on the ith floor. This number is −1 if the position is empty, and r if the position is occupied by the rth car to pick up. The positive numbers form a consecutive sequence from 1 to the number of cars. The entrance is on the first floor and the elevator (which is initially empty) is in the first position. There is at least one car in the parking tower.

输出

For each test case generate a single line containing a single integer  that is the number of seconds before the last customer is served.

 

样例输入

 
3
1  5
1  -1  -1  -1  2 
1  5
2  -1  -1  -1  1 
3 6
-1  5  6  -1  -1  3
-1  -1  7  -1  2  9
-1  10  4  1  8  -1

样例输出

 
5
10
320

这道题其实就是找规律,

首先每一层的汽车都是一个环形,且顺时针逆时针都可以移动,另外需要注意的是,每当一层中有一个汽车发生移动时其他的汽车也随之移动

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
    int x;
    int y;
}qu1[60];//之中的x定义车辆所在层数,y定义车辆所在环形位置
int main()
{
    int k;
    cin>>k;
    while(k--)
    {
       int n,m;
       int a[60][60]={0};
       cin>>n>>m;
       int max1=0;
       for(int i=1;i<=n;i++){
           for(int j=1;j<=m;j++){
                cin>>a[i][j];
                if(a[i][j]!=-1){
                    qu1[a[i][j]].x=i;
                    qu1[a[i][j]].y=j+100*m;//因为在环形中距离1最远的是m/2+1的位置,例如m=6时,
                                           //距离1最远的都应该是4,距离是3个单位,5距离1是2个单位
                                           //6距离1是一个单位,所以这里使用顺时针旋转此层所有汽车都加上移动的距离数
                                           //逆时针旋转此层所有的汽车都减去移动距离数
                                           //所以这的j+100*m就是怕出现负数
                }
                if(max1<a[i][j])max1=a[i][j];
           }
       }
       int sum=0;
       for(int i=1;i<=max1;i++){
           int x1=qu1[i].x;
           int k1=qu1[i].y%m;
           if(k1==0)k1=m;
           sum+=(x1-1)*20;//因为得先从第一层上去接到车再送到第一层,所以应该*20
           int k2=min(k1-1,m+1-k1);//此处就是用来判断是否应该逆时针或者顺时针旋转
           sum+=k2*5;
           if(k1-1>m+1-k1){//把此层剩余车辆跟着旋转
               for(int j=1;j<=m;j++){
                   if(a[x1][j]>i)
                       qu1[a[x1][j]].y+=k2;
               }
           }
           else{
               for(int j=1;j<=m;j++){
                   if(a[x1][j]>i)
                       qu1[a[x1][j]].y-=k2;
               }
           }
       }
       cout<<sum<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值