zoj Seeding 2100

来源:互联网 发布:淘宝联盟自动上货软件 编辑:程序博客网 时间:2024/04/29 20:50
</pre></center><p><center><span style="color:green;">seeding</span></center><center><span style="color:green;">Time Limit: </span>2 Seconds      <span style="color:green;">Memory Limit: </span>65536 KB </center></p><hr />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. <p>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.</p><p>Tom wants to seed all the squares that do not contain stones. Is it possible?</p><p><strong>Input</strong></p><p>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.</p><p>Input is terminated with two 0's. This case is not to be processed. </p><p><strong>Output</strong></p><p>For each test case, print "YES" if Tom can make it, or "NO" otherwise.</p><p><strong>Sample Input</strong></p><p>4 4.S...S..........4 4.......S.......S0 0</p><p><strong>Sample Output</strong></p><p>YESNO</p><p><pre name="code" class="cpp">#include<stdio.h>char s[10][10];int a[4]={0,1,0,-1};int b[4]={1,0,-1,0};int m,n,flag,count;void dfs(int r,int c){if(r<1||r>m||c<1||c>n)  {  return;  }if(s[r][c]=='S')   return; if(flag)  return;s[r][c]='S';  count++;if(count == n*m)    {        flag=1;        return;    }    for(int i=0;i<4;i++)   {      dfs(r+a[i],c+b[i]);    }s[r][c]='.';count--;}int main(){while(scanf("%d%d",&m,&n),m|n){int i,j,k,l;count=0;flag=0;for(i=1;i<=m;i++)   {     getchar();    for(j=1;j<=n;j++)      {        scanf("%c",&s[i][j]);        if(s[i][j]=='S')           count++;}   }dfs(1,1);if(flag)  printf("YES\n"); else    printf("NO\n");}return 0}


0 0
原创粉丝点击