hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat)

as Scott Meyers said in his book Effective STL,
“My advice on choosing among the sorting algorithms is to make your selection based on what you need to accomplish, not on performance considerations. If you choose an algorithm that does only what you need to do (e.g., a partition instead of a full sort), you’re likely to end up with code that’s not only the clearest expression of what you want to do, it’s also the most efficient way to accomplish it using the STL.”
Code the problem as it be maybe the most efficient way,
like the TEX principle, what you think is what you get.

#include <cstdio>
#include <algorithm>

#define MAXSIZE 12

char grid[MAXSIZE][MAXSIZE];

int main() {
    //freopen("input.txt","r",stdin);
    int nrow,ncol,ypos,xpos, i;
    char *p;
    while(scanf("%d%d%d",&nrow,&ncol,&ypos)!=EOF && nrow>0) {
        memset(grid,0,sizeof(grid));
        for(i=1;i<=nrow;++i) { scanf("%s",&grid[i][1]); }
        for(xpos=1,i=1;;++i) {
            p=&grid[xpos][ypos];
            switch(*p) {
            case 'E': ++ypos; *p=i; break;
            case 'W': --ypos; *p=i; break;
            case 'N': --xpos; *p=i; break;
            case 'S': ++xpos; *p=i; break;
            case '\0': goto EXITCODE0;
            default: goto EXITCODE1;
            }
        }
EXITCODE0: printf("%d step(s) to exit\n",i-1); continue;
EXITCODE1: printf("%d step(s) before a loop of %d step(s)\n",*p-1,i-*p); continue;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值