【水题】UVA 512 Spreadsheet Tracking

本文介绍了一个C++程序,用于模拟电子表格上的操作,包括单元格交换、行列的插入和删除等,并通过一系列操作后查询单元格的新位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

给你r行c列的电子表格,给你n个操作,具体来说一共有五种操作:
EX r1 c1 r2 c2交换单元格(r1,c1),(r2,c2).
A x1 x2 …… xA 插入或删除A行或列(DC-删除列, DR-删除行, IC-插入列, IR-插入行, 1 <= A <= 10)。
接下来q个查询,给你原始的单元格(r0, c0)输出原始的,输出操作后的。

代码:

#include<bits/stdc++.h>
using namespace std;
struct node
{
    char c[5];//存操作字符串
    int r1, c1, r2, c2;//交换的两个单元
    int x[20], m;//存对应m个数
};
node a[10000];
int n;
int Move(int *r0, int *c0)
{
    int i, j;
    for(i = 0; i < n; i++)
    {
        if(a[i].c[0] == 'E')//交换两个单元
        {
            if(a[i].r1 == *r0 && a[i].c1 == *c0) {//下标变换一下
                *r0 = a[i].r2; *c0 = a[i].c2;
            }
            else if(a[i].r2 == *r0 && a[i].c2 == *c0)
            {
                *r0 = a[i].r1; *c0 = a[i].c1;
            }
        }
        else
        {
            int dr = 0, dc = 0;
            for(j = 0; j < a[i].m; j++)
            {
                int x = a[i].x[j];
                if(a[i].c[0] == 'I')//插入
                {
                    if(a[i].c[1] == 'R')//插入行
                    {
                        if(x <= *r0) dr++;//插入的行在*r0上边, 所以*r0++
                    }
                    if(a[i].c[1] == 'C')//插入列
                    {
                        if(x <= *c0) dc++;
                    }
                }
                else//删除
                {
                    if(a[i].c[1] == 'R')//删除行
                    {
                        if(x == *r0) return 0;//一样删除了,返回0
                        else if(x < *r0) dr--;//删除行在*r0上边, 所以*r0--
                    }
                    else
                    {
                        if(x == *c0) return 0;
                        else if(x < *c0) dc--;
                    }
                }
            }
            *r0 += dr; *c0 += dc;
        }
    }
    return 1;
}
int main()
{
    int r, c, r0, c0;
    int i, j, q, cas = 0;
    while(~scanf("%d %d %d", &r, &c, &n))
    {
        if(!r && !c) break;
        for(i = 0; i < n; i++)
        {
            scanf("%s", a[i].c);
            if(a[i].c[0] == 'E')
            {
                scanf("%d %d %d %d", &a[i].r1, &a[i].c1, &a[i].r2, &a[i].c2);
            }
            else
            {
                scanf("%d", &a[i].m);
                for(j = 0; j < a[i].m; j++)
                    scanf("%d", &a[i].x[j]);
            }
        }
        scanf("%d", &q);
        if(cas > 0) printf("\n");
        printf("Spreadsheet #%d\n", ++cas);
        while(q--)
        {
            scanf("%d %d", &r0, &c0);
            printf("Cell data in (%d,%d) ", r0, c0);
            if(!Move(&r0, &c0)) printf("GONE\n");
            else
            {
                printf("moved to (%d,%d)\n", r0, c0);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值