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 ...
随机推荐
- HBase集群搭建
HBase集群搭建 搭建环境:假设我们的linux环境已经准备好,包括网络.JDK.防火墙.主机名.免密登录等都没有问题,而且一定要有zookeeper.下面我们用3台linux虚拟机来搭建Hbase ...
- go语言实战向导
版权声明:本文由魏佳原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/173 来源:腾云阁 https://www.qclou ...
- java 集合2(迭代器)
迭代器方法:(把迭代器想象成抓娃娃机的爪子) hasNext() 问是否有元素可遍历,如果有元素可以遍历,返回true,否则返回false 工作原理:这一个迭代的过程是这样的,获取到迭代器时候 ...
- html5中插入视频和音频
<audio src="1.mp3" controls></audio> <video src="1.mp4" controls& ...
- Form1和Form2的交互
比如在第二个窗体中操作第一个窗体中的TreeView,动态添加节点和子节点. ------回答--------- ------其他回答(20分)--------- 尽量不要这样做.控件,窗体,你在.n ...
- nodeschool.io 7
~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...
- Xcode 8 打包上线 iTunes Connect 找不到构建版本
Xcode 8 打包上线 iTunes Connect 找不到构建版本 最近苹果推出新的mac操作系统(macOS Sierra 10.12),大家可能都已经升级了,作为一个开发者,小编肯定是第一时间 ...
- 关于gridview 实现查询功能的方法
protected void btnSearch_Click(object sender, EventArgs e) { TestCon(); } protected void btnAllData_ ...
- java下载网络图片
import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IO ...
- 多线程相关Interlocked.Increment问题
今天群里有人问到如下代码打印出来的东西为什么不是连续得,所以有大神解释了原因.在这过程中遇到了些奇怪的情况 static void Main(string[] args) { for (int i = ...