Num 36:zoj 2100 [Depth-first search algorithm] [backtracking]

Source: Internet
Author: User



The problem is solved by backtracking:


Topic:

Seeding Time Limit: 2 Seconds Memory Limit: 65536 KB It is spring time and farmers has to plant seeds in the field. Tom had a nice field,which are a rectangle with n * m squares. There is big stones in some of the squares.

Tom has a seeding-machine. At the beginning, the lies in the top left corner of the field. After the machine finishes one square, Tom drives it to an adjacent square, and continues seeding. In order to protect the machine, Tom won't drive it into a square of that contains stones. It's not allowed-to-drive-into-a square that been seeded before, either.

Tom wants to seed all the squares this does not contain stones. Is it possible?


Input

The first line of all test case contains, integers n and m that denote the size of the field. (1 < N, m < 7) The next n lines give the field, each of the which contains M characters. ' S ' is a square with stones, and '. ' is a square without stones.

Input is terminated with the 0 ' s. This is a not-to-be processed.


Output

For each test case, print "YES" if Tom can make it, or "NO" otherwise.


Sample Input

4 4
. S..
. S..
....
....
4 4
....
... S
....
... S
0 0


Sample Output

YES
NO


Main topic:

From the upper left corner of the road, can not go back to the map of a little walk again, (s for obstacles);


Topic Analysis:

From the upper left corner, the use of backtracking to carry out deep search, to determine whether there is a situation, can not turn around a road to finish;


AC Code:

#include <stdio.h> #include <stdlib.h> #include <string.h>int x,y,m,n,snum,max=0,flag;char map[10][ 10];void Fun (int x,int y) {if (x<1| | x>m| | y<1| | Y>n) return;    if (map[x][y]== ' S ') return;    if (flag==1) return;map[x][y]= ' S '; snum++;if (snum==m*n)    {    flag=1;    return;    } Fun (X-1,y), Fun (X+1,y), Fun (x,y-1), Fun (x,y+1), snum--;map[x][y]= '. ';} int main () {int i,j;while (scanf ("%d%d", &m,&n), m|n) {snum=0;for (i=1;i<=m;i++) for (j=1;j<=n;j++) {scanf ( "%c", &map[i][j]); if (map[i][j]== ' S ') snum++;} Flag=0;fun (), if (flag) printf ("yes\n"), Else printf ("no\n");} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Num 36:zoj 2100 [Depth-first search algorithm] [backtracking]

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.