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"