DFS【The song of Ice and Fire】

本文深入探讨了游戏开发领域的核心技术,包括游戏引擎、动画、3D空间视频等,旨在为开发者提供全面的游戏开发知识和实践经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

    House Lannister of Casterly Rock is one of the Great Houses of Seven Kingdoms, and the principal house of the Westerlands. Their seat is Casterly Rock, though another branch exists that is based in nearby Lannisport. Their sigil is a golden lion on a field of crimson.Their official motto is "Hear Me Roar!" However, their unofficial motto, equally well known, is "A Lannister always pays his debts."The Warden of the West is traditionally a Lannister

    The gold of Casterly Rock,Castamere, the Pendric Hills and the Golden Tooth has made them the wealthiest of the Great Houses, the keep at Casterly Rock sits literally on top of a gold mine, built into the very rock. From that lofty yet uncomfortable position, House Lannister rules over the Westerlands and influences all of Westeros, by virtue of the kingdom’s purse strings.

    One day, Tyrion Lannister come to his gold mine field,which is a large rectangular in the land.If the two gold deposits are adjacent, then it will be one mineral resources. Your tesk is tell him how many mineral resources are contained in this land.

输入格式

The input consists of serveral test cases. For each case contains two positive integer N and M(1 <= M , N <= 100), If N = 0 it means the end of the input; Each of the following N lines contain M characters. Each character is '$' or '#' reflect that there is gold or not.

输出

For each test case, print the number of mineral resources ,and one line one case.

 

样例输入

3 4
##$$
$##$
##$#
1 5
###$$
2 2
##
##
0 0

样例输出

2
1
0



#include<cstring>
#include<iostream>
char x[110][110];
int rem[110][110];
using namespace std;
void dfs(int i,int j)
{
    if(rem[i][j])
        rem[i][j]=0;
    else if(rem[i][j]==0)
        return;
    dfs(i-1,j-1);
    dfs(i+1,j+1);
    dfs(i-1,j+1);
    dfs(i+1,j-1);
    dfs(i-1,j);
    dfs(i+1,j);
    dfs(i,j-1);
    dfs(i,j+1);

}
int main()
{
    int i,j,a,b,sum,k,t;
    while(cin>>a>>b)
    {
        if(a==0&&b==0)
            break;
        memset(rem,0,sizeof(rem));
        sum=0;
        for(i=1; i<=a; i++)
            for(j=1; j<=b; j++)
            {
                cin>>x[i][j];
                if(x[i][j]=='$')
                    rem[i][j]=1;
            }
        for(i=1; i<=a; i++)
            for(j=1; j<=b; j++)
                if(rem[i][j])
                {
                    sum++;
                    dfs(i,j);
                }
        cout<<sum<<endl;
    }
    return 0;
}

(1):深搜写法,递归  from  魏继鹏

#include<stdio.h>
#include<stdlib.h>
char m[100][100];//数组定义在函数外面,不然你会很悲剧的发现自己的程序无法输入数据。
int a,b;
int  f[8][2]= {{-1,0},{0,1},{1,0},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}}; //*周围八个点的坐标集合。用两个数组来储存
void dfs(int k,int s)//*调用函数,bfs*//
{
    int i;
    m[k][s]=’#’;//*将搜索过的标记成陆地*//
    for(i=0; i<8; i++)
        if(m[k+f[i][0]][s+f[i][1]]==’$’)//*当周围有金矿的时候,再次搜索其他方位的、*//
            dfs(k+f[i][0],s+f[i][1]);//*不断的调用函数,继续往下一深度搜索,直到把周围的全部搜完*//
}
int main()
{
    int n,i,j,count;
    scanf("%d",&n);
    while(n--)
    {
        count=0;
        scanf("%d%d",&a,&b);
        for(i=1; i<=a; i++)
            for(j=1; j<=b; j++)
                scanf("%c” ,&m[i][j]);
                      for(i=1;i<=a;i++)
                      for(j=1;j<=b;j++)
                  {
                      if(m[i][j]==’$’)
                  {
                      count++;
                      dfs(i,j);//*不断的调用函数*//
                  }
                  }
                      printf("%d\n",count);
                  }
                      return 0;
                  }



AC 代码(2):广搜写法,队列  from  魏继鹏

#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
int a,b;
char m[100][100];   //地图
int f[8][2]={{-1,0},{0,1},{1,0},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}}; //八个方向
struct point//*定义队列元素*//
{
int x,y;
};
queue<point> v;//*建立一个队列*//
void bfs(int k,int s)
{
int i;
point L,p,q;
L.x=k;L.y=s;
v.push(L);
m[k][s]=’#’;//*标记*//
while(!v.empty())
{
p=v.front();
for(i=0;i<4;i++)
{
if(m[p.x+f[i][0]][p.y+f[i][1]]==’$’)//*判断是否是金矿或者已经搜索过*//
{
m[p.x+f[i][0]][p.y+f[i][1]]=’$’;
q.x=p.x+f[i][0];q.y=p.y+f[i][1];//*满足条件的放入队列中*//
v.push(q);
}
}
v.pop();//*清空队列*//
}
}
int main()
{
int n,i,j,count;
scanf("%d",&n);
while(n--)
{
count=0;
scanf("%d%d",&a,&b);
for(i=1;i<=a;i++) 
for(j=1;j<=b;j++)
scanf("%c",&m[i][j]);
for(i=1;i<=a;i++)
for(j=1;j<=b;j++)
if(m[i][j]==’$’)
{
count++;
bfs(i,j); //每找到一个符合要求的点,对其进行搜索
}
printf("%d\n",count);
}
return 0;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值