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 |
简单的DFS,不过内存用得较多,有什么办法可减少内存的使用?(附代码)#include<string> #include<stack> #include<cstdio> using namespace std; char image[21][21]; int flag[21][21]; int move[8][2]={ {-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1} }; int sum,row,col,rr,cc; void dfs(int r,int c){ flag[r][c]=1; for(int i=0;i<8;i++){ int x=r+move[i][0]; int y=c+move[i][1]; if(x>=0&&x<row&&y>=0&&y<col){ if(image[x][y]=='X'&&!flag[x][y]) dfs(x,y); else if(image[x][y]=='.'&&(x==r||y==c)) sum++; }else{ if(x==r||y==c) sum++; } } return; } int main(){ while(scanf("%d%d%d%d",&row,&col,&rr,&cc)&&row!=0){ for(int i=0;i<row;i++) scanf("%s",image[i]); memset(flag,0,sizeof(flag)); sum=0; dfs(rr-1,cc-1); printf("%d\n",sum); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator