Topic Link--
http://acm.hdu.edu.cn/showproblem.php?pid=1241
First, a n*m character matrix is given, '*' denotes the open space, '@' represents the well. Q How many wells are there in this matrix?
The 8 points around each point can be connected to each other.
Starting with the point in the upper-left corner, enumerate backwards and Dfs search is ready. Remember memory.
That's the end of the crap, the code--
1#include <cstdio>2#include <cmath>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 Const intN = About;8 Const intM =8;9 Ten intdis[m][2] = {{-1, -1}, {-1,0}, {-1,1}, {0, -1}, {0,1}, {1, -1}, {1,0}, {1,1}};//Judging 8 Directions One CharMp[n][n];//Original A intN, M;//Matrix Size - intAns//Results - the voidDfsintXintY//DFS Search functions - { -Mp[x][y] ='*';//at the beginning of the current point of processing, it needs to be eliminated, so as not to repeat the process, that is, "memory" - for(inti =0; i < M; i++) + { - intMX = x+dis[i][0]; + intmy = y+dis[i][1]; A if(MX >=0&& mx < n && my >=0&& my < m && mp[mx][my] = ='@') DFS (MX, my); at } - } - - voidWork ()//Main processing function - { - for(inti =0; I < n; i++) in { - for(intj =0; J < M; J + +) to { + if(Mp[i][j] = ='@') - { theans++; * Dfs (I, j); $ }Panax Notoginseng } - } the } + A voidInit ()//initialization function the { + GetChar (); - for(inti =0; I < n; i++) scanf ("%s", Mp[i]); $Ans =0; $ } - - voidOutit ()//output Function the { -printf"%d\n", ans);Wuyi } the - intMain () Wu { - //freopen ("test.in", "R", stdin); About while(~SCANF ("%d%d", &n, &m) &&m) $ { - Init (); - Work (); - outit (); A } + return 0; the}
View Code
HDU 1241Oil deposits (Dfs template)