回形遍历 calc
来源:互联网 发布:淘宝直播同时在线人数 编辑:程序博客网 时间:2024/06/06 04:19
传送
70分,N^2的时间复杂度,模拟呗。
只是变量好繁琐,以及这个题本身烦人的坐标倒置。
100分,也是模拟,不过要在70分的基础上加上优化,70分每次只走一个格,而100分时优化到每次走一排(或者一列)。O(4*n)
实现:
观察下面的图:
发现,除了第一次,其他时候dx和dy都要-1
70分
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<cmath>using namespace std;int ax,ay,n,m,x,y,z,q;int f[1009][1009],flag;int main(){ scanf("%d%d%d%d%d",&n,&m,&x,&y,&z); //q=min(min(n-x,m-y),min(x+1,y+1)); //printf("%d",q);//第几圈 int p=m*n,num=0,dir=1; ax=1,ay=1; x++,y++; while(num<p) { //printf("(%d,%d)\n",ax,ay); if(!f[ay][ax]) f[ay][ax]=++num; if(ax==x&&ay==y) p=num+z;//走到(x,y)再走z步 if(dir==1)//右 { ax++; if(ax>n||f[ay][ax]!=0) { ax--,dir=2;//下 } } else if(dir==2)//下 { ay++; if(ay>m||f[ay][ax]!=0) { ay--,dir=3;//左 } } else if(dir==3)//左 { ax--; if(ax<1||f[ay][ax]!=0) { ax++,dir=4;//上 } } else if(dir==4)//上 { ay--; if(ay<1||f[ay][ax]!=0) { ay++,dir=1;//右 } } } for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(f[i][j]==p) { printf("%d %d",j-1,i-1);//一开始不想这样找最后的坐标,无奈老是错,只好这样 return 0; } } } return 0; }
100分
#include<iostream>#include<cstring>#include<string>#include<cmath>#include<algorithm>#include<cstdio> #define LL long longusing namespace std;LL n,m,x,y,deep,dir,ax,ay,first=1;LL z,step,maxn;int main(){ //freopen("a.in","r",stdin); scanf("%lld%lld%lld%lld%lld",&n,&m,&x,&y,&z); dir=1;step=1;maxn=1ll*(n*m); LL dx=n-1,dy=m-1; while(step!=1ll*(n*m)) { if(dir==1) { if(ay==y&&ax<=x&&ax+dx>=x) { maxn=step+(x-ax)+z; } step+=dx; ax+=dx; if(step>=maxn) { printf("%lld %lld",ax-(step-maxn),ay); return 0; } dir=2; if(!first) dx--;//除了第一圈,dx-- else first=0; } else if(dir==2) { if(ax==x&&ay<=y&&ay+dy>=y) { maxn=step+(y-ay)+z; } step+=dy; ay+=dy; if(step>=maxn) { printf("%lld %lld",ax,ay-(step-maxn)); return 0; } dir=3; dy--; } else if(dir==3) { if(ay==y&&ax>=x&&ax-dx<=x) { maxn=step+(ax-x)+z; } step+=dx; ax-=dx; if(step>=maxn) { printf("%lld %lld",ax+(step-maxn),ay); return 0; } dir=4; dx--; } else if(dir==4) { if(ax==x&&ay>=y&&ay-dy<=x) { maxn=step+(ay-y)+z; } step+=dy; ay-=dy; if(step>=maxn) { printf("%lld %lld",ax,ay+(step-maxn)); return 0; } dir=1; dy--; } } printf("%d %d",ax,ay); return 0;}
阅读全文
1 1
- 回形遍历 calc
- calc
- Calc
- calc()
- calc.exe
- 10calc
- Calc计算器
- calc常用
- 2506: calc
- 简易calc
- extremedb--calc
- 【BZOJ2506】calc
- bzoj2671: Calc
- BZOJ2671: Calc
- BZOJ2506 calc
- 2655: calc
- 2506: calc
- 2655: calc
- 一、(六)并发处理
- 解决Android 6.0以上的相机权限适配问题
- kaggle
- 1041. 考试座位号(15)
- 台湾国立大学机器学习基石.听课笔记(第四讲): 机器学习的可行性
- 回形遍历 calc
- Eclipse添加和设置Maven
- Java Arrays.asList()案例
- JAVA 中的浮点数据类型
- Pyhton字符串处理
- 月度开销
- 进程IPC---消息队列
- 数据分析介绍之二——单变量数据观察之直方图
- 一、(七)控制语句