Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
请各位帮忙看看这个内存为什么会爆 我看了解题报告上面用21*21*16000+的静态数组都没爆啊#include<iostream> #include<queue> #include<memory> #include<string> using namespace std; int flag[21][21]; int sign[21][21]; int drec[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int wi,hi,body; int limit; int pro=1; struct snake { int x,y; }; struct co { int x,y,step; snake bdlh[8]; //保存当前位置蛇的坐标 }; co r,w; void refresh() { memset(flag,0,sizeof(flag)); memset(sign,0,sizeof(sign)); } bool uj(int x,int y) { if(x>=1&&x<=wi&&y>=1&&y<=hi) if(flag[x][y]==0) if(!sign[x][y]) return true; return false; } void movedeal()//移动处理//处理之后重新设置一边模拟数组 { int i;//!! snake tail,change;//!! tail.x=w.x; tail.y=w.y; w=r; w.x=tail.x; w.y=tail.y; for(i=0;i<body;i++) { change=w.bdlh[i]; w.bdlh[i]=tail; //cout<<"("<<w.bdlh[i].x<<","<<w.bdlh[i].y<<")"; tail=change; }//换位 //cout<<endl; w.step=r.step+1; for(i=0;i<body;i++) { flag[w.bdlh[i].x][w.bdlh[i].y]=2; } flag[tail.x][tail.y]=0; } void movedealt() { int i,j; for(i=1;i<=wi;i++) for(j=1;j<=hi;j++) { if(flag[i][j]==2) flag[i][j]=0; } for(i=0;i<body;i++) { flag[r.bdlh[i].x][r.bdlh[i].y]=2; } } bool bfs() { int i=0; queue<co>path; r.x=r.bdlh[0].x; r.y=r.bdlh[0].y; r.step=0; path.push(r);//入列 while(!path.empty()) { r=path.front(); movedealt(); path.pop(); for(i=0;i<4;i++) { movedealt(); w.x=r.x+drec[i][0]; w.y=r.y+drec[i][1]; if(r.x==1&&r.y==1)//条件判断 如果头一开始就在11; { printf("Case %d: %d\n",pro++,w.step-1); return true; } w.step=r.step+1; if(uj(w.x,w.y)) { sign[r.x][r.y]=1; movedeal(); path.push(w); } while(uj(w.x,w.y)) { w.x+=drec[i][0]; w.y+=drec[i][1]; w.step++; } if(w.x==1&&w.y==1) { printf("Case %d: %d\n",pro++,w.step); return true; } } } return false; } bool in() { refresh(); int x,y; int i=0; cin>>wi>>hi>>body; if(!(wi||hi||body)) return false; for(i=0;i<body;i++) { cin>>x>>y; flag[x][y]=2; r.bdlh[i].x=x; r.bdlh[i].y=y;//将蛇身的坐标保存下来方便进行移动时的处理 } cin>>limit; for(i=0;i<limit;i++) { cin>>x>>y; flag[x][y]=1000; } return true; } int main() { while(in()) { if(!bfs()) printf("Case %d: -1\n",pro++); } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator