【UVA 512 --- Spreadsheet Tracking】模拟

【UVA 512 --- Spreadsheet Tracking】模拟

题目来源:点击进入【UVA 512 — Spreadsheet Tracking】

Description

Data in spreadsheets are stored in cells, which are organized in rows ® and columns ©. Some
operations on spreadsheets can be applied to single cells (r, c), while others can be applied to entire
rows or columns. Typical cell operations include inserting and deleting rows or columns and exchanging
cell contents.
Some spreadsheets allow users to mark collections of rows or columns for deletion, so the entire
collection can be deleted at once. Some (unusual) spreadsheets allow users to mark collections of rows
or columns for insertions too. Issuing an insertion command results in new rows or columns being
inserted before each of the marked rows or columns. Suppose, for example, the user marks rows 1 and
5 of the spreadsheet on the left for deletion. The spreadsheet then shrinks to the one on the right.

Input

在这里插入图片描述

Output

在这里插入图片描述

Sample Input

7 9
5
DR 2 1 5
DC 4 3 6 7 9
IC 1 3
IR 2 2 4
EX 1 2 6 5
4
4 8
5 5
7 8
6 5
0 0

Sample Output

Spreadsheet #1
Cell data in (4,8) moved to (4,6)
Cell data in (5,5) GONE
Cell data in (7,8) moved to (7,6)
Cell data in (6,5) moved to (1,2)

AC代码:

#include <stdio.h>
#include <string.h>
const int MAXN = 105;
int map[MAXN][MAXN],ans[MAXN][MAXN],tmp[MAXN][MAXN];
bool vis[MAXN];
int r,c,n,t;

void copy(char type,int x,int y)
{
    if(type=='R') 
        for(int i=1;i<=c;i++)
            map[x][i]=tmp[y][i];
    else
        for(int i=1;i<=r;i++)
            map[i][x]=tmp[i][y];
}

void del(char type)
{
    memcpy(tmp,map,sizeof(map));
    int t= type=='R' ? r : c;
    int num=1;
    for(int i=1;i<=t;i++)
        if(!vis[i]) 
            copy(type,num++,i);
    if(type=='R') r=num-1;
    else c=num-1;
}

void insert(char type)
{
    memcpy(tmp,map,sizeof(map));
    int t= type=='R' ? r : c;
    int num=1;
    for(int i=1;i<=t;i++)
    {
        if(vis[i]) copy(type,num++,0);
        copy(type,num++,i);
    }
    if(type=='R') r=num-1;
    else c=num-1;
}

int main()
{
    char s[10];
    int cas=0;
    while(scanf("%d%d",&r,&c), r)
    {
        scanf("%d",&n);
        for(int i=1;i<=r;i++)
            for(int j=1;j<=c;j++)
                map[i][j]=i*MAXN+j;
        while(n--)
        {
            scanf("%s",s);
            if(s[0]=='E') 
            {
                int x1,x2,y1,y2,t;
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                t=map[x1][y1]; map[x1][y1]=map[x2][y2]; map[x2][y2]=t;
            }
            else
            {
                int x;
                memset(vis,false,sizeof(vis));
                scanf("%d",&t);
                while(t--) 
                {
                    scanf("%d",&x);
                    vis[x]=true;
                }
                if(s[0]=='D') del(s[1]);
                else insert(s[1]);
            }
        }
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=r;i++)
        {
            for(int j=1;j<=c;j++)
            {
                int x=map[i][j]/MAXN;
                int y=map[i][j]%MAXN;
                ans[x][y]=i*MAXN+j;
            }
            
        }
        if(cas++) printf("\n");
        printf("Spreadsheet #%d\n", cas);
        scanf("%d", &t);
        while(t--)
        {
            int x,y;
            scanf("%d%d", &x, &y);
            printf("Cell data in (%d,%d) ", x, y);
            if(ans[x][y] == 0) printf("GONE\n");
            else printf("moved to (%d,%d)\n", ans[x][y]/MAXN, ans[x][y]%MAXN);
        }
    }
    return 0;
}
发布了412 篇原创文章 · 获赞 135 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/104080075