深搜基础题目 杭电 HDU 1241

HDU 1241 是深搜算法的入门题目,递归实现。

原题目传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1241

代码仅供参考,c++实现:

#include <iostream>
using namespace std;

char land[150][150];
int p,q;

void dfs(int x,int y){
    land[x][y] = ‘*‘;
    if(land[x-1][y]!= ‘@‘ && land[x+1][y] != ‘@‘ && land[x][y-1] != ‘@‘ && land[x][y+1] != ‘@‘ && land[x-1][y+1]!= ‘@‘ && land[x+1][y-1] != ‘@‘ && land[x-1][y-1] != ‘@‘ && land[x+1][y+1] != ‘@‘)
        return ;

    if(x+1 > 0 && y+1 > 0 && x+1 <= p && y+1 <= q && land[x+1][y+1]== ‘@‘) { dfs(x+1,y+1);  }
    if(x+1 > 0 && y-1 > 0 && x+1 <= p && y-1 <= q && land[x+1][y-1]== ‘@‘) { dfs(x+1,y-1);  }
    if(y-1 > 0 && x-1 > 0 && x-1 <= p && y-1 <= q && land[x-1][y-1]== ‘@‘) { dfs(x-1,y-1);  }
    if(y+1 > 0 && x-1 > 0 && x-1 <= p && y+1 <= q && land[x-1][y+1]== ‘@‘) { dfs(x-1,y+1);  }

    if(x+1 > 0 && y > 0 && x+1 <= p && y <= q && land[x+1][y]== ‘@‘) { dfs(x+1,y);  }
    if(x-1 > 0 && y > 0 && x-1 <= p && y <= q && land[x-1][y]== ‘@‘) { dfs(x-1,y);  }
    if(y-1 > 0 && x > 0 && x <= p && y-1 <= q && land[x][y-1]== ‘@‘) { dfs(x,y-1);  }
    if(y+1 > 0 && x > 0 && x <= p && y+1 <= q && land[x][y+1]== ‘@‘) { dfs(x,y+1);  }

    return ;
}
int main(int argc, const char * argv[]) {

    int num;
    cin>>p>>q;
    while(p>0 && q>0){

            num = 0;
            for(int i = 1; i <= p ;i++){
                for(int j = 1; j<= q; j++){
                    cin>>land[i][j];
                }
            }
            for(int i = 1; i <= p ;i++){
                for(int j = 1; j<= q; j++){
                    if(land[i][j] == ‘@‘){
                        num++;
                        dfs(i,j);
                    }

                }
            }
            cout<< num<<endl;
        cin>>p>>q;
    }
    return 0;
}
时间: 2024-10-10 17:59:01

深搜基础题目 杭电 HDU 1241的相关文章

杭电 HDU 1098 Ignatius&#39;s puzzle

Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7068    Accepted Submission(s): 4883 Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no

01背包基础 (杭电2602)

01背包问题: 有一个体积为V的背包,有n件物品,每件物品的体积,价值分别为w[i],p[i];要从n件物品中选些放入背包中,使背包里物品的总价值最大. 动态方程:c[i][j]=max(c[i-1][j],c[i-1][j-w[i]]+p[i]). 有关动态方程方面的代码: for (int i = 1; i <= n; i++) { for (int j = 1; j <= total_weight; j++) { if (w[i] > j) { c[i][j] = c[i-1][j

【深搜加剪枝五】HDU 1010 Tempter of the Bone

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326    Accepted Submission(s): 17567 Problem Description The doggie found a bone in an ancient maze, which fascinated him a l

杭电 HDU ACM 5186 zhx&#39;s submissions

zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1892    Accepted Submission(s): 507 Problem Description As one of the most powerful brushes, zhx submits a lot of code on many

杭电HDU ACM Uncle Tom&#39;s Inherited Land*(二分图匹配 建模)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2496    Accepted Submission(s): 1028 Special Judge Problem Description Your old uncle Tom inherited a piece of land f

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

杭电 HDU 1038 Biker&#39;s Trip Odometer

Biker's Trip Odometer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4745    Accepted Submission(s): 3144 Problem Description Most bicycle speedometers work by using a Hall Effect sensor faste

杭电 HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4904    Accepted Submission(s): 2743 Problem Description The digital root of a positive integer is found by summing the digi

杭电hdu 4861 Couple doubi

杭电 2014多校联训第一场   1001   Couple doubi   逗比夫妇 这标题我就不多说什么了. 题意:有K个球在桌上,每个球都有价值,第i个球的价值是1^i+2^i+...+(p-1)^i (mod p).其中p是一个素数,之后逗比男先选球,最后所有球总分高的获胜.如果逗比男获胜,那么输出“YES”否则输出“NO”.(逗比男和逗比女都采取最有策略). 当然这也p是奇素数的一个重要公式.曾有题是这个公式求和.当然如果你知道就很简单了.如果不知道,就打表找规律吧. 根据这一重要的公