Maze interval ation (Simple Deep-first search)

Source: Internet
Author: User

Let's talk about:

Shows the meaning of the question in advance:

XXXXXXXXX

X

X * x

X

XXXXXXXXX

X

X

X

XXXXX

You can think of it as a House. The character 'X' is the wall, and you are now in the location of '*'. Ask you to fill in the blank space around, '*' is replaced. Note that the firewall cannot be crossed. In fact, it is the most basic issue of deep traversal. You only need to find the location of '*' and check whether there are spaces around it. If yes, fill in '#' and then recursion, otherwise, the function will return directly.

Source code:

# Include <stdio. h> # include <string. h> # define maxw 80 + 5 # define maxh 30 + 5 char room [maxh] [maxw]; void DFS (INT, INT); int main () {int t, n, x, y, I; freopen ("data", "r", stdin); // scanf ("% d", & T); getchar (); while (t --) {n = 0; while (gets (room [N]) if (room [N] [0] = '_') break; else n ++; For (x = 0; x <n; X ++) // locate the initial position for (y = 0; y <strlen (room [x]); Y ++) if (room [x] [Y] = '*') DFS (x, y); For (x = 0; x <= N; X ++) printf ("% s \ n", room [x]);} return 0;} void DFS (int x, int y) {If (room [x] [Y] = ''| room [x] [Y] = '*') room [x] [Y] = '#'; else return; // traverse the perimeter of the current point DFS (x-1, Y-1); DFS (x, Y-1 ); DFS (x + 1, Y-1); DFS (x-1, Y); DFS (x + 1, Y); DFS (x-1, Y + 1); DFS (X, Y + 1); DFS (x + 1, Y + 1); return ;}


Maze interval ation (Simple Deep-first search)

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.