HDU 1241Oil deposits (Dfs template)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.