简单题
注意电梯间在每层的first place。而且电梯和传送带不会同时工作以节约时间。二者只能一个动完另一个动
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 55
struct Car
{
int x, y;
}car[maxn * maxn];
int n, m, tot;
int pos[maxn];
void input()
{
scanf("%d%d", &n, &m);
tot = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
int a;
scanf("%d", &a);
if (a == -1)
continue;
car[a].x = i;
car[a].y = j;
tot++;
}
}
int dist(int a, int b)
{
return min(abs(a - b), m - abs(a - b));
}
void work()
{
memset(pos, 0, sizeof(pos));
int ans = 0;
for (int i = 1; i <= tot; i++)
{
ans += car[i].x * 20;
int temp = dist(car[i].y, pos[car[i].x]);
ans += abs(temp) * 5;
pos[car[i].x] = car[i].y;
}
printf("%d\n", ans);
}
int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
input();
work();
}
return 0;
}