UVA512 Spreadsheet Tracking Java版

本文介绍了一种用于模拟电子表格操作的算法,包括行和列的删除、插入以及单元格交换等复杂操作。通过实例演示了如何使用Java实现这些功能,并保持行和列数量在操作过程中的有效性。

原文题目

输入输出格式

样例

题目大意

有一个r行c列(1≤r,c≤50)的电子表格,行从上到下编号为1~r,列从左到右编号为 1~c。如图(a)所示,如果先删除第1、5行,然后删除第3,6,7,9列,结果如图(c)所示。

接下来在第2、3、5行前各插入一个空行,然后在第3列前插入一个空列, 会得到如图(e)的结果。 你的任务是模拟这样的n个操作。具体来说一共有5种操作

  • EX r1 c1 r2 c2交换单元格(r1,c1),(r2,c2)
  • DC-删除列
  • DR-删除行
  • IC插入列
  • IR-插入行

在插入删除指令后,各个x值不同,且顺序任意。接下来是q个查询,每个查询格 为“r c”,表示查询原始表格的单元格(r,c)。对于每个查询,输出操作执行完后该单元格 新位置。输入保证在任意时刻行列数均不超过50。

图表从上到下从左到右分别为图(a)-(e)具体见原文题目

代码

import java.util.Scanner;

public class Main {
    static int r, c;
    final static int MAX = 101;
    final static int BIG = 1000;
    static int[][] p0 = new int[MAX][MAX]; // 原数组
    static int[][] p1 = new int[MAX][MAX]; // 辅助数组
    static int[][] p2 = new int[MAX][MAX]; // ans
    static int[] cols = new int[MAX];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = 0;
        while (true) {
            r = sc.nextInt();
            c = sc.nextInt();
            if (r == 0 || c == 0)
                break;
            for (int i = 1; i <= r; i++)
                for (int j = 1; j <= c; j++)
                    p0[i][j] = i * BIG + j;
            int n = sc.nextInt();
            sc.nextLine();
            while (n-- > 0) {
                char[] c1 = sc.next().toCharArray();
                if (c1[0] == 'E') {
                    int a = sc.nextInt(),b= sc.nextInt(),c=sc.nextInt(),d=sc.nextInt();
                    int t = p0[a][b];
                    p0[a][b] = p0[c][d];
                    p0[c][d] = t;
                } else {
                    int a = sc.nextInt() ;
                    cols = new int[MAX];
                    for (int i = 0; i < a; i++){
                        int x = sc.nextInt();
                        cols[x] = 1;
                    }
                    if (c1[0] == 'D')
                        del(c1[1]);
                    else
                        ins(c1[1]);
                }
            }
            p2 = new int[MAX][MAX];
            for (int i = 1; i <= r; i++)
                for (int j = 1; j <= c; j++) {
                    p2[p0[i][j] / BIG][p0[i][j] % BIG] = i * BIG + j;
                }
            if (k > 0)
                System.out.println();
            System.out.println("Spreadsheet #" + (++k));
            int q = sc.nextInt();
            while (q-- > 0) {
                int r1 = sc.nextInt(), c1 = sc.nextInt();
                System.out.print("Cell data in (" + r1 + "," + c1 + ") ");
                if (p2[r1][c1] == 0)
                    System.out.println("GONE");
                else
                    System.out.println("moved to (" + p2[r1][c1] / BIG + "," + p2[r1][c1] % BIG + ")");
            }
        }
    }

    static void cv() {
        for (int i = 1; i <= r; i++) {
            for (int j = 1; j <= c; j++) {
                p1[i][j] = p0[i][j];
            }
        }
    }

    static void copy(char t, int p, int q) {

        if (t == 'R') {
            for (int i = 1; i <= c; i++) {
                p0[p][i] = p1[q][i];
            }
        } else {
            for (int i = 1; i <= r; i++) {
                p0[i][p] = p1[i][q];
            }
        }
    }

    static void del(char t) { // 删除行或者列
        cv();
        int c1 = (t == 'R') ? r : c;
        int c2 = 0;
        for (int i = 1; i <= c1; i++) {
            if (cols[i] == 0)
                copy(t, ++c2, i);
        }
        if (t == 'R')
            r = c2;
        else
            c = c2;
    }

    static void ins(char t) { // 插入行或者列
        cv();
        int c1 = t == 'R' ? r : c;
        int c2 = 0;
        for (int i = 1; i <= c1; i++) {
            if (cols[i] != 0)
                copy(t, ++c2, 0);
            copy(t, ++c2, i);
        }
        if (t == 'R')
            r = c2;
        else
            c = c2;

    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值