码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 512 模拟!

时间:2015-01-27 21:59:25      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:uva   c++   acm   

背景:1——wa:最后一组输出不要空行!。

思路:我的思路,就是模拟整个表,把表实行操作之后的形态表示出来,把原表中的数据再在已经模拟的表中去查询。书上的思路是,先把一系列的操作保存在一个结构体数组中,每一个结构体数组元素对应一个操作,最后对于每一个坐标的系统执行这一套操作,就可以得出变化好的坐标!这种方法可能只要知道操作结构体的思想,写起来更容易.

学习:1.写完之后查一遍代码,比去单步调试效果好,输出中间值来调试,效果也很好。

我的代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
int map0[100][100],map[100][100];
int n0,m0,n,m,t;

bool cmp(int x,int y){return x<y;}

void ii(char direction){
    int t,cc[100],c;
    scanf("%d",&t);
    for(int i=t;i > 0;i--) scanf("%d",&cc[i-1]);
    sort(cc,cc+t,cmp);    //´Ó×î´óÐÐÊý¿ªÊ¼Ôö¼Ó£¬±ÜÃâ³öÏÖ»ìÂÒ¡£
    while(t--){
        c=cc[t];
        if(direction == 'R'){
            for(int i=n;i >= c;i--)
                for(int j=0;j < m;j++)
                     map[i][j]=map[i-1][j];
            for(int j=0;j < m;j++) map[c-1][j]=0;    //¿ÕÐÐÓÃ0À´Ìî³ä¡£
            n++;
        }else{
            for(int i=m;i >= c;i--)
                for(int j=0;j < n;j++)
                    map[j][i]=map[j][i-1];
            for(int j=0;j < n;j++) map[j][c-1]=0;
            m++;
        }							        
    }    
}

void d(char direction){
    int t,cc[100],c;
    scanf("%d",&t);
    for(int i=t;i > 0;i--) scanf("%d",&cc[i-1]);
    sort(cc,cc+t,cmp);
    while(t--){
        c=cc[t];
        if(direction == 'R'){
            for(int i=c-1;i < n-1;i++)
                for(int j=0;j < m;j++)
                    map[i][j]=map[i+1][j];
            n--;
		        }else{
		            for(int i=c-1;i < m-1;i++)
		                for(int j=0;j < n;j++)
		                    map[j][i]=map[j][i+1];
		            m--;
		        }
    }
}


void exchange(void){
    int x,y,z,w,temp;
    scanf("%d%d%d%d",&x,&y,&z,&w);
    temp=map[x-1][y-1];
    map[x-1][y-1]=map[z-1][w-1];
    map[z-1][w-1]=temp;
}

int main(void){
    int count=1;
    while(~scanf("%d%d",&n0,&m0) && n0 != 0){
    	  scanf("%d",&t);
    	  if(count-1)printf("\n");
    	  printf("Spreadsheet #%d\n",count++);
        for(int i=0,key=1;i < n0;i++)
            for(int j=0;j < m0;j++)
                map0[i][j]=map[i][j]=key++;
        n=n0;
        m=m0;
        while(t--){
            getchar();
            char a,b;
            scanf("%c%c",&a,&b);
            if(a == 'E') exchange();
            else if(a == 'I') ii(b);
            else d(b);
        }
        scanf("%d",&t);
        while(t--){
            int x0,y0,x,y;
            bool ok=false;
            scanf("%d%d",&x0,&y0);
            for(int i=0;i < n;i++){
                for(int j=0;j < m;j++){
                    if(map[i][j] == map0[x0-1][y0-1]){
                        ok=true;
                        x=i+1;
                        y=j+1;
                        break;
                    }
                }
                if(ok) break;
            }
            printf("Cell data in (%d,%d) ",x0,y0);
            if(ok) printf("moved to (%d,%d)\n",x,y);
            else printf("GONE\n");
        }
    }
    return 0;
}


UVa 512 模拟!

标签:uva   c++   acm   

原文地址:http://blog.csdn.net/jibancanyang/article/details/43201511

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!