标签:
Description
Input
Output
Sample Input
Sample Output
1 #include "iostream" 2 #include "queue" 3 using namespace std; 4 struct person 5 { 6 int i; 7 int j; 8 }; 9 char o[105][105]; 10 void obegin(int n,int m) 11 { 12 int i,j; 13 for (i=0;i<=n+1;i++) 14 for (j=0;j<=m+1;j++) 15 { 16 if (i*j == 0 || i == n+1 || j == m+1) 17 o[i][j] = ‘*‘; 18 else 19 cin>>o[i][j]; 20 } 21 } 22 int dfs(int n,int m) 23 { 24 queue <person> p; 25 person fir,sec; 26 int c=0; 27 int f; 28 for (int i=1;i<=n;i++) 29 for (int j=0;j<=m;j++) 30 if (o[i][j] == ‘@‘) 31 { 32 f=0; 33 fir.i = i; 34 fir.j = j; 35 c++; 36 p.push(fir); 37 while (!p.empty()) 38 { 39 sec = p.front(); 40 p.pop(); 41 for (int k=1;k<=8;k++) 42 { 43 switch(k) 44 { 45 case 1: if (o[sec.i+1][sec.j] == ‘@‘) 46 { 47 fir.i=sec.i+1; 48 fir.j=sec.j; 49 }break; 50 case 2:if (o[sec.i-1][sec.j] == ‘@‘) 51 { 52 fir.i=sec.i-1; 53 fir.j=sec.j; 54 }break; 55 case 3:if (o[sec.i][sec.j+1] == ‘@‘) 56 { 57 fir.i=sec.i; 58 fir.j=sec.j+1; 59 }break; 60 case 4:if (o[sec.i][sec.j-1] == ‘@‘) 61 { 62 fir.i=sec.i; 63 fir.j=sec.j-1; 64 }break; 65 case 5:if (o[sec.i+1][sec.j+1] == ‘@‘) 66 { 67 fir.i=sec.i+1; 68 fir.j=sec.j+1; 69 }break; 70 case 6:if (o[sec.i+1][sec.j-1] == ‘@‘) 71 { 72 fir.i=sec.i+1; 73 fir.j=sec.j-1; 74 }break; 75 case 7:if (o[sec.i-1][sec.j-1] == ‘@‘) 76 { 77 fir.i=sec.i-1; 78 fir.j=sec.j-1; 79 }break; 80 case 8:if (o[sec.i-1][sec.j+1] == ‘@‘) 81 { 82 fir.i=sec.i-1; 83 fir.j=sec.j+1; 84 85 }break; 86 } 87 if (o[fir.i][fir.j] == ‘@‘) 88 { 89 o[fir.i][fir.j]=‘#‘; 90 p.push(fir); 91 f++; 92 } 93 if (f/100 == 1) 94 { 95 f=0; 96 c++; 97 } 98 } 99 } 100 } 101 return c; 102 } 103 int main() 104 { 105 int n,m; 106 while (cin>>m>>n && n) 107 { 108 obegin(m,n); 109 cout<<dfs(m,n)<<endl; 110 } 111 return 0; 112 }
暑假集训(1)第七弹 -----Oil Deposits(Poj1562)
标签:
原文地址:http://www.cnblogs.com/huas-zlw/p/5676949.html