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 |
AC了 不知道还有没有方法优化呢?#include<iostream> #include<cmath> #include<cstdio> using namespace std; #define MAX 100 char map[MAX][MAX]; int visit[MAX][MAX]; int sum; int r,c; int path[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; void dfs(int a,int b) { visit[a][b]=1; int i; for(i=0;i<8;i++) { int x=a+path[i][0]; int y=b+path[i][1]; if(x>=1&&x<=r&&y>=1&&y<=c) { if(map[x][y]=='X'&&visit[x][y]==0) dfs(x,y); else if(map[x][y]=='.'&&(x==a||y==b)) { sum++; } } else if(x==a||y==b) sum++; } } int main () { int x,y; int i,j; while(scanf("%d%d%d%d",&r,&c,&x,&y)) { for(i=1;i<=r;i++) for(j=1;j<=c;j++) { visit[i][j]=0; } for(i=1;i<=r;i++) { cin.ignore(); for(j=1;j<=c;j++) { scanf("%c",&map[i][j]); } } sum=0; if(r==0&&c==0&&x==0&&y==0) break; dfs(x,y); 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