首页 > 代码库 > hdu 1241 Oil Deposits

hdu 1241 Oil Deposits

集训第一场,A题

dfs简单题

之前看过dfs,没实现过,第一次实现出来O(∩_∩)O~水手加油~

 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int row,col; 5 char oil[1000][1000]; 6 int to[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}}; 7 void dfs(int i,int j) 8 { 9     //if(oil[i][j]==‘@‘)10     if(oil[i][j] != @ || i >=row || j >= col || i < 0 || j < 0) return ;11     oil[i][j] = *;12     for(int k = 0; k < 8; k++)13         dfs(i + to[k][0],j + to[k][1]);14 }15 16 int main()17 {18     freopen("input.txt","r",stdin);19     int Count;20     while(cin>>row>>col && row && col)21     {22         Count = 0;23         for(int i = 0; i < row; i++)24             for(int j = 0; j < col; j++)25                 cin>>oil[i][j];26         for(int i = 0; i < row; i++)27         {28             for(int j = 0; j < col; j++)29             {30                 if(oil[i][j] == @)31                    {32                         dfs(i,j);Count++;33                    }34 35             }36         }37         cout<<Count<<endl;38     }39     return 0;40 }