广搜入门 待改进的广搜

杭电1010

Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
 

 

Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.

The input is terminated with three 0's. This test case is not to be processed.
 

 

Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 

 

Sample Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
 

 

Sample Output
NO YES
 
搜索的基本思想
1.化解为多个子问题  每一个子问题都是向四个方向的遍历过程
2.为了避免死循环 需要一个标记数组(visit)
3.为了避免一条路径的标记对其他路径的干扰 需要一个回溯清空标记数组的过程
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int n,m,time;
int flag;//判断是否有符合条件的搜索
int visit[20][20];
char mapp[20][20];
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};//用于四个方向的遍历
void dfs(int x,int y,int t)
{
 int i;
 if(flag==1) return;//找到一个就退回。。 减少时间的消耗
 if(mapp[x][y]=='D')
 {
    if(t==time)
    flag=1;
    return;  //递归的终止条件 得多注意
    }
 if(t>=time) return;
 for(i=0;i<4;i++)
 {
  int xx,yy;
  xx=x+dir[i][0];
  yy=y+dir[i][1];
  if(xx<0||xx>=n||yy<0||yy>=m||mapp[xx][yy]=='X'||visit[xx][yy]==1) continue;
  visit[xx][yy]=1;
  dfs(xx,yy,t+1);//此时的i如果为全局变量的话 这里就对i的值做了改变
  visit[xx][yy]=0;// 回溯过程 防止一条结束的搜索路径对其他路径的干扰 这是重点!!!!!
 }
}
int main()
{
 int start1,start2,i,j;
 while(scanf("%d %d %d",&n,&m,&time)!=EOF)
 {
  if(n==0&&m==0&&time==0) break;
  memset(visit,0,sizeof(visit));
  flag=0;
  for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
   {
      cin>>mapp[i][j];
   }
  }
  for(i=0;i<n;i++)//找到初始位置
  {
   for(j=0;j<m;j++)
   {
    if(mapp[i][j]=='S')
    {
     start1=i;
     start2=j;
     break;
    }
   }
  }
  visit[start1][start2]=1;
  dfs(start1,start2,0);
  if(flag==1) printf("YES\n");
  else printf("NO\n");
 }
 return 0;
}
总结教训的话 全局变量得好好用 在递归中需要在内部传值的不要使用全局变量
posted @ 2016-04-06 22:10  猪突猛进!!!  阅读(182)  评论(0编辑  收藏  举报