习题3-5 谜题 UVa227

来源:互联网 发布:apache svn服务器搭建 编辑:程序博客网 时间:2024/06/07 05:56
算法竞赛入门经典(第2版)第3章 数组和字符串
习题3-5 谜题 UVa227
感悟。
1、直接看英文原题的输入输出样例,在题意理解要求上省了许多力。

2、程序难在输入输出处理,难在字符读取,策略:写一段代码,跟踪调试一段,正确之后才往下写,其中发现不少错误,一气呵成,写出无误的代码,真的是很难很难啊。

3、对getchar()有更深入的理解。

4、平常心提交代码,没有奢望一次AC,看到的结果是Presentation error还好核心部分没问题,只要修改输出格式即可

5、直接阅读网站原文,限于英文水平有限,进一步参考http://blog.csdn.net/thudaliangrx/article/details/50699439对代码输出格式进行修改,提交AC.

6、对Separate output from different puzzle records by one blank line.有更深的理解,输出记录间被空行隔开,但第一条输出记录之前,最后一条输出记录之后,无空行。

7、用了结构体。

8、引入struct pos opos[4]={{-1,0},{1,0},{0,-1},{0,1}};//移动的数据 


附上代码

环境Dev-cpp4.9.9.2

#include <stdio.h>
char a[10][10];
char o[10000+10];
struct pos{
    int r;
    int c;
};
struct pos opos[4]={{-1,0},{1,0},{0,-1},{0,1}};//移动的数据 
struct pos mypos,tpos,tmppos;
int order(char c){
    switch(c){
        case 'A':
            return 0;
        case 'B':
            return 1;
        case 'L':
            return 2;
        case 'R':
            return 3; 
    }
}
int main(){
    char c;
    int rcount,ccount,ocount;
    int i,j,k;
    int rpos,cpos;
    int kase=0;
    char tmpc;
    int flag;
    int first;
    while((c=getchar())!='Z'){
        rcount=0;
        ccount=0;
        while(rcount<5){//将输入的字符串转化为5*5的网格 
            if(c=='\n'){
                if(rcount==4)
                    break;
                rcount++;
                ccount=0;
                c=getchar();
                continue;
            }
            a[rcount][ccount]=c;
            if(c==' '){
                mypos.r=rcount;
                mypos.c=ccount;
            }
            ccount++;
            c=getchar();
        }
          
        ocount=0;
        while((c=getchar())!='0'){//接收指令序列 
            if(c=='\n')
                continue;
            o[ocount]=c;
            ocount++;
        }
        c=getchar();//吞掉'0'之后的'\n' 
        kase++;
        if(kase>1)
            printf("\n");
        printf("Puzzle #%d:\n",kase);
        flag=1;
        for(i=0;i<ocount;i++){//处理在指令操作下的网格 
            k=order(o[i]);
            tpos.r=mypos.r+opos[k].r;
            tpos.c=mypos.c+opos[k].c;
            if(tpos.r<0||tpos.r>4||tpos.c<0||tpos.c>4){
                printf("This puzzle has no final configuration.\n");
                flag=0;
                break;
            }else{
                tmpc=a[mypos.r][mypos.c];
                a[mypos.r][mypos.c]=a[tpos.r][tpos.c];
                a[tpos.r][tpos.c]=tmpc;
                
                tmppos=tpos;
                tpos=mypos;
                mypos=tmppos;
            }
            
        }
        
        if(flag){
            for(i=0;i<5;i++){
                first=1;
                for(j=0;j<5;j++){
                    if(first){
                        first=0;
                        printf("%c",a[i][j]);
                    }else{
                        printf(" %c",a[i][j]);
                    }
                }
                printf("\n");
            }
        }
    }
    return 0;
}

0 0
原创粉丝点击