Penguins

在这里插入图片描述

这题知道是个迷宫问题,我既然广度半天广度不明白,,,看了大佬的代码才知道原来自己有多菜。。。。。(虽然一直很菜)

AC代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 20;

int d[4][2] = { {1,0},{0,-1},{0,1},{-1,0} }; //D,L,R,U

int mirror(int i) {
	if (i == 1 || i == 2) {
		return i ^ 3;
	}
	else
		return i;
}

pair<int, int> move(string g[N], int x, int y, int i) {

	int x1 = x + d[i][0];
	int y1 = y + d[i][1];

	if (x1 < 0 || x1 >= N || y1 < 0 || y1 >= N || g[x1][y1] == '#') {
		return { x,y };
	}
	return { x1,y1 };
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	string a[N], b[N];

	for (int i = 0; i < N; i++) cin >> a[i] >> b[i];

	int f[N][N][N][N];
	int g[N][N][N][N];

	memset(f, -1, sizeof(f));

	queue<tuple<int, int, int, int>> q;
	q.emplace(N - 1, N - 1, N - 1, 0);

	f[N - 1][N - 1][N - 1][0] = 0;

	while (!q.empty()) {

		auto [x1, y1, x2, y2] = q.front();
		q.pop();

		for (int i = 0; i < 4; i++) {

			auto [nx1, ny1] = move(a, x1, y1, i);
			auto [nx2, ny2] = move(b, x2, y2, mirror(i));

			if (f[nx1][ny1][nx2][ny2] < 0) {

				f[nx1][ny1][nx2][ny2] = f[x1][y1][x2][y2] + 1;
				g[nx1][ny1][nx2][ny2] = (((x1 * N + y1) * N + x2) * N + y2) * 4 + i;//这个数据压缩实属厉害,第一次见。。菜了
				q.emplace(nx1, ny1, nx2, ny2);
			}
		}
	}

	cout << f[0][N - 1][0][0] << endl;
    
    int x1 = 0,y1 = N-1,x2 = 0,y2 = 0;
    
    string ans;
    
    while(true){
        
        a[x1][y1] = b[x2][y2] = 'A';
        
        if(f[x1][y1][x2][y2] == 0){
            break;
        }
        
        int G = g[x1][y1][x2][y2];
        
        x1 = G / 4 / N / N / N;
        y1 = G / 4 / N / N % N;
        x2 = G / 4 / N % N;
        y2 = G / 4 % N;
        int d = G % 4;
        
        ans += "DLRU"[d];
    }
    
    reverse(ans.begin(), ans.end());
    
    cout << ans << endl;
    
    for(int i = 0;i<N;i++)
       cout << a[i] << " " << b[i] << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值