/*
有一个停车楼,停车楼里的车需要全部开出去,标号为-1的位置为空,其他的数字为汽车,汽车出库必须要按照数字从小到大的顺序,
停车楼出口为第一层第一个 位置,其中电梯在每一行的第一列, 每一行是一个环形传送带,每移动一格耗费5分钟,每移动一层消耗10分钟,
电梯每上楼接车,到达每一层接车,然后下去从开头出口出去。 问你最少耗费多长时间所有的车都可以出去。
*/
题意好难,思路不难。
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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int map[55][55];
struct Node{
int x;
int y;
}edge[50*52];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,j,h,l;
int sum = 0;
int count = 0;
scanf("%d%d",&h,&l);
for(i=1;i<=h;i++)
{
map[i][0] = 1;
for(j=1;j<=l;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j] != -1)
{
edge[map[i][j]].x = i;
edge[map[i][j]].y = j;
if(map[i][j] > count){
count = map[i][j];
}
}
}
}
for(i=1;i<=count;i++)//汽车从小到大出库
{
int x = edge[i].x;
int y = edge[i].y;
sum += ((x - 1)* 10 * 2);//电梯 上楼接 再回去从出口把车送出去 ,所以结果*2
int res = min(abs(y - map[x][0]),l - abs(y - map[x][0])); //在同行中 找距离左右两边最近的那个。
sum += res * 5;
map[x][0] = y;
}
printf("%d\n",sum);
}
return 0;
}