Seeding 2100

来源:互联网 发布:互相点击软件 编辑:程序博客网 时间:2024/06/05 02:01

Seeding

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 40   Accepted Submission(s) : 14
Problem Description
It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with n * m squares. There are big stones in some of the squares. Tom has a seeding-machine. At the beginning, the machine lies in the top left corner of the field. After the machine finishes one square, Tom drives it into an adjacent square, and continues seeding. In order to protect the machine, Tom will not drive it into a square that contains stones. It is not allowed to drive the machine into a square that been seeded before, either.

Tom wants to seed all the squares that do not contain stones. Is it possible?


Input

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

Input is terminated with two 0's. This case is 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

C语言程序代码
/*
题意::
  给你n*m的矩形土地,‘.’表示可耕种土地 ,‘S’表示石头,问是否可以将土地
  全部耕种完。
*/
#include<stdio.h>
#include<string.h>
char a[7][7];
int n,m,cnt,k;
void dfs(int x,int y)
{
 if(a[x][y]=='S')
  return ;
 if(x<1||x>n||y<1||y>m)
  return ;
 if(k)
  return ;
  a[x][y]='S';
  cnt++;
  if(cnt==n*m)
  {
   k=1;
   return ;
  }
  dfs(x,y+1);
  dfs(x,y-1);
  dfs(x-1,y);
  dfs(x+1,y);
  cnt--;
  a[x][y]='.';
}
int main(){
 int i,j;
 while(scanf("%d%d",&n,&m),n|m)
 {
  cnt=0;
  for(i=1;i<=n;i++)
   scanf("%s",a[i]+1);
  for(i=1;i<=n;i++)
  {
   for(j=1;j<=m;j++)
   {
    if(a[i][j]=='S')
     cnt++;
   }
  }
  k=0;
  dfs(1,1);
  if(k)
   printf("YES\n");
  else
   printf("NO\n"); 
 }
 return 0;
}
0 0
原创粉丝点击