⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p2100_简单搜索.cpp

📁 高手写的所有acm例程 在acm.zju.edu.cn 上的题目的例程
💻 CPP
字号:
#include <stdio.h>
#include <string.h>

const    int dx [4] = { 0 , 0 , -1 , 1 };
const    int dy [4] = { -1 , 1 , 0 , 0 };

int   N , M , Total;
char  map [6] [7];
bool  Searched [6] [6];


bool  init ()
{
      scanf ( "%d%d\n" , &N , &M );
      if ( N == 0 ) return false;
      for ( int i = Total = 0; i < N; i ++ ) {
          gets ( map [i] );
          for ( int j = 0; j < M; j ++ ) if ( map [i] [j] == '.' ) Total ++;
      }
      return true;
}

bool  range ( int x ,int y )
{
      return  x >= 0 && x < N && y >= 0 && y < M;
}

bool  Search ( int x , int y , int depth )
{
      if ( depth == Total ) return true;
      int  i , px , py;
      for ( i = 0; i < 4; i ++ ) {
          px = x + dx [i] , py = y + dy [i];
          if ( !range ( px , py ) || map [px] [py] == 'S' || Searched [px] [py] ) continue;
          Searched [px] [py] = true;
          if ( Search ( px , py , depth + 1 ) ) return true;
          Searched [px] [py] = false;          
      }
      return false;
}

main ()
{
     while ( init () ) {
           if ( map [0] [0] == 'S' ) {
                printf ( "NO\n" );
                continue;
           }
           
           memset ( Searched , 0 , sizeof ( Searched ));
           Searched [0] [0] = true;
           if ( Search ( 0 , 0 , 1 )) printf ( "YES\n" );
           else printf ( "NO\n" );
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -