HDU 2952 counting Sheep (Dfs find unicom block)

Source: Internet
Author: User

Topic Link: please poke here. The main idea of the topic and ideas: Read test instructions is good, is DFS find unicom block.

There's nothing to say, see the code.

#include <cstdio> #include <cstring> #include <algorithm> #define N 100+5using namespace Std;int n,m; Char g[n][n];int dir[4][2]={1,0,0,1,-1,0,0,-1};void dfs (int x,int y) {for (int i=0;i<4;i++) {int nx=x+dir[        I][0],NY=Y+DIR[I][1]; if (nx>=0 && nx<n && ny>=0 && ny<m && g[nx][ny]== ' # ') {G[NX]  [ny]= '. ';            This will change ' # ' to '. ', indicating that it has been visited.        DFS (NX,NY);    }}}int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        for (int i=0;i<n;i++) scanf ("%s", &g[i]);        int ans=0;                for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (g[i][j]== ' # ')                    {g[i][j]= '. ';     DFS (I,J);                    After each DFS, ans is + +.                ans++;    }}} printf ("%d\n", ans); } return 0;}


HDU 2952 counting Sheep (Dfs find unicom block)

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.