Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

看明白其实很容易,求周长竟然这样算的!!

Posted by wangbaobao at 2009-05-16 10:59:12 on Problem 1111
/*
对图中的#点看它4周有几个"." 就加上几个,在用dfs搜索就能求出周长
*/
#include"iostream"

using namespace std;
char map[30][30];
int path[30][30];
int num;
int r,c;
int dfs(int x,int y)
{
	if(map[x][y]=='.'||path[x][y]==1)
		return 1;
	
    path[x][y]=1;
	if(map[x+1][y]=='.')
		num++;
    if(map[x-1][y]=='.')
		num++;
	if(map[x][y-1]=='.')
		num++;
	if(map[x][y+1]=='.')
		num++;
	dfs(x+1,y);
	dfs(x-1,y);
	dfs(x,y+1);
	dfs(x,y-1);
	dfs(x+1,y-1);
	dfs(x+1,y+1);
	dfs(x-1,y-1);
 	dfs(x-1,y+1);
    return num;
}

int main()
{
	int  x,y;
	while(cin>>r>>c>>x>>y)
	{
		memset(map,'.',sizeof(map));
		memset(path,0,sizeof(path));
		if(r==0&&c==0&&x==0&&y==0)
			break;
        
	   for(int i=1;i<=r;i++)
		   for(int j=1;j<=c;j++)
			   cin>>map[i][j];
	    num=0;
		dfs(x,y);
	   cout<<num<<endl;
	}
	return 1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator