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 <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) 收藏的更多相关文章
- 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 ...
- 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 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 ...
- 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) ...
- 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 ...
- 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 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 ...
随机推荐
- SQL复制一个表的数据到另一个表
最近做一个项目,由于客户数据量大,为了不将数据彻底删除,于是将数据移动到历史表,原始表的数据删除.由于技术有限,想不到好的方法,于是写个存储过程 执行,为了防止执行过程中出现异常,执行不完整.用到hI ...
- java.lang.OutOfMemoryError: PermGen space
Exception in thread ""http-bio-8080"-exec-1" java.lang.OutOfMemoryError: PermGen ...
- CSS 笔记三(Tables/Box Model/Outline)
CSS Tables border border: border-width border-style border-color|initial|inherit; border-width borde ...
- [转]关于 initWithNibName 和 loadNibNamed 的区别和联系
转载地址:http://jianyu996.blog.163.com/blog/static/1121145552012102293653906/ 关于 initWithNibName 和 loadN ...
- [转]SVN客户端解决authorization failed问题
转载地址:http://blog.csdn.net/patdz/article/details/7669591 1. 创建文件夹 E:\STWSource\STWLibrarySVN 2.在文件夹ST ...
- js页面刷新之实现普通页面
准备面试题目的时候遇到了页面刷新,就整理了一下,网上查找,大概就是八种方法,但是自己测试的时候出现了几个问题,跟大家分享: 首先准备一个测试页面: <!--html代码--> <h1 ...
- 获取本地IP,并设置到IP控件
char szHostName[MAX_PATH + 1]; gethostname(szHostName, MAX_PATH); //得到计算机名 hostent *p = gethostbynam ...
- oracle分析函数
在工作中使用到的分析函数主要有两种,一个是sum () over (partition by ……order by ……)另外一个就是 lead(lag)over (|partition by|ord ...
- 微信小程序文件结构
在小程序的跟目录有三个文件 app.js 小程序逻辑 必须有app.json 小程序公共设置 必须有app.wxss 小程序公共样式表 非必须有 小程序的每个页面是一个文件夹 里面包含4种 ...
- python 反射调用
因为目前在写一个python的项目,用到了Python的反射机制,所以做一下笔记,把写项目过程中的感悟记下来. 先简单介绍下Demo用到的函数: sys.path 是python的模块的路径集,是一个 ...