use sentinel to avoid boudary testing,

use swap trick to avoid extra copy.

original version

#include <cstdio>
#include <algorithm> int main() {
//freopen("input.txt","r",stdin);
const int MAXSIZE=22, dimSize=20;
int bacnums1[MAXSIZE][MAXSIZE]={0},bacnums2[MAXSIZE][MAXSIZE]={0}, (*now)[MAXSIZE]=bacnums1, (*next)[MAXSIZE]=bacnums2, dna[16];
int ncase, nday, i,j,tmp;
if(scanf("%d",&ncase)!=1 || ncase<=0) return -1;
while(ncase--) {
scanf("%d",&nday);
for(i=0;i<16;++i) scanf("%d",&dna[i]);
for(i=1;i<=dimSize;++i)
for(j=1;j<=dimSize;++j)
scanf("%d",&now[i][j]);
while(nday-->0) {
for(i=1;i<=dimSize;++i)
for(j=1;j<=dimSize;++j) {
tmp=now[i][j]+now[i-1][j]+now[i+1][j]+now[i][j-1]+now[i][j+1];
next[i][j]=now[i][j]+dna[tmp];
if(next[i][j]<0) { next[i][j]=0; }
else if(next[i][j]>3) { next[i][j]=3; }
}
std::swap(now,next);
}
for(i=1;i<=dimSize;++i) {
for(j=1;j<=dimSize;++j)
switch(now[i][j]) {
case 0: putchar('.'); break;
case 1: putchar('!'); break;
case 2: putchar('X'); break;
case 3: putchar('#'); break;
default: break;
}
putchar('\n');
}
if(ncase) putchar('\n');
}
return 0;
}

version 2, fgets + parsing, write to a buffer then output,

since the lines are quite short, there is no performance improvement.

#include <cstdio>
#include <algorithm> int main() {
//freopen("input.txt","r",stdin);
const int MAXSIZE=22, dimSize=20, lineMaxSize=500;
int bacnums1[MAXSIZE][MAXSIZE]={0},bacnums2[MAXSIZE][MAXSIZE]={0}, (*now)[MAXSIZE]=bacnums1, (*next)[MAXSIZE]=bacnums2, dna[16];
char line[lineMaxSize],output[lineMaxSize], *pc;
int ncase, nday, i,j,tmp, *p, *pend;
if(scanf("%d",&ncase)!=1 || ncase<=0) return -1;
while(ncase--) {
scanf("%d\n",&nday);
pc=fgets(line,lineMaxSize,stdin);
for(p=dna,pend=dna+16;p!=pend;++p) {
for(;*pc==' ';++pc) ;
if(*pc=='-') { tmp='0'-*++pc; }
else { tmp=*pc-'0'; }
++pc; *p=tmp;
}
for(i=1;i<=dimSize;++i) {
pc=fgets(line,lineMaxSize,stdin);
for(p=&now[i][1],pend=p+dimSize;p!=pend;++p) {
for(;*pc==' ';++pc) ;
*p=*pc++ -'0';
}
}
while(nday-->0) {
for(i=1;i<=dimSize;++i)
for(j=1;j<=dimSize;++j) {
tmp=now[i][j]+now[i-1][j]+now[i+1][j]+now[i][j-1]+now[i][j+1];
next[i][j]=now[i][j]+dna[tmp];
if(next[i][j]<0) { next[i][j]=0; }
else if(next[i][j]>3) { next[i][j]=3; }
}
std::swap(now,next);
}
for(pc=output, i=1;i<=dimSize;++i) {
for(j=1;j<=dimSize;++j)
switch(now[i][j]) {
case 0: *pc++='.'; break;
case 1: *pc++='!'; break;
case 2: *pc++='X'; break;
case 3: *pc++='#'; break;
default: break;
}
*pc++='\n';
}
if(ncase) *pc=0; else *(pc-1)=0;
puts(output);
}
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 1057 (simulation, use sentinel to avoid boudary testing, use swap trick to avoid extra copy.) 分类: hdoj 2015-06-19 11:58 25人阅读 评论(0) 收藏的更多相关文章

  1. Design T-Shirt 分类: HDU 2015-06-26 11:58 7人阅读 评论(0) 收藏

    Design T-Shirt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  2. 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 ...

  3. hdu 1041 (OO approach, private constructor to prevent instantiation, sprintf) 分类: hdoj 2015-06-17 15:57 25人阅读 评论(0) 收藏

    a problem where OO seems more natural to me, implementing a utility class not instantiable. how to p ...

  4. Train Problem I 分类: HDU 2015-06-26 11:27 10人阅读 评论(0) 收藏

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. Fibonacci Again 分类: HDU 2015-06-26 11:05 13人阅读 评论(0) 收藏

    Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  6. 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 ...

  7. 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) ...

  8. 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) ...

  9. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. 挂羊头卖狗肉蓄意欺骗读者——谭浩强《C程序设计(第四版)》中所谓的“按照C99”(二)

    挂羊头卖狗肉蓄意欺骗读者——谭浩强<C程序设计(第四版)>中所谓的“按照C99”(二) 在<谭C>p4:“本书的叙述以C99标准为依据”,下面从C89到C99的主要变化方面来看 ...

  2. CRM 2011 Install Errors - Tips and Tricks continued(转)

    The more I get to install/upgrade to CRM 2011 in different environment the more I come across differ ...

  3. xml问题报错处理

    添加个classPath:/  保存下就能解决报错了  /后面要加个空格,最后一行尖括号里面不能有空格.

  4. 简单破解.net(C#)程序

    一直在用makedown2(free版),每当打开多个页面,就会提示升级为pro,还要注册码激活什么的.就有了破解的想法.以前也弄过一个小程序的破解,所以还算有些经验. 1. ildasm 用来将ma ...

  5. 【转】Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If

    转载地址:http://fanshuyao.iteye.com/blog/1695482 在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server ...

  6. 【Linux命令与工具】磁盘与目录的容量——df和du

    df(disk free):列出文件系统的整体磁盘使用量 用法: df [-akmhi] [目录或文件名] 参数: -a: 列出所有的文件系统,包括系统特有的/proc等文件系统 -k: 以KB的容量 ...

  7. laravel框架总结(三) -- 路径分析

    1.直接写绝对路径,这样会用在/goods/show前面加上域名 <a href="/goods/show?id=<?php echo $item['id']; ?>&qu ...

  8. HDU Coprime

    Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total S ...

  9. Linux休眠,挂起,待机,关机的区别及相关命令

    休眠是一种更加省电的模式,它将内存中的数据保存于硬盘中,所有设备都停止工作.当再次使用时需按开关机键,机器将会恢复到您的执行休眠时的状态,而不用再次执行启动操作系统复杂的过程. 待机(挂起)是将当前处 ...

  10. 03-第一个C语言程序的分析

    本文目录 一.代码分析 二.开发和运行C程序的步骤 三.总结 说明:这个C语言专题,是学习iOS开发的前奏.也为了让有面向对象语言开发经验的程序员,能够快速上手C语言.如果你还没有编程经验,或者对C语 ...