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 |
牛人帮忙看看,WA啊,请教一下怎么回事(测例已过)#include<iostream> #include<string> using namespace std; char graf[22][22]; string tempG[20]; class TStack{ private: int cont[20][3]; int top; public: TStack::TStack(); void push(int x, int y, int d); void pop(int &x, int &y, int &d); void empty(void); bool isEmpty(void); }; void TStack::empty(){ top=-1; } int getNextDir(int &x, int &y, int curD); int calCurC(int x, int y); int calCurC(int x, int y) { int c=0; if(graf[x][y]!='X') return 0; if(graf[x-1][y]!='X'&&graf[x-1][y]!='x') c++; if(graf[x+1][y]!='X'&&graf[x+1][y]!='x') c++; if(graf[x][y+1]!='X'&&graf[x][y+1]!='x') c++; if(graf[x][y-1]!='X'&&graf[x][y-1]!='x') c++; return c; } int getNextDir(int &x, int &y, int curD) { switch(curD+1) { case 1://左 y--; if(graf[x][y]=='X') return 1; y++; case 2://上 x--; if(graf[x][y]=='X') return 2; x++; case 3://右 y++; if(graf[x][y]=='X') return 3; y--; case 4://下 x++; if(graf[x][y]=='X') return 4; x--; case 5://左上 y--;x--; if(graf[x][y]=='X') return 5; y++;x++; case 6://左下 y--;x++; if(graf[x][y]=='X') return 6; y++;x--; case 7://右下 y++;x++; if(graf[x][y]=='X') return 7; y--;x--; case 8://右上 y++;x--; if(graf[x][y]=='X') return 8; y--;x++; default: //全部探索完成 return 0; } } void TStack::pop(int &x, int &y, int &d) { //简化,不考虑空栈 x=cont[top][0]; y=cont[top][1]; d=cont[top][2]; top--; } void TStack::push(int x, int y, int d) { //简化,不考虑满栈 top++; cont[top][0]=x; cont[top][1]=y; cont[top][2]=d; } TStack::TStack() { top=-1; } bool TStack::isEmpty() { if(top==-1) return true; else return false; } int main() { int i,j; //循环变量 int width,height,x,y,curX,curY; //图形的宽、高,当前点座标 int d=0, c=0; //方向及总周长 cin>>height>>width>>x>>y; while(height||width||x||y){ //初始化栈 TStack st; //初始化c c=0; //初始化图形 for(i=0;i<height+2;i++) for(j=0;j<width+2;j++) graf[i][j]='.'; //建立图阵 for(i=0;i<height;i++) cin>>tempG[i]; for(i=1;i<=height;i++) for(j=1;j<=width;j++) graf[i][j]=tempG[i-1][j-1]; /* for(i=0;i<=height+1;i++) { for(j=0;j<=width+1;j++) cout<<graf[i][j]; cout<<endl; } */ do{ curX=x;curY=y; d=getNextDir(x,y,d); c+=calCurC(curX,curY); graf[curX][curY]='x'; //设置当前点为已探测 if(d) { st.push(curX,curY,d); d=0; } else{ if(!st.isEmpty()) st.pop(x,y,d); } } while(!st.isEmpty()||d!=0); cout<<c<<endl; st.empty(); cin>>height>>width>>x>>y; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator