ZOJ Problem Set - 2100

简介:
ZOJ Problem Set - 2100
Seeding

Time Limit: 1 Second      Memory Limit: 32768 KB

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

=============

典型的DFS题,很不错。

题目很简单,从左上角出发,判断是否能完全把地耕玩(拖拉机不能在石块和已经耕好的地上行驶。)

?
#include <stdio.h>
#include <string.h>
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; //四个方向
char map[10][10]; //定义一张地图
int done;
int Flag;
int n,m;
void dfs( int x, int y)
{
   int i;
   if (x<1 || y<1 || x>n || y>m)   return ; //判断是否越界 
   done++;
   if (done==n*m) //如果成功则返回
   {
      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" ,&n,&m)!=EOF)
    {
       if (n==0 && m==0)
       break ;
       done=0;
       Flag=0;
       for (i=1;i<=n;i++)
       {
          
          for (j=1;j<=m;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" );
    }
?
    return 0;
 
}
本博客为 木宛城主原创,基于 Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名 木宛城主(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。
分类: Algorithms

本文转自木宛城主博客园博客,原文链接:http://www.cnblogs.com/OceanEyes/archive/2011/05/10/ZOJ_DFS_2100.html,如需转载请自行联系原作者
目录
相关文章
ZOJ - Problem Set - 3960 What Kind of Friends Are You?
ZOJ - Problem Set - 3960 What Kind of Friends Are You?
86 0
ZOJ - Problem Set - 3960 What Kind of Friends Are You?
ZOJ - Problem Set - 3985 String of CCPC
ZOJ - Problem Set - 3985 String of CCPC
99 0
ZOJ Problem Set - 3758 素数
ZOJ Problem Set - 3758 素数
100 0
|
机器学习/深度学习 人工智能 BI
ZOJ Problem Set - 3758 素数
Singles’ Day Time Limit: 2 Seconds Memory Limit: 65536 KB Singles’ Day(or One’s Day), an unofficial holiday in China, is a pop cu...
926 0
|
存储 算法 索引
ZOJ 3505. Yet Another Set of Numbers 解题报告
    ZOJ 3505:Yet Another Set of Numbers     地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3505       题意:有一个数字集合,集合中的数遵循以下规则:       ( 1 ).
924 0

热门文章

最新文章

下一篇
无影云桌面