HDU 1312 统计有多少个点

本文探讨了使用广度优先搜索(BFS)和深度优先搜索(DFS)解决地图探索问题的方法,其中地图由黑色和红色方块组成,玩家只能在黑色方块上移动,目标是计算玩家能够到达的黑色方块总数。通过实例分析,详细解释了两种搜索算法的实现过程及应用场景。

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

Red and Black

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


Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.
 

Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
 

Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
 

Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
 

Sample Output
45 59 6 13

 

题意:一幅地图,#不能走,@代表进去的点,问你有多少个点(.),@也算一个。

比较水的一道搜索题。

我写了两种代码。

BFS和DFS

#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char map[33][33];
int vis[33][33];
int n,m;
int sum;
struct node
{
	int x,y;
}
f[333];
#define inf 0xfffffff
void bfs(int x,int y)
{
	int i;
	queue<node>q;
	node st,ed;
	st.x=x;
	st.y=y;
	q.push(st);
	while(!q.empty())
	{
		st=q.front();
		q.pop();
		for(i=0;i<4;i++)
		{
			ed.x=st.x+dir[i][0];
			ed.y=st.y+dir[i][1];
			if(ed.x>=n ||ed.y>=m ||ed.x<0 ||ed.y<0 ||map[ed.x][ed.y]=='#' ||map[ed.x][ed.y]=='@')
				continue;
			map[ed.x][ed.y]='@';
			sum++;
			q.push(ed);
		}
	}
}
int main()
{
	int i,j,x,y;
	while(scanf("%d%d",&m,&n)!=EOF&&n!=0 &&m!=0)
	{
		for(i=0;i<n;i++)
			scanf("%s",map[i]);
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='@')
				{
					x=i;
					y=j;		 
				}
			}
			sum=1;
			bfs(x,y);
			printf("%d\n",sum);
	}
	return 0;
}
//广搜








#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char map[33][33];
int vis[33][33];
int n,m;
int sum;
#define inf 0xfffffff
void dfs(int x,int y)
{
    int sx,sy,i;
    sum++;
    for(i=0;i<4;i++)
    {
        sx=x+dir[i][0];
        sy=y+dir[i][1];
        if(sx>=n ||sy>=m ||sx<0 ||sy<0 ||map[sx][sy]=='#' ||map[sx][sy]=='@')
            continue;
          map[sx][sy]='@';
              dfs(sx,sy);
    } 
}
int main()
{
    int i,j,x,y;
    while(scanf("%d%d",&m,&n)!=EOF&&n!=0 &&m!=0)
    {
        for(i=0;i<n;i++)
            scanf("%s",map[i]);
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='@')
                {
                    x=i;
                    y=j;         
                }
            }
            sum=0;
            dfs(x,y);
            printf("%d\n",sum);
    }
    return 0;
}
//深搜
//深搜和广搜都是只能讲满足的点放入继续搜索,所以搜过的点要把它变成不满足的点,满足的点可以继续再搜。这就是本题的关键


 

 

内容概要:《中文大模型基准测评2025年上半年报告》由SuperCLUE团队发布,详细评估了2025年上半年中文大模型的发展状况。报告涵盖了大模型的关键进展、国内外大模型全景图及差距、专项测评基准介绍等。通过SuperCLUE基准,对45个国内外代表性大模型进行了六大任务(数学推理、科学推理、代码生成、智能体Agent、精确指令遵循、幻觉控制)的综合测评。结果显示,海外模型如o3、o4-mini(high)在推理任务上表现突出,而国内模型如Doubao-Seed-1.6-thinking-250715在智能体Agent和幻觉控制任务上表现出色。此外,报告还分析了模型性价比、效能区间分布,并对代表性模型如Doubao-Seed-1.6-thinking-250715、DeepSeek-R1-0528、GLM-4.5等进行了详细介绍。整体来看,国内大模型在特定任务上已接近国际顶尖水平,但在综合推理能力上仍有提升空间。 适用人群:对大模型技术感兴趣的科研人员、工程师、产品经理及投资者。 使用场景及目标:①了解2025年上半年中文大模型的发展现状与趋势;②评估国内外大模型在不同任务上的表现差异;③为技术选型和性能优化提供参考依据。 其他说明:报告提供了详细的测评方法、评分标准及结果分析,确保评估的科学性和公正性。此外,SuperCLUE团队还发布了多个专项测评基准,涵盖多模态、文本、推理等多个领域,为业界提供全面的测评服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值