社区讨论
ABC E WA 4 悬一关
学术版参与者 4已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @mhj23gka
- 此快照首次捕获于
- 2025/11/03 19:28 4 个月前
- 此快照最后确认于
- 2025/11/03 19:28 4 个月前
CPP
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m;
char s[100][100];
struct dat {
int x, y, aa, bb, cc, dd;
};
queue<dat> q;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int dis[50][50][13][13][13][13];
bool check(int b, int a, int d, int c) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i+n][j+m] == '#' && i > a && i <= n - b && j > c && j <= m - d) {
return false;
}
}
}
return true;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
int ii, jj;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> s[i+n][j+m];
if (s[i+n][j+m] == 'T') ii = i+n, jj = j+m;
}
}
q.push({ii, jj, 0, 0, 0, 0});
memset(dis, -0x3f, sizeof(dis));
dis[ii][jj][0][0][0][0] = 0;
while (!q.empty()) {
auto dt = q.front();
q.pop();
int x = dt.x, y = dt.y;
int aa = dt.aa, bb = dt.bb, cc = dt.cc, dd = dt.dd;
//if(aa==1&&bb==1&&cc==1&&dd==1)cout<<111<<endl;
if (check(aa, bb, cc, dd)) {
cout << dis[x][y][aa][bb][cc][dd];
return 0;
}
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
// if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
if (s[xx][yy] == '#') continue;
int dtx = xx - ii, dty = yy - jj;
int aaa = aa, bbb = bb, ccc = cc, ddd = dd;
if (dtx < 0) aaa = max(aaa, -dtx);
else bbb = max(bbb, dtx);
if (dty < 0) ccc = max(ccc, -dty);
else ddd = max(ddd, dty);
if (dis[xx][yy][aaa][bbb][ccc][ddd] >= 0) continue;
dis[xx][yy][aaa][bbb][ccc][ddd] = dis[x][y][aa][bb][cc][dd] + 1;
q.push({xx, yy, aaa, bbb, ccc, ddd});
}
}
cout << -1;
return 0;
}
回复
共 4 条回复,欢迎继续交流。
正在加载回复...