当前位置: 首页 > news >正文

洛谷题解 | CF111C Petya and Spiders

目录

    • 题目描述
    • 输入格式
    • 输出格式
    • 输入输出样例 #1
      • 输入 #1
      • 输出 #1
    • 输入输出样例 #2
      • 输入 #2
      • 输出 #2
    • 说明/提示
    • 题目简化
    • 题目思路
    • AC 代码

题目描述

Little Petya loves training spiders. Petya has a board $ n×m $ in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.

输入格式

The first line contains two space-separated integers $ n $ and $ m $ ( $ 1<=n,m<=40,n·m<=40 $ ) — the board sizes.

输出格式

In the first line print the maximum number of cells without spiders.

输入输出样例 #1

输入 #1

1 1

输出 #1

0

输入输出样例 #2

输入 #2

2 3

输出 #2

4

说明/提示

In the first sample the only possible answer is:

s

In the second sample one of the possible solutions is:

<br></br>rdl<br></br>rul<br></br>s denotes command “stay idle”, l, r, d, u denote commands “crawl left”, “crawl right”, “crawl down”, “crawl up”, correspondingly.

题目简化

求在一个地图上放置障碍物的最小数量,使得所有空白格子都能被覆盖到。

题目思路

搜索。

首先遍历棋盘找到第一个没有蜘蛛的格子,如果找不到这样的格子,则更新最优解为当前留空格子数量 x x x

对于没有蜘蛛的格子,尝试在其周围放置蜘蛛,调用搜索函数,更新留空格子数量 x x x,直到所有格子都被蜘蛛占满或者当前留空格子数量 ≥ \ge 最优解(也就是说一定是最优解法)时终止。

在尝试放置蜘蛛后,恢复棋盘。

AC 代码

#include<bits/stdc++.h>
using namespace std;
int n,m,best,dx[5] = {0,1,0,-1,0},dy[5] = {-1,0,1,0,0},a[45][45];
///判断坐标是否在棋盘范围内
bool ok(int x,int y){return x >= 0 && x < n && y >= 0 && y < m;
}
void dfs(int x){vector<pair<int,int> > tmp;int xx = -1,yy = -1;//寻找没有蜘蛛的格子for(int i = 0;i < n;i++){for(int j = 0;j < m;j++){if(!a[i][j]){xx = i,yy = j;break;}}if(xx != -1) break;}//没有找到if(xx == -1){best = x;return;}if(x + 1 >= best) return;for(int i = 0;i < 5;i++){//尝试在空格子周围放置蜘蛛int x1 = xx + dx[i];int y1 = yy + dy[i];if(ok(x1,y1)){tmp.clear();for(int j = 0;j < 5;j++){//遍历当前位置(x1, y1)周围的五个点int x2 = x1 + dx[j];int y2 = y1 + dy[j];if(ok(x2,y2) && !a[x2][y2]){ //判断该点是否在棋盘范围内且没有蜘蛛tmp.push_back(make_pair(x2,y2)); //保存当前可以放置蜘蛛的位置a[x2][y2] = 1; //将蜘蛛标记为已占据}}dfs(x + 1); //搜索下一个没有蜘蛛的位置for(int j = 0;j < tmp.size();j++) a[tmp[j].first][tmp[j].second] = 0; //恢复之前放置蜘蛛后的状态}}
}
int main()
{cin >> n >> m;best = n * m;dfs(0);cout << n * m - best;return 0;
}

创作不易,白嫖不好,各位的支持和认可,就是我创作的最大动力,如果喜欢我的文章,给个关注吧!

冰焰狼 | 文

如果本篇博客有任何错误,请批评指教,不胜感激 !

相关文章:

  • 算法基础学习|02归并排序——分治
  • go单向链表
  • 阿里千问Qwen3技术解析与部署指南 :混合推理架构突破性优势与对DeepSeek R1的全面超越
  • 软件测试基础知识详解
  • 【VLNs篇】01:视觉语言导航(VLN)中的LLM角色
  • 关于flex布局
  • scratch代码——游戏开发 【弹簧与反弹】
  • ArrayList的elementData.length和size
  • 双向流热固耦合的收敛
  • (leetcode) 力扣100 4.移动零(两种O(n)方法 双指针)
  • 大模型核心技术及架构解析
  • 2025.4.29_STM32_看门狗WDG
  • 一文读懂 JavaScript 中的深浅拷贝
  • C#学习——类型、变量
  • 学习在暑假避免躺平和内卷(马井堂)
  • 16、路由守卫:设置魔法结界——React 19 React Router
  • 系统安装 ios放同一个u盘 ventory使用+windows安装,双系统互相访问中间盘
  • PPT/WORD如何实现Shift键加鼠标滚轮实现左右滑动
  • 嵌入式复习第一章
  • 拆固态硬盘短接开卡+ as ssd benchmark查看硬盘读写速度
  • 160名老人报旅行团被扔服务区?张家界官方通报
  • 自称“最美”通缉犯出狱当主播?央广网:三观怎能跟着“五官”跑
  • “上海-日喀则”直飞航线正式通航,将于5月1日开启首航
  • 《奇袭白虎团》原型人物之一赵顺合辞世,享年95岁
  • 原创话剧风向标!这个展演上《大宅门》《白鹿原》先后上演
  • 十四届全国人大常委会第十五次会议在京举行,审议民营经济促进法草案等