当前位置:网站首页>AcWing 1131. Saving Private Ryan (the shortest way)
AcWing 1131. Saving Private Ryan (the shortest way)
2022-07-02 19:32:00 【Mr. Qiao Da】
AcWing 1131. Saving Private Ryan
Dealing with data is a very troublesome problem , How to build drawings to record walls and doors , utilize set Record gate , Match with binary record key , This writing operation needs to be learned to remember
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int, int>PII;
const int N = 15, M = 360, INF = 0x3f3f3f3f;
int h[N * N], ne[M], e[M], w[M], idx; // Store adjacency table
int dist[N * N][1 << 11]; // Storage status
deque<PII>q; // Storage status
int n, m, p, k, S;
set<PII>edge; // Record gate
int g[N][N];
int dx[4] = {
1, 0, -1, 0};
int dy[4] = {
0, 1, 0, -1};
int key[N * N];
bool st[N * N][1 << 11];
void add(int a, int b, int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++ ;
}
void build(){
// Drawing
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= m; j ++ ){
for(int k = 0; k < 4; k ++ ){
int xx = dx[k] + i;
int yy = dy[k] + j;
if(!xx || !yy || xx > n || yy > m) continue; // If out of bounds , Just skip it
int a = g[i][j];
int b = g[xx][yy];
if(!edge.count({
a, b})) add(a, b, 0); // If there is no door in this place , It's just a wall
}
}
}
}
int bfs(){
memset(dist, 0x3f, sizeof dist);
dist[1][0] = 0; // The status is the first point , The key is 0
q.push_back({
1, 0}); // The status is the first point , The key is 0
while(q.size()){
auto op = q.front();
q.pop_front();
if(st[op.x][op.y]) continue; // If this state has been used
st[op.x][op.y] = true;
if(op.x == n * m) return dist[op.x][op.y];
if(key[op.x]){
// If there is a key in this place
int state = op.y | key[op.x];
if(dist[op.x][state] > dist[op.x][op.y]){
dist[op.x][state] = dist[op.x][op.y];
q.push_front({
op.x, state});
}
}
for(int i = h[op.x]; ~i; i = ne[i]){
int j = e[i];
if(w[i] && !(op.y >> w[i] - 1 & 1)) continue; // If there is a door here but there is no key
if(dist[j][op.y] > dist[op.x][op.y] + 1){
//
dist[j][op.y] = dist[op.x][op.y] + 1;
q.push_back({
j, op.y});
}
}
}
return -1;
}
int main()
{
cin>>n>>m>>p>>k;
for(int t = 1, i = 1; i <= n; i ++ ){
for(int j = 1; j <= m; j ++ ){
g[i][j] = t ++ ;
}
}
memset(h, -1, sizeof h);
while(k -- ){
int x1, y1, x2, y2, z;
cin>>x1>>y1>>x2>>y2>>z;
int a = g[x1][y1], b = g[x2][y2];
edge.insert({
a, b}); // Record door or wall
edge.insert({
b, a});
if(z){
add(a, b, z);
add(b, a, z);
}
}
build(); // Drawing
cin>>S;
while(S -- ){
int a, b, c;
cin>>a>>b>>c;
key[g[a][b]] |= 1 << c - 1; // Store keys in binary
}
cout<<bfs()<<endl;
return 0;
}
边栏推荐
- Date tool class (updated from time to time)
- C文件输入操作
- End-to-End Object Detection with Transformers(DETR)论文阅读与理解
- Yolov3 trains its own data set to generate train txt
- 4274. Suffix expression - binary expression tree
- Reading notes of code neatness
- Watchful pioneer world outlook Architecture - (how does a good game come from)
- MySQL高级(进阶)SQL语句
- 453-atoi函数的实现
- Chapter 7 - class foundation
猜你喜欢
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
程序猿入门攻略(十二)——数据的存储
zabbix5客户端安装和配置
Use cheat engine to modify money, life and stars in Kingdom rush
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
codeforces每日5题(均1700)-第四天
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
随机推荐
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
程序猿入门攻略(十二)——数据的存储
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
Watchful pioneer world outlook Architecture - (how does a good game come from)
452-strcpy、strcat、strcmp、strstr、strchr的实现
Emmet basic syntax
Machine learning notes - time series prediction research: monthly sales of French champagne
2022 software engineering final exam recall Edition
AcWing 1131. 拯救大兵瑞恩 题解(最短路)
Golang concurrent programming goroutine, channel, sync
多态的理解以及作用
电脑使用哪个录制视频软件比较好
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
4274. Suffix expression - binary expression tree
AcWing 1137. 选择最佳线路 题解(最短路)
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
Golang:[]byte to string
虚拟机初始化脚本, 虚拟机相互免秘钥
juypter notebook 修改默认打开文件夹以及默认浏览器