深搜专题初步-1001

来源:互联网 发布:sql 查询某个字段包含 编辑:程序博客网 时间:2024/05/21 12:09

1001

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
总提交: 86            测试通过: 26

描述

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?

输入

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.

输出

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

样例输入

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

样例输出

YESNO

  代码:

#include<stdio.h>#include<string.h>char map[11][11];int flag;int done;int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int m,n;void dfs(int x,int y){ int i;  if(x<1||y<1||x>m||y>n)return ;  done++;  if(done==m*n)  {  flag=1;  return ;  }  for(i=0;i<4;i++)  {  if(map[x+dir[i][0]][y+dir[i][1]]=='.')     {     map[x+dir[i][0]][y+dir[i][1]]='S';     dfs(x+dir[i][0],y+dir[i][1]);     }  }  done--;  map[x][y]='.';}int main(){  int i,j;  memset(map,'S',sizeof(map));  while(~scanf("%d%d%*c",&m,&n))  {   if(m==0&&n==0)break;   flag=0;   done=0;   for(i=1;i<=m;i++)   {   for(j=1;j<=n;j++)     {      scanf("%c",&map[i][j]);  if(map[i][j]=='S')    done++;     }     getchar();   }    map[1][1]='S';   dfs(1,1);   if(flag==1)    printf("YES\n");    else printf("NO\n");  }}


0 0
原创粉丝点击