Author : Anwar Jahid Ruman Hossain
CSE, Batch - 6
BRUR.
Problem Statement : [UVA] 572 - Oil Deposits
Source : UVA Online Judge
Category : Graph Theory
Algorithm : DFS
Verdict : Accepted
- #include<bits/stdc++.h>
- using namespace std;
- #define mp(a,b) make_pair(a,b)
- #define mem(a,v) memset(a,v,sizeof(a))
- int dx8[]={1,1,1,0,-1,-1,-1,0};
- int dy8[]={-1,0,1,1,1,0,-1,-1};
- char str[105][105];
- map<pair<int,int>,int>visit;
- void dfs(int a,int b){
- int x,y;
- visit[mp(a,b)]=1;
- for(int i=0;i<8;i++){
- x=a+dx8[i];
- y=b+dy8[i];
- if(str[x][y]=='@' && !visit[mp(x,y)]){
- dfs(x,y);
- }
- }
- }
- int main () {
-
-
-
- int r,c;
- while(cin>>r>>c){
- if(r==0 && c==0)break;
- mem(str,0);
- for(int i=0;i<r;i++){
- cin>>str[i];
- }
- visit.clear();
- int cnt=0;
- for(int i=0;i<r;i++){
- for(int j=0;j<c;j++){
- if(str[i][j]=='@' && !visit[mp(i,j)]){
- cnt++;
- dfs(i,j);
- }
- }
- }
- cout<<cnt<<endl;
- }
- return 0;
- }
No comments:
Post a Comment