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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.