UVA 12652 Lines of Containers

来源:互联网 发布:spss因子载荷矩阵 编辑:程序博客网 时间:2024/05/23 16:50

刚开始看到这题的时候 还以为是一个经典的搜索题目 后来才发现原来我想错了...

题意就是给你一个L*C的矩阵 问你最少经过多少步能变成123456789...的有序的矩阵 每次操作可以选两行交换顺序 或者选两列交换顺序

若无法变成初始矩阵 输出"*"

由于初始矩阵是有序的 每一行的元素也是固定的 而无论经过多少次操作 原本在同一行的数字一定最终也在同一行, 列同理   于是就可以根据数字的大小判断他原本应该在第几行第几列

#include <bits/stdc++.h>using namespace std;int a[310][310], visr[310], vis[310], n, m, ans;void swapr(int x, int y) {    ans++;    for(int i = 1; i <= m; i++) swap(a[x][i], a[y][i]);}void swapc(int x, int y) {    ans++;    for(int i = 1; i <= n; i++) swap(a[i][x], a[i][y]);}int main() {    //freopen("New Text Document.txt", "r", stdin);    while(~scanf("%d%d", &n, &m)) {        int flag = 1;        ans = 0;        memset(visr, -1, sizeof visr);        for(int i = 1; i <= n; i++) {            for(int j = 1; j <= m; j++) {                scanf("%d", &a[i][j]);                int t = (a[i][j] + m - 1) / m;                if(visr[t] == -1) visr[t] = i;                else if(visr[t] != i) flag = 0;            }        }        if(!flag) {            puts("*");            continue;        }        memset(vis, 0, sizeof vis);        for(int i = 1; i <= n; i++) {            if(!vis[i]) {                int t = visr[i];                vis[i] = 1;                while(t != i) {                    swapr(t, visr[t]);                    vis[t] = 1;                    t = visr[t];                }            }        }        for(int i = 1; i <= m; i++) visr[a[1][i]] = i;        memset(vis, 0, sizeof vis);        for(int i = 1; i <= m; i++) {            if(!vis[i]) {                int t = visr[i];                vis[i] = 1;                while(t != i) {                    swapc(t, visr[t]);                    vis[t] = 1;                    t = visr[t];                }            }        }        for(int i = 1; i <= n; i++) {            for(int j = 1; j <= m; j++) {                if(a[i][j] != j + (i - 1) * m) flag = 0;            }        }        if(!flag) {            puts("*");        }        else printf("%d\n", ans);    }    return 0;}

题目:

12652 Lines of Containers
A shipment of Nlogs, the main export product from Nlogonia, is in the harbour, in containers, ready
to be shipped. All containers have the same dimensions and all of them are cubes. Containers are
organized in the cargo terminal in L lines and C columns, for a total of LC containers. Each container
is marked with a distinct identification number, from 1 to LC.
Each one of the L container lines will be loaded in a different ship. To make it simpler when
unloading at each destination country, containers from a line must be organized such that the identifiers
are in sequential order. More precisely, the first line must have the containers identified from 1 to C in
increasing order, line 2 must have containers numbered from C + 1 to 2C (in increasing order), and so
on until the last line. which will have containers numbered (L − 1)C + 1 to LC (again, in increasing
order). Figure (a) shows the organization of a shipment with 5 lines and 4 columns of containers.
A crane is able to move either a full line or a full column of containers. It cannot move other
groupments or individual containers.
In night before the loading, a group of workers operated the cranes to swap shipment lines and
columns as a way of protest because of low salaries. Figure (b) shows the configuration after swapping
lines 1 and 4; Figure (c) shows the configuration after another swap, this time between colummns 2
and 3.
The loading must be done today, but before starting the containers must be reorganized as described
previously. You must write a program which, given the information about the position of every container
after the protest, determines whether you can reposition the containers in such way that every one of
them is in its expected positions, using only cranes. If repositioning is possible, you must also calculate
the smallest number of column and line swaps needed to do it.
Input
The input contains several test cases. The first line of of a test case contains two integers L and C
which describe, respectively, the number of lines and columns of the shipment. The next L lines show
the configuration of the containers after the workers protest. Each of these L lines will have C integers
Xl,c , indicating the position of a container. Every integer between 1 and LC appears exactly once in
the input.
Output
For each test case your program must produce a single line, containing a single integer, the minimum
number of column and line swaps needed to place the containers in their original positions. If there isUniversidad de Valladolid OJ: 12652 – Lines of Containers 2/2
no way to place the containers in their original positions using only cranes, the line must contain only
the character ‘*’.
Restrictions
• 1 ≤ L ≤ 300
• 1 ≤ C ≤ 300
• 1 ≤ Xl,c ≤ LC
Sample Input
2 2
3 4
1 2
3 3
9 2 4
5 8 7
6 1 3
5 4
13 15 14 16
5 7 6 8
9 11 10 12
1 3 2 4
17 19 18 20
Sample Output
1
*
2

0 0