uva 512

这是一道字符串处理的题目。但通过自己写的和书上写的代码对比,也是学到一些东西。

我一开始想存储一个点变换后的坐标需要两个值(x,y),再加上一个bool类型的变量判断这个点有没有被删去即可。因此写出代码如下:

#include<stdio.h>
#include<string.h>
#define maxn 55

struct point{
    int ansx;
    int ansy;
    bool exist;
};
point pt[maxn][maxn];

int r,c,n;

void DC(int x){
    for(int i = 1;i<=r;i++){
        for(int j=1;j<=c;j++){
            if(pt[i][j].ansy==x) pt[i][j].exist = false;
            else if(pt[i][j].ansy>x) pt[i][j].ansy--;
            else{}
        }
    }
} 

void DR(int x){
    for(int i = 1;i<=r;i++){
        for(int j=1;j<=c;j++){
            if(pt[i][j].ansx==x) pt[i][j].exist = false;
            else if(pt[i][j].ansx>x) pt[i][j].ansx--;
            else{}
        }
    }
}

void IC(int x){
    for(int i = 1;i<=r;i++){
        for(int j=1;j<=c;j++){
            if(pt[i][j].ansy>=x) pt[i][j].ansy++ ;
        }
    }
}

void IR(int x){
    for(int i = 1;i<=r;i++){
        for(int j=1;j<=c;j++){
            if(pt[i][j].ansx>=x) pt[i][j].ansx++ ;
        }
    }
}

int main(){
    int i,j,r1,r2,c1,c2,q,kase=0;
    char cmd[15];
    while(scanf("%d%d%d",&r,&c,&n)&&r){
        for(i=1;i<=r;i++){
            for(j=1;j<=c;j++){
                pt[i][j].ansx = i;
                pt[i][j].ansy = j;
                pt[i][j].exist = true;
            }
        }
        while(n--){
            scanf("%s",cmd);
            if(cmd[0] == ‘E‘){
                scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
                int tmpx = pt[r1][c1].ansx,tmpy = pt[r1][c1].ansy;
                pt[r1][c1].ansx = pt[r2][c2].ansx;pt[r1][c1].ansy = pt[r2][c2].ansy;
                pt[r2][c2].ansx = tmpx;pt[r2][c2].ansy = tmpy;
            }
            else if(cmd[0] == ‘D‘ && cmd[1] == ‘C‘){
                int a,x;
                scanf("%d",&a);
                while(a--){
                    scanf("%d",&x);
                    DC(x);
                }
            }
            else if(cmd[0] == ‘D‘ && cmd[1] == ‘R‘){
                int a,x;
                scanf("%d",&a);
                while(a--){
                    scanf("%d",&x);
                    DR(x);
                }
            }
            else if(cmd[0] == ‘I‘ && cmd[1] == ‘C‘){
                int a,x;
                scanf("%d",&a);
                while(a--){
                    scanf("%d",&x);
                    IC(x);
                }
            }
            else{
                int a,x;
                scanf("%d",&a);
                while(a--){
                    scanf("%d",&x);
                    IR(x);
                }
            }
        }
        if (kase>0) printf("\n");
        printf("Spreadsheet #%d\n",++kase);
        scanf("%d",&q);
        while(q--){
            scanf("%d%d",&r1,&c1);
            printf("Cell data in (%d,%d) ",r1,c1);
            if(!pt[r1][c1].exist) printf("GONE\n");
            else printf("move to (%d,%d)\n",pt[r1][c1].ansx,pt[r1][c1].ansy);
        }
    }
    return 0;
}

以上代码有许多重复的地方。而且因为每次搜索一个点变换后坐标都要遍历整个结构体数组,非常麻烦。看了书之后,学到了用一个值表示一对坐标的方法。

d(x,y)=x*BIG+y(BIG是一个大数,至少要大于列数的最大值maxn)

使用这样的方法能够使算法更加简洁一些。

ps:注意正确的使用memset,防止结果错误=。=

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define maxn 100
#define BIG 1000
using namespace std;

int d[maxn][maxn],d2[maxn][maxn],r,c,n,line[maxn],ans[maxn][maxn]; 

void del(char type){
    //cout<<"I am in"<<endl;
    int cnt = 0,i,j;
    memcpy(d2,d,sizeof(d));
    if(type == ‘R‘){
        for(i=1;i<=r;i++){
            if(line[i]) continue;
            cnt++;
            for(j=1;j<=c;j++){
                d[cnt][j] = d2[i][j];
            }
        }
        r=cnt;
    }
    else{
        for(j=1;j<=c;i++){
            if(line[j]) continue;
            cnt++;
            for(i=1;i<=r;j++){
                d[i][cnt] = d2[i][j];
            }
        }
        c=cnt;
    }
}

void ins(char type){
    //cout<<"I am in"<<endl;
    int cnt = 0,i,j;
    memcpy(d2,d,sizeof(d));
    if(type == ‘R‘){
        for(i=1;i<=r;i++){
            if(line[i]) {
                cnt++;
                for(j=1;j<=c;j++) d[cnt][j] = 0;
            }
            cnt++;
            for(j=1;j<=c;j++){
                d[cnt][j] = d2[i][j];
            }
        }
        r=cnt;
    }
    else{
        //cout<<"I am in"<<endl;
        for(j=1;j<=c;j++){
            if(line[j]) {
                cnt++;
                for(i=1;i<=r;i++) d[i][cnt] = 0;
            }
            cnt++;
            for(i=1;i<=r;i++){
                d[i][cnt] = d2[i][j];
            }
        }
        c=cnt;
    }
}

int main(){
    int i,j,r1,r2,c1,c2,kase=0,q;
    char cmd[10];
    while(scanf("%d%d%d",&r,&c,&n) && r){
        memset(d,0,sizeof(d));
        for(i=1;i<=r;i++){
            for(j=1;j<=c;j++)
                d[i][j] = i*BIG + j;
        }
        while(n--){
            scanf("%s",cmd);
            if(cmd[0] == ‘E‘){
                scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
                int tmp = d[r1][c1];d[r1][c1] = d[r2][c2];d[r2][c2] = tmp;
            }
            else{
                int a,x;
                scanf("%d",&a);
                memset(line,0,sizeof(line));
                for(i=0;i<a;i++) {
                    scanf("%d",&x);
                    line[x] = 1;
                }
                //cout<<x<<endl;
                if(cmd[0] == ‘D‘) del(cmd[1]);
                else ins(cmd[1]);
            }
        }
        memset(ans,0,sizeof(ans));
        for(i=1;i<=r;i++){
            for(j=1;j<=c;j++)
                ans[d[i][j]/BIG][d[i][j]%BIG]= i*BIG + j;
        }
        if(kase>0) printf("\n");
        printf("Spreadsheet #%d\n",++kase);
        scanf("%d",&q);
        while(q--){
            scanf("%d%d",&r1,&c1);
            printf("Cell data in (%d,%d) ",r1,c1);
            if(ans[r1][c1]==0) printf("GONE\n");
            else printf("move to (%d,%d)\n",ans[r1][c1]/BIG,ans[r1][c1]%BIG);
        }
    }
    return 0;
} 
时间: 2024-08-05 18:40:22

uva 512的相关文章

UVa 512 模拟!

背景:1--wa:最后一组输出不要空行!. 思路:我的思路,就是模拟整个表,把表实行操作之后的形态表示出来,把原表中的数据再在已经模拟的表中去查询.书上的思路是,先把一系列的操作保存在一个结构体数组中,每一个结构体数组元素对应一个操作,最后对于每一个坐标的系统执行这一套操作,就可以得出变化好的坐标!这种方法可能只要知道操作结构体的思想,写起来更容易. 学习:1.写完之后查一遍代码,比去单步调试效果好,输出中间值来调试,效果也很好. 我的代码: #include<stdio.h> #includ

uva 512 追踪电子表格中的单元格

 Spreadsheet Tracking  Data in spreadsheets are stored in cells, which are organized in rows (r) and columns (c). Some operations on spreadsheets can be applied to single cells (r,c), while others can be applied to entire rows or columns. Typical cel

[2016-02-08][UVA][679][Dropping Balls]

UVA - 679 Dropping Balls Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A number of K balls are dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped f

uva 11137 Ingenuous Cubrency (完全背包)

uva 11137 Ingenuous Cubrency People in Cubeland use cubic coins. Not only the unit of currency is called a cube but also the coins are shaped like cubes and their values are cubes. Coins with values of all cubic numbers up to 9261 (= 21 3), i.e., coi

UVA 297 Quadtrees(四叉树建树、合并与遍历)

<span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">K - </span><span style="color: blue; font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color

uva 297 - Quadtrees

 Quadtrees  A quadtree is a representation format used to encode images. The fundamental idea behind the quadtree is that any image can be split into four quadrants. Each quadrant may again be split in four sub quadrants, etc. In the quadtree, the im

UVA - 11752 The Super Powers

We all know the Super Powers ofthis world and how they manage to get advantages in political warfare or evenin other sectors. But this is not a political platform and so we will talkabout a different kind of super powers – "The Super Power Numbers&qu

UVa 568 Just the Facts

A过去后看了一下别人的解法,发现除了打表还有一种数论的方法. 分析一下阶乘后面的0是怎么出现的呢,当然是2乘5得到的. 我们将1~N先放在一个数组里面. 从数组第一个元素开始,先统计一下N!中因子为5的个数记为count,将其除去,然后再除去count个2.这样一来的话把所有元素乘起来后就不会出现10的倍数了. 当然并不是真正的乘起来,那样的话肯定是要溢出的,因为只关心最后一位数,所以每次乘完后求10的余数即可. 我的做法是打表,因为题目里给了N <= 10000的条件限制,所以可以把1~100

UVA Quadtrees

题目如下: Quadtrees  A quadtree is a representation format used to encode images. The fundamental ideabehind the quadtree is that any image can be split into four quadrants. Each quadrant mayagain be split in four sub quadrants, etc. In the quadtree, the