hdu 1241Oil Deposits(BFS)
生活随笔
收集整理的這篇文章主要介紹了
hdu 1241Oil Deposits(BFS)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1241
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13137????Accepted Submission(s): 7611
?
Input The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.?
Output For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.?
Sample Input 1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0?
Sample Output 0 1 2 2 題目大意: 這個題目就是要查找油田的個數,比如說第二組數據 3 5 *@*@* **@** ? ? ? ? ? ? 注明:"@"這個與他的幾個方向都是@都是可以相連的。所以稱為一塊油田。輸出1。 *@*@* ?這種就很容易想到搜索。這里的一個技巧就是起點就從@開始,其次就是找到@就使其變成*;不過找過的還是要標記為1,否則就會重復,這樣不連接的還可以繼續搜~ 東西還是要反復的咀嚼,不然就會很生,忘得差不多0.0 詳見代碼。 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 6 using namespace std; 7 8 int dir[8][2]= {0,1,0,-1,1,0,-1,0,1,1,1,-1,-1,1,-1,-1}; 9 char map[110][110]; 10 int a,b,vis[110][110],k; 11 12 struct node 13 { 14 int x,y; 15 int t; 16 } s,ss; 17 18 queue<node>q,qq; 19 int bfs() 20 { 21 while (!q.empty()) 22 { 23 s=q.front(); 24 q.pop(); 25 //vis[s.x][s.y]=1; 26 for (int i=0; i<8; i++) 27 { 28 int x=s.x+dir[i][0]; 29 int y=s.y+dir[i][1]; 30 //int t=s.t+1; 31 if (x>=0&&x<a&&y>=0&&y<b) 32 { 33 if (!vis[x][y]&&map[x][y]=='@') 34 { 35 ss.x=x; 36 ss.y=y; 37 map[ss.x][ss.y]='*'; 38 //vis[x][y]=1; 39 q.push(ss); 40 } 41 42 } 43 } 44 } 45 } 46 47 int main () 48 { 49 while (scanf("%d%d",&a,&b)!=EOF) 50 { 51 memset(vis,0,sizeof(vis)); 52 if (a==0&&b==0) 53 break; 54 for (int i=0; i<a; i++) 55 { 56 getchar(); 57 for (int j=0; j<b; j++) 58 { 59 scanf("%c",&map[i][j]); 60 } 61 } 62 //int k=0; 63 for (int i=k=0; i<a; i++) 64 { 65 for (int j=0; j<b; j++) 66 { 67 if (map[i][j]=='@') 68 { 69 k++; 70 s.x=i; 71 s.y=j; 72 map[s.x][s.y]='*'; 73 vis[s.x][s.y]=1; 74 q.push(s); 75 bfs(); 76 } 77 } 78 } 79 printf ("%d\n",k); 80 } 81 return 0; 82 }?
轉載于:https://www.cnblogs.com/qq-star/p/4139845.html
總結
以上是生活随笔為你收集整理的hdu 1241Oil Deposits(BFS)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DevExpress控件库----Ale
- 下一篇: iOS定时器