zoukankan      html  css  js  c++  java
  • Seeding(dfs)

    Seeding

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 101   Accepted Submission(s) : 52
    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

     题解:跟南阳最小步数差不多,这个是耕过的地就不再耕了,所以带个回溯,记录步数,如果走的步数等于‘.’的总个数就符合找不到就不对;

    代码:

     1 #include<stdio.h>
     2 int m,n,tot,nos;
     3 bool flag;
     4 char table[7][7];
     5 void dfs(int x,int y){
     6     //max=tot>max?tot:max;
     7     if(table[x][y]=='S'||x<0||x>=n||y>=m||y<0)return;
     8     table[x][y]='S';
     9     tot++;
    10     if(tot==nos){
    11         flag=true;
    12         return;
    13     }
    14     dfs(x+1,y);
    15     dfs(x,y+1);
    16     dfs(x-1,y);
    17     dfs(x,y-1);
    18     tot--;//回溯;
    19     table[x][y]='.';//回溯;
    20     return ;
    21 }
    22 int main(){
    23     int x,y,i,j;
    24     while(~scanf("%d%d",&n,&m),n||m){//max=0;
    25         for(x=0;x<n;x++)scanf("%s",table[x]);
    26         nos=0;tot=0;flag=false;
    27         for(x=0;x<n;x++){
    28             for(y=0;y<m;y++){
    29                 if(table[x][y]=='.'){
    30                     nos++;
    31                     if(nos==1)i=x,j=y;
    32                 }
    33             }
    34         }
    35         dfs(i,j);
    36     //    printf("%d
    ",max);
    37         //for(x=0;x<n;x++)printf("%s
    ",table[x]);
    38         if(flag)puts("YES");
    39         else puts("NO");
    40     }
    41     return 0;
    42 } 
  • 相关阅读:
    所有抽样单元都有相等的被选取机会 ,说法错误
    银行存款余额表由谁编制的
    资本公积——资本溢价与资本公积——其他资本公积
    货币单元抽祥
    企业安全生产费用账务的处理
    Tableau代写制作地图可视化和树形图、条形图
    Tableau 代写数据可视化:探索性图形分析新生儿死亡率数据
    R、Python代写、Open Refine采集pdf数据,清理数据和格式化数据
    使用GIS编程代写制作静态地图和处理地理数据
    用R语言编程代写制作交互式图表和地图
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4703409.html
Copyright © 2011-2022 走看看