ACM: 深搜题

             Oil Deposits

描述

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

输入

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.

输出

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

 

input:

1 1

*

3 5

*@*@*

**@**

*@*@*

1 8

@@****@*

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

0 0

output:

0

1

2

2

问题描述:在一个网格矩阵中寻找不同油矿 , 当遇到“@”时 , 这个点上 下 左 右 斜角上是“@”的点属于同一个油矿。

第一个想法:广搜 和 深搜

决定深搜递归解决.

#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;


char grid[101][101];
bool flag[101][101];

int dir[][2] = {{-1,1} , {0,1} , {1,1} , {1,0} , {1,-1} , {0,-1} , {-1,-1} , {-1,0}};

void DFS(int x,int y,int m,int n)
   
 flag[x][y] = true;   
 for(int i=0;i<8;i++)
 {
  int xx = x + dir[i][0];
  int yy = y + dir[i][1];
  if(xx >= 0 && xx < m && yy >= 0 && yy < n && !flag[xx][yy] && grid[xx][yy] == ‘@')    
   DFS(xx,yy,m,n);          
 }
}

int main()
{
    int m , n , count;
    int i , j;
    while(cin >> m >> n && m+n)  
         
       memset(flag,false,sizeof(flag));  
       for(i = 0; i < m; i++)          
          for(j=0; j < n; j++)             
              cin >> grid[i][j];                      
   
       count = 0;                 
      for(i = 0; i < m; i++)          
         for(j=0;j<n;j++)          
                      
                if(grid[i][j] == '@' && !flag[i][j])   
                               
                      count++;                 
                      DFS(i,j,m,n);                      
                                                 
         }
        cout << count << endl;
    }

return 0;
}

 

 

                                             Hero In Maze

描述

500年前,Jesse是我国最卓越的剑客。他英俊潇洒,而且机智过人^_^。

突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中。Jesse听说这个消息已经是两天以后了,他知道公主在迷宫中还能坚持T天,他急忙赶到迷宫,开始到处寻找公主的下落。
时间一点一点的过去,Jesse还是无法找到公主。最后当他找到公主的时候,美丽的公主已经死了。从此Jesse郁郁寡欢,茶饭不思,一年后追随公主而去了。T_T
500年后的今天,Jesse托梦给你,希望你帮他判断一下当年他是否有机会在给定的时间内找到公主。

他会为你提供迷宫的地图以及所剩的时间T。请你判断他是否能救出心爱的公主。

输入

题目包括多组测试数据。
每组测试数据以三个整数N,M,T(0<n, m≤20, t>0)开头,分别代表迷宫的长和高,以及公主能坚持的天数。
紧接着有M行,N列字符,由".","*","P","S"组成。其中
"." 代表能够行走的空地。
"*" 代表墙壁,Jesse不能从此通过。
"P" 是公主所在的位置。
"S" 是Jesse的起始位置。
每个时间段里Jesse只能选择“上、下、左、右”任意一方向走一步。
输入以0 0 0结束。

输出

如果能在规定时间内救出公主输出“YES”,否则输出“NO”。

样例输入

4 4 10

....

....

....

S**P

0 0 0

 

样例输出

YES 第一想法:广搜遍历

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int max = 100;

 

int ans[max][max];
char map[max][max];
int dir[][2]={{0,1},{0,-1},{1,0},{-1,0}};
int n , m , day , i , j;

 

struct node
{
     int x;
     int y;
};

 


int bfs(node s,node e)
{
      node qu[max*max+1];
      node t;
      int front = -1;
      int rear = -1;
      map[s.x][s.y]='*';
      rear++;
      qu[rear]=s;
      ans[s.x][s.y]=0;
 
     while(front < rear)
     {
           front++;
           s = qu[front];
          for(i = 0; i < 4; i++)
         {
                t.x = s.x + dir[i][0];
                t.y = s.y + dir[i][1];
               if(t.x >= 0 && t.x < n && t.y >= 0 && t.y < m && map[t.x][t.y] != '*')
              {
                       ans[t.x][t.y]=ans[s.x][s.y]+1;
                       if(t.x == e.x && t.y == e.y && ans[t.x][t.y] <= day)
                       {
                              return 1;
                       }
                       rear++;
                       map[t.x][t.y] = '*';
                       qu[rear] = t;
                }
        }

 

}
     return 0;
}

 


int main()
{
     node s,e;
     while(cin >> n >> m >> day , m || n || day)
     {
          for(i = 0; i < n; i++)
         {
            getchar();
            for(j = 0; j < m; j++)
           {
                cin >> map[i][j];
               if(map[i][j]=='S')
              {
                   s.x=i;
                   s.y=j;
              }
              if(map[i][j]=='P')
             {
                  e.x=i;
                  e.y=j;
             }
           }
      }
       if(bfs(s,e))
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
  }
 
   return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值