Zoj 2100 Seeding

Source: Internet
Author: User

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 was 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

Test instructions: A sowing machine, no area to walk through, or an area of stone (S) to ask if you can go through all the stone-free areas of the map.

The puzzle: Mark the traversed area as a stone-covered area and keep walking if you can walk through the ans==s (the area that passes is equal to all the areas without stone) then return the output yes or backtrack

And clear the mark, change other routes to continue searching until the end

#include <stdio.h> #include <string.h>char map[10][10];int n,m,ans,ok,s;void dfs (int x,int y) {int i,j;int Move[4][2]={0,1,0,-1,1,0,-1,0};if (ans==s) ok=1;for (i=0;i<4;i++) {int Tx=x+move[i][0];int ty=y+move[i][1];if (0 <tx&&tx<=n&&0<ty&&ty<=m&&map[tx][ty]== '. ')    {           map[tx][ty]= ' S ';        ans++;        DFS (tx,ty);        Map[tx][ty]= '. ';        ans--;}}}    int main () {int j,i;while (scanf ("%d%d", &n,&m), n|m) {ans=0;ok=0;s=0;for (i=1;i<=n;i++) {GetChar (); for (j=1; j<=m;j++) {scanf ("%c", &map[i][j]), if (map[i][j]== '. ') s++;}} ans++;map[1][1]= ' S ';d FS (!ok) printf ("no\n"); elseprintf ("yes\n");} return 0;}

  

Zoj 2100 Seeding

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.