ZOJ Seeding 2100 "Simple deep search"

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


Author:ZHANG, Zheng
Source:Zhejiang University Local Contest 2004

Simple Deep Search

 #include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;bool Used[10][10];char map[10][10];int dx[4]={1,-1,0,0};int dy[4]={0,0,-1,1};int N,M;int num;int tot;int Flag;void DFS (int x,int y) {used[x][y]=true;if (tot==num) {flag=1;//printf ("yes\n"); return;//true;} for (int i=0;i<4;i++) {int nx=x+dx[i];int ny=y+dy[i];if (map[nx][ny]== '. ') &&0<=nx&&nx<n&&0<=ny&&ny<m&&!used[nx][ny]) {//used[nx][ny]= true;tot++;//printf ("-->%d%d\n", Nx,ny);D FS (Nx,ny);//printf ("<--%d%d\n", nx,ny); tot--;used[nx][ny]=false;/ /return;}} printf ("no\n"); return;//false;} int main () {while (scanf ("%d%d", &n,&m), n!=0| | m!=0) {for (int i=0;i<n;i++) {scanf ("%s", Map[i]);} num=0;tot=1;flag=0;for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (map[i][j]== '. ') num++;}} memset (used,false,sizeof (used));D FS (0,0),//printf ("%d%d\n", Num,tot), if (!flag) printf ("no\n"), Else printf ("yes\n" );} return 0;} 


Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

ZOJ Seeding 2100 "Simple deep 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.