L - Oil Deposits (hdu 1241) (基础dfs)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41061455/article/details/80349283

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42649    Accepted Submission(s): 24739

Problem Description

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.

Sample Input

1 1


3 5




1 8


5 5






0 0

Sample Output

0 1 2 2


Mid-Central USA 1997


using namespace std;
const int maxn=100+5;
char Map[maxn][maxn];
int m,n,flag[maxn][maxn];
void dfs(int r,int c,int id)
    if(r<0||r>=m||c<0||c>=n) return ;//出界的格子
    if(flag[r][c]||Map[r][c]!='@') return ;//不是'@'或者已经访问过的格子
    for(int i=-1;i<=1;i++)//搜索相邻及其对角线
        for(int j=-1;j<=1;j++)
        if(i||j) dfs(r+i,c+j,id);
int main()
        for(int i=0;i<m;i++) scanf("%s",Map[i]);
        int ans=0;
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            if(!flag[i][j]&&Map[i][j]=='@') dfs(i,j,++ans);
    return 0;



Problem DescriptionrnThe 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.rnrnThe 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.rn rnrnInputrnThe 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:rnrn'X': a block of wall, which the doggie cannot enter; rn'S': the start point of the doggie; rn'D': the Door; orrn'.': an empty block.rnrnThe input is terminated with three 0's. This test case is not to be processed.rn rnrnOutputrnFor each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.rn rnrnSample Inputrn4 4 5rnS.X.rn..X.rn..XDrn....rn3 4 5rnS.X.rn..X.rn...Drn0 0 0rn rnrnSample OutputrnNOrnYESrn题目大意:给出起始位置和终点位置,要求在指定的时间刚好到达终点时间,每移动一步一秒,并且不能返回。rnrn问题一:我的代码不知道哪里出错了,自己举一些例子总是过不了,更不用说AC了,请教大神们看看哪里有问题。rn问题二:小弟还是不太理解这题里面剪枝的具体作用,我的想法是:比如第R步距离终点是奇数步,且时间也剩下奇数,那么再无论向剩下的3个方向中的哪一个走一步,走了R+1步,距离终点自然变成了偶数步,且剩下的时间也是偶数的,那奇偶剪枝的意义不就没有了么?rn[code=c]#include rn#include rn#define true 1rn#define false 0rnint flag;rnint atx,aty,time,n,m;rn//atx,aty记录终点坐标rnchar map[26][26];rnvoid bfs(int x,int y,int step);rnint main()rnrn int n,m,time,i,j;//n行m列rn int x,y;//起始地点rn while(scanf("%d%d%d",&n,&m,&time)!=EOF)rn rn if(n==0&&m==0&&time==0)rn break;rn flag=false;rn //画出一个新的地图rn for(i=0;i=n||y>=m)//>=因为x和y的值来源于数组序号,最大序号为n-1rn return;rn if(flag==true||x==atx&&y==aty&&step==0)rn rn flag=true;rn return;rn rn //奇偶剪枝rn temp=abs(x-atx)+abs(y-aty)+step;rn if(temp%2!=0)return;rnrn //核心环节:深度搜索rn map[x][y]='X';rn if(map[x+1][y]!='X')rn bfs(x+1,y,step-1);rn if(flag)return;rn rn if(map[x-1][y]!='X')rn bfs(x-1,y,step-1);rn if(flag)return;rn rn if(map[x][y+1]!='X')rn bfs(x,y+1,step-1);rn if(flag)return;rn rn if(map[x][y-1]!='X')rn bfs(x,y-1,step-1);rn if(flag)return;rn rn map[x][y]='.';rnrn[/code]rn


  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他