hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat) 分类: hdoj 2015-06-16 12:33 28人阅读 评论(0) 收藏
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.
hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat) 分类: hdoj 2015-06-16 12:33 28人阅读 评论(0) 收藏的更多相关文章
- hdu 1030 Delta-wave (C++, 0ms, explanatory comments.) 分类: hdoj 2015-06-15 12:21 45人阅读 评论(0) 收藏
problem description http://acm.hdu.edu.cn/showproblem.php?pid=1030 #include <cstdio> #include ...
- hdu 1051 (greedy algorithm, how a little modification turn 15ms to 0ms) 分类: hdoj 2015-06-18 12:54 29人阅读 评论(0) 收藏
the 2 version are essentially the same, except version 2 search from the larger end, which reduce th ...
- hdu 1050 (preinitilization or postcleansing, std::fill) 分类: hdoj 2015-06-18 11:33 34人阅读 评论(0) 收藏
errors, clauses in place, logical ones, should be avoided. #include <cstdio> #include <cstr ...
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1039 (string process, fgets, scanf, neat utilization of switch clause) 分类: hdoj 2015-06-16 22:15 38人阅读 评论(0) 收藏
(string process, fgets, scanf, neat utilization of switch clause) simple problem, simple code. #incl ...
- hdu 1057 (simulation, use sentinel to avoid boudary testing, use swap trick to avoid extra copy.) 分类: hdoj 2015-06-19 11:58 25人阅读 评论(0) 收藏
use sentinel to avoid boudary testing, use swap trick to avoid extra copy. original version #include ...
- hdu 1033 (bit masking, utilization of switch, '\0' as end of c string) 分类: hdoj 2015-06-15 21:47 37人阅读 评论(0) 收藏
bit masking is very common on the lower level code. #include <cstdio> #include <algorithm&g ...
随机推荐
- 微信小程序初体验(上)
版权声明:本文由练小习原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/204 来源:腾云阁 https://www.qclo ...
- Android 子activity关闭 向父activity传值
使用startActivity方式启动的Activity和它的父Activity无关,当它关闭时也不会提供任何反馈. 可变通的,你可以启动一个Activity作为子Activity,它与父Activi ...
- DSP28377S - ADC学习编程笔记
DSP28377S - ADC学习编程笔记 彭会锋 2016-08-04 20:19:52 1 ADC类型导致的配置区别 F28377S的ADC类型是Type 4类型,我的理解是不同类型的ADC采 ...
- 在excel单元格中提取信息
平时在excel中处理数据的时候,肯定会遇到在单元格提取信息的情况,比如在地址中提取省.市.地区等,如果数据源内容规整的话,可以直接使用left().right().mid()等函数直接提取,但是大多 ...
- js--题目二
//如何获取下面代码所有元素. <form> <input name="" type="text" /> <input name= ...
- 数据库中User和Schema的关系
如果我们想了解数据库中的User和Schema到底什么关系,那么让我们首先来了解一下数据库中User和Schema到底是什么概念. 在SQL Server2000中,由于架构的原因,Us ...
- 198. 213. 337. House Robber -- 不取相邻值的最大值
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- 新建txt文件新增内容并打印出
#!/usr/bin/python import os file1=open("C:\Python34\ceshi.txt","a+"); #a+开一个文件用 ...
- MASS批量维护
T-CODE: MASS 批量更改MASS_CHARVAL 特征的批量维护MASS_EINE 信息记录的成批维护MASS_EKKO 采购订单的成批维护MASS_MARC 后勤/配送的成批维护MASS_ ...
- backbonejs中的模型篇(二)
一:模型标识符 每个模型都有一个用作唯一标识符的ID属性,以便在不同模型间进行区分.通过id属性我们可以直接访问模型对象当中用于标识符存放的属性,默认属性名为id,但也可以通过设置idAttribut ...