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

P9904 [COCI 2023/2024 #1] Labirint 题解

题目传送门

解题思路

看到这种题目,感觉只能暴力或者暴力优化通过。

考虑记忆化搜索,首先设 d p i , j , s dp_{i,j,s} dpi,j,s 表示走到第 i i i 行,第 j j j 列时,遇到的所有颜色 s s s。这里我们使用了二进制数表示遇到的所有颜色,比如串 0110 0110 0110 表示遇到了第 2 , 3 2,3 2,3 种颜色。

我们设 ( x , y ) (x,y) (x,y) 表示可以通过 ( i , j ) (i, j) (i,j) 得到的单元格。那么 d p i , j , s = min ⁡ ∣ x − i ∣ + ∣ y − j ∣ ≤ 1 d p x , y , s or  2 t o ( a x , y ) dp_{i,j,s} = \min\limits_{|x - i| + |y - j| \leq 1} dp_{x,y,s \ \text{or}\ 2^{to(a_{x,y})}} dpi,j,s=xi+yj1mindpx,y,s or 2to(ax,y),其中 or \text{or} or 表示或运算, t o ( x ) to(x) to(x) 表示字符 x x x 所对应的颜色编号(可以自己定义)。关于 s s s 的操作:如果 s s s 1010 1010 1010 t o ( x ) = 2 to(x) = 2 to(x)=2,那么 s or  2 2 s\ \text{or}\ 2^2 s or 22,那么 s s s 就变成了 1110 1110 1110

CODE:

#include <bits/stdc++.h>
using namespace std;
#define int long long
char a[110][110], b[110][110];
int n, m, sx, sy, ex, ey, cnt;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int k[10];
inline int geto(char a) {if (a == 'P') {return 0;} else if (a == 'C') {return 1;} else if (a == 'Z') {return 2;}return 3;
}
int ans;
int dp[110][110][(1 << 4) + 10];
inline int dfs(int x, int y, int res) {if (x == ex && y == ey) {return dp[x][y][res] = ((res & 1) != 0) + ((res & 2) != 0) + ((res & 4) != 0) + ((res & 8) != 0);}if (dp[x][y][res] != 5) {return dp[x][y][res];}dp[x][y][res] = 6;
//	cout << x << ' ' << y << endl;for (int i = 0; i < 4; i++) {int xx = x + dx[i];int yy = y + dy[i];if (xx > 0 && yy > 0 && xx <= n && yy <= m) {int p;if (abs(xx - x) == 1) {p = geto(b[min(x, xx)][y]);} else {p = geto(a[x][min(y, yy)]);}int now = res | (1 << p);dp[x][y][res] = min(dp[x][y][res], dfs(xx, yy, now));}}return dp[x][y][res];
}
signed main() {ios::sync_with_stdio(false);ios_base::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> n >> m;for (int i = 1; i <= n; i++) {for (int j = 1; j < m; j++) {cin >> a[i][j];}}for (int i = 1; i < n; i++) {for (int j = 1; j <= m; j++) {cin >> b[i][j];}}int q;cin >> q;while (q--) {cin >> sx >> sy >> ex >> ey;for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {for (int k = 0; k <= 16; k++) {dp[i][j][k] = 5;}}}cout << dfs(sx, sy, 0) << "\n";}return 0;
}
http://www.xdnf.cn/news/188929.html

相关文章:

  • codeforcesB. Alice‘s Adventures in Permuting
  • 「地平线」创始人余凯:自动驾驶尚未成熟,人形机器人更无从谈起
  • C++编程指南39 - 不要特化函数模板
  • Callable Future 实现多线程按照顺序上传文件
  • yolov5 源码 +jupyter notebook 笔记 kaggle
  • quickbi finebi 测评(案例讲解)
  • MySQL 主从复制
  • 图像保边滤波之BEEPS滤波算法
  • KUKA机器人自动备份设置
  • vscode 使用gitcode团队管理项目
  • 区块链随学随记
  • jetson nano上Ubuntu系统调用摄像头bug
  • 塔能科技:点亮节能之光,赋能工厂与城市
  • 20250428-AI Agent:智能体的演进与未来
  • 包装产线通过canopen转Profinet网关控制伺服
  • 关于常量指针和指向常量的指针
  • 泰山派常用命令
  • map和set:
  • ai环境conda带torch整体迁移。
  • 一文了解 模型上下文协议(MCP)
  • word插入APA格式的参考文献
  • NGINX ngx_http_addition_module 模块响应体前后注入内容
  • VS2022+OpenCasCade配置编译
  • 【leetcode】最长公共子路径问题
  • 从大众传媒到数字生态:开源AI智能名片链动2+1模式S2B2C商城小程序驱动的营销革命
  • prompt提示词编写技巧
  • Context7 MCP:提供实时、版本特定的文档以解决AI幻觉问题
  • Go 语言入门:(一) 环境安装
  • 大语言模型(LLMs)微调技术总结
  • web 基础与 http 协议