CodeChef:Skiing(优先队列 & BFS)

本文介绍了一个滑雪路径选择问题,目标是最小化滑雪者能够到达所有位置所需的起点数量。通过优先选择高海拔位置并进行广度优先搜索来解决该问题。

Chef and Hasan went for skiing in a rectangular field, the field can be represented as a grid of N rows and M columns, rows are numbered from 1 to N starting from top, columns are numbered from 1 to M starting from left, each cell has a a number representing the height of the snow on this particular part of the field.

after skiing for a while in the field they decided to play a game, first hasan has to choose a set of cells S not necessarily adjacent and show this set to Chef.

after that, Chef has to choose exactly one cell d not necessarily from set S and tell it to Hasan.

finally, Hasan has to choose one cell from set S and start skiing from that cell and try to reach cell d that the chef just chose, if he couldn't then Chef wins, otherwise Hasan wins.

When Hasan is skiing, he can move from one cell to another iff both cells are adjacent (they share a side) and the height of destination cell is not more than the height of current cell.

it's obvious that it's easy for Hasan to win this game, because he can simply choose all cells to be in set S, but he want the set S to have minimum number of cells to prove that he is very skilled in skiing. knowing that both players play optimally, please help Hasan to find minimum number of cells to be in set S to guarantee the winning for him.

 

Input

First line contains integer T donating the number of test-cases.

First line of each test-case contains two integers n and m donating the number of rows and columns respectively.

Each of the following n lines contains m numbers donating the heights of the field

Output

For each test-case output a single line containing a single integer donating minimum number of cells has to be in set S so that Hasan guarantee winning

Constraints

Should contain all the constraints on the input data that you may have. Format it like:

  • 1 ≤ T ≤ 100
  • 1 ≤ N, M ≤ 1,000
  • 1 ≤ sum of N in all test-cases ≤ 1,000
  • 1 ≤ sum of M in all test-cases ≤ 1,000
  • 1 ≤ height of snow ≤ 1,000,000,000

 

Example

Input:
2
3 3
1 2 2
2 1 1
2 1 1
3 3
3 2 2
2 1 1
2 1 1


Output:
2
1

 

Explanation

Example case 1. Hasan has to select at least two cells to be in S so that he guarantee he can reach all other cells starting from anyone of them, example of cells to choose (2,1) and (1,2)

Example case 2. if Hasan chose cell (1,1) to be in S then he can reach any cell starting from it and thus guarantee winning


题意:一个N*M的雪地,每个格子代表其高度。要求找出一个最小的集合S,使得集合S里面的点可以滑到雪地的任何地方。

思路:显然优先找最高的点,从它bfs下去就行了,使用优先队列优先搜索高的。

# include <bits/stdc++.h>
# define mp make_pair
# define A first
# define B second
# define pb push_back
# define PII pair<int,int>
using namespace std;
const int maxn = 1e3+3;
int a[maxn][maxn],n,m;
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
bool vis[maxn*maxn];
PII p[maxn*maxn];
void bfs(PII x)
{
    priority_queue<PII>q;
    q.push(x);
    while(!q.empty())
    {
        int x = q.top().A, y = q.top().B;
        q.pop();
        for(int i=0; i<4; ++i)
        {
            int nx = y/m + dx[i];
            int ny = y%m + dy[i];
            if(nx < 0 || ny < 0 || nx >= n || ny >= m || vis[nx*m+ny] || a[y/m][y%m] < a[nx][ny]) continue;
            vis[nx*m+ny] = true;
            q.push(mp(a[nx][ny], nx*m+ny));
        }
    }
 
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int ans = 0;
        memset(vis, false, sizeof(vis));
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; ++i)
        {
            for(int j=0; j<m; ++j)
            {
                scanf("%d",&a[i][j]);
                p[i*m+j] = mp(a[i][j], i*m+j);
            }
        }
        sort(p, p+n*m, greater<PII>());
        for(int i=0; i<n*m; ++i)
        {
            if(!vis[p[i].B])
            {
                ++ans;
                vis[p[i].B] = true;
                bfs(p[i]);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


边缘计算环境中基于启发式算法的深度神经网络卸载策略(Matlab代码实现)内容概要:本文围绕&ldquo;边缘计算环境中基于启发式算法的深度神经网络卸载策略&rdquo;展开,提出了一种利用启发式算法优化深度神经网络任务在边缘计算环境中的计算卸载方案。通过Matlab代码实现,研究如何将复杂的神经网络任务合理分配到边缘设备与云端,以降低延迟、节省能耗并提高系统效率。文中重点探讨了任务分割、资源调度、通信开销与计算能力之间的权衡,并借助启发式算法(如遗传算法、粒子群优化等)寻找近似最优的卸载决策。该方法适用于资源受限的边缘计算场景,具有较强的工程应用价值。; 适合人群:具备一定编程基础和算法知识,熟悉Matlab工具,从事边缘计算、物联网、人工智能或智能优化方向研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于解决边缘智能中深度学习任务的高效部署问题;②为移动端或嵌入式设备上的AI应用提供低延迟、低功耗的任务卸载策略;③支撑智慧城市、自动驾驶、工业物联网等实时性要求高的应用场景。; 阅读建议:建议读者结合提供的Matlab代码进行实验复现,深入理解算法实现细节与系统建模过程,同时可尝试替换不同启发式算法或调整网络结构以进一步优化性能。
航拍图像多类别实例分割数据集 一、基础信息 &bull; 数据集名称:航拍图像多类别实例分割数据集 &bull; 图片数量: 训练集:1283张图片 验证集:416张图片 总计:1699张航拍图片 &bull; 训练集:1283张图片 &bull; 验证集:416张图片 &bull; 总计:1699张航拍图片 &bull; 分类类别: 桥梁(Bridge) 田径场(GroundTrackField) 港口(Harbor) 直升机(Helicopter) 大型车辆(LargeVehicle) 环岛(Roundabout) 小型车辆(SmallVehicle) 足球场(Soccerballfield) 游泳池(Swimmingpool) 棒球场(baseballdiamond) 篮球场(basketballcourt) 飞机(plane) 船只(ship) 储罐(storagetank) 网球场(tennis_court) &bull; 桥梁(Bridge) &bull; 田径场(GroundTrackField) &bull; 港口(Harbor) &bull; 直升机(Helicopter) &bull; 大型车辆(LargeVehicle) &bull; 环岛(Roundabout) &bull; 小型车辆(SmallVehicle) &bull; 足球场(Soccerballfield) &bull; 游泳池(Swimmingpool) &bull; 棒球场(baseballdiamond) &bull; 篮球场(basketballcourt) &bull; 飞机(plane) &bull; 船只(ship) &bull; 储罐(storagetank) &bull; 网球场(tennis_court) &bull; 标注格式:YOLO格式,包含实例分割的多边形坐标,适用于实例分割任务。 &bull; 数据格式:航拍图像数据。 二、适用场景 &bull; 航拍图像分析系统开发:数据集支持实例分割任务,帮助构建能够自动识别和分割航拍图像中各种物体的AI模型,用于地理信息系统、环境监测等。 &bull; 城市
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值