NEFU561 block calculation "recursion"

Source: Internet
Author: User

Topic Links:

http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=561


Main topic:

In a m*n rectangular room, a square tile with white and black color on the ground, you stand in one piece

On black tiles, you can only move to adjacent black tiles. Q: How many fast black tiles can be reached in total.

data, '. ' Represents a black tile, ' # ' represents a white tile, ' @ ' means the tile you stand on (the tile is black).


Ideas:

Can only move to adjacent black tiles, then for position (x, y), only to (X+1,y), (x,y+1), (X-1,y),

(x,y+1) The black tile moves. Every time you move, look at the tiles you haven't traversed, if it's a black tile,

go on, or you'll return. The number of black tile blocks traversed with ans, the final recursive equation is f (x, y) = f(x+1,y) +

(x,y+1) + (x-1,y) + (x,y+1).

To avoid repeating the number of tiles traversed, mark each piece of tile passed as white so that it does not repeat the calculation.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;int N,m;char map[35][35];int DFS (int x,int y) {    int d = 0;    if (x < 0 | | x >= N | | y < 0 | | y >= M | | Map[x][y] = = ' # ')        return 0;   Return, recursive boundary    if (map[x][y] = = '. ' | | Map[x][y] = = ' @ ')    {        d = 1;      tags, avoid repeated searches        map[x][y] = ' # ';    }    Return Dfs (x+1,y) + DFS (x,y+1) + DFS (x-1,y) + DFS (x,y-1) + D;} int main () {    while (~scanf ("%d%d", &m,&n) && (n| | M))    {        int ans = 0;        for (int i = 0; i < N; ++i)            scanf ("%s", Map[i]);        for (int i = 0, i < N; ++i) for            (int j = 0; j < M; ++j)                if (map[i][j] = = ' @ ')                    ans = DFS (i,j);        printf ("%d\n", ans);    }    return 0;}


NEFU561 Block calculates "recursion"

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.