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 |
Re:dfs 纯是自己做的,一次ac了,太爽了!!In Reply To:dfs 纯是自己做的,一次ac了,太爽了!! Posted by:wangbaobao at 2009-05-11 20:56:29 > #include"iostream" > using namespace std; > int m,n; > char array[101][101]; > int path[101][101]; > void dfs(int i,int j) > { > path[i][j]=1; //对没走过的点进行置1 记录表示已经走过不能在走了。 > if(i-1>=0&&array[i-1][j]=='@'&&path[i-1][j]==0) > dfs(i-1,j); > if(i+1<m&&array[i+1][j]=='@'&&path[i+1][j]==0) > dfs(i+1,j); > if(j-1>=0&&array[i][j-1]=='@'&&path[i][j-1]==0) > dfs(i,j-1); > if(j+1<n&&array[i][j+1]=='@'&&path[i][j+1]==0) > dfs(i,j+1); > if(i-1>=0&&j-1>=0&&array[i-1][j-1]=='@'&&path[i-1][j-1]==0) > dfs(i-1,j-1); > if(i-1>=0&&j+1<n&&array[i-1][j+1]=='@'&&path[i-1][j+1]==0) > dfs(i-1,j+1); > if(i+1<m&&j-1>=0&&array[i+1][j-1]=='@'&&path[i+1][j-1]==0) > dfs(i+1,j-1); > if(i+1<m&&j+1<n&&array[i+1][j+1]=='@'&&path[i+1][j+1]==0) > dfs(i+1,j+1); > } > > > int main() > { > > while(cin>>m>>n,m) > { > int count=0; //用于对路径进行计算 > memset(path,0,sizeof(path)); // 对path数组初始化,0表示没有走过 > int i,j; > for( i=0;i<m;i++) > for( j=0;j<n;j++) > cin>>array[i][j]; > > for(i=0;i<m;i++) > for(j=0;j<n;j++) > { > if(array[i][j]=='@'&&path[i][j]==0) > { > count++; > > dfs(i,j); // 深度搜索 > } > } > cout<<count<<endl; > } > return 1; > } > > > 哈哈 传说中的floodfill啊 1A Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator