题解分享
题解分享简介
回忆迷宫(编程题) - 题解
```
#include
using namespace std;
#define maxn 105
int tu[maxn][maxn]; // tu[i][j]为0表示未定,为1表示为迷宫内空地,为2表示为墙,为3表示迷宫外空地
unordered_map<char, int> dire;
int n, step[maxn];
int initx, inity; // 初始位置的横纵坐标,比如样例中的initx=6,inity=6
int hangshu, lieshu; // 整个图的行数和列数,比如样例中的hangshu为7,lieshu为7
int mymove[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
bool vis[maxn][maxn];
void getinfor() {
int x = 0, y = 0;
initx = max(initx, x + 2);
inity = max(inity, y + 2);
for (int i = 1; i <= n; i++) {
switch (step[i]) {
case 1:
x++;
initx = max(initx, x + 2);
break;
case 2:
x--;
break;
case 3:
y++;
inity = max(inity, y + 2);
break;
case 4:
y--;
break;
}
}
}
void dfs(int x, int y) {
for (int i = 1; i <= n; i++) {
tu[x][y] = 1;
hangshu = max(hangshu, x);
lieshu = max(lieshu, y);
switch (step[i]) {
case 1:
x--;
break;
case 2:
x++;
break;
case 3:
y--;
break;
case 4:
y++;
break;
}
}
tu[x][y] = 1;
hangshu = max(hangshu, x);
lieshu = max(lieshu, y);
hangshu++;
lieshu++; // 因为右边和上边要有墙
}
void dfs1(int x, int y) { // 这是为了将围墙围住的部分置为围墙
int nextx, nexty;
for (int i = 0; i < 4; i++) {
nextx = x + mymove[i][0];
nexty = y + mymove[i][1];
if (tu[nextx][nexty] == 0 && 1 <= nextx && nextx <= hangshu && 1 <= nexty && nexty <= lieshu) {
tu[nextx][nexty] = 3;
dfs1(nextx, nexty);
}
}
}
int main() {
scanf("%d", &n);
dire['U'] = 1;
dire['D'] = 2;
dire['L'] = 3;
dire['R'] = 4;
string str;
cin >> str;
for (int i = 1; i <= n; i++) {
step[i] = dire[str[i - 1]];
}
memset(tu, 0, sizeof(tu));
getinfor(); // 根据step[]获取一些信息
dfs(initx, inity);
for (int i = 1; i <= hangshu; i++) {
for (int j = 1; j <= lieshu; j++) {
vis[i][j] = 1;
if (tu[i][j] == 0) { // 这个元素未定
if (tu[i - 1][j] == 1 || tu[i + 1][j] == 1 || tu[i][j - 1] == 1 || tu[i][j + 1] == 1) {
tu[i][j] = 2; // 设置为围墙
}
}
}
}
for (int i = 1; i <= hangshu; i++) {
if (i == 1 || i == hangshu) {
for (int j = 1; j <= lieshu; j++) {
if (tu[i][j] == 0) {
tu[i][j] = 3; // 置为迷宫外空地
dfs1(i, j);
}
}
} else {
if (tu[i][1] == 0) {
tu[i][1] = 3;
dfs1(i, 1);
}
if (tu[i][lieshu] == 0) {
tu[i][lieshu] = 3;
dfs1(i, lieshu);
}
}
}
// 以下是输出
for (int i = 1; i <= hangshu; i++) {
for (int j = 1; j <= lieshu; j++) {
if (tu[i][j] == 2 || tu[i][j] == 0) printf("*");
else printf(" ");
}
putchar('\n');
}
return 0;
}
```
查看全文
1
0
0
11
回忆迷宫(编程题) - 题解
```
#include<bits/stdc++.h>
using namespace std;
char mmap[220][220]; //开220的数组 从中心(110,110)开始移动防止越界
string route;
int n;
bool flag[220][220];
int d[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};
int bx1,bx2,by1,by2;
bool outcheck[220][220];
bool check(int x,int y){ //验证一个既不是空地也不是墙的点是否连接边界
queue <pair<int,int>> q;
q.push({x,y});
outcheck[x][y] = true;
while(!q.empty()){
auto t = q.front();
q.pop();
int ox = t.first,oy=t.second;
if(ox == bx1 || ox == bx2 || oy == by1 || oy == by2 ){
return false; //边界联通,为外
}
for(int i = 0 ; i<4;i++){
int nx = ox + d[i][0],ny = oy+d[i][1];
if(!flag[nx][ny] && !outcheck[nx][ny]){
q.push({nx,ny});
outcheck[nx][ny] = true;
}
}
}
return true;//在墙内
}
int main(){
cin >> n;
cin >> route;
int x = 110,y=110;
bx1 = 110;
bx2 = 110;
by1 = 110;
by2 = 110;
map<char,pair<int,int>> order;
order['U'] = {-1,0};
order['L'] = {0,-1};
order['D'] = {1,0};
order['R'] = {0,1};
flag[x][y] = true;
mmap[x][y] = ' ';
for(int i = 0 ; i<220;i++){
for(int j = 0 ; j<220;j++){
mmap[i][j] = ' ';
}
}
flag[x][y] = true;
mmap[x][y] = ' ';
for(int j = 0 ; j<4 ; j++){
int nx = x + d[j][0];
int ny = y + d[j][1];
if(!flag[nx][ny]){
flag[nx][ny] = true;
mmap[nx][ny] = '*';
}
if(nx < bx1) bx1 = nx;
if(nx > bx2) bx2 = nx;
if(ny < by1) by1 = ny;
if(ny > by2) by2 = ny;
} //初始点处理
for(int i = 0; i<=n;i++){
x = x + order[route[i]].first;
y = y + order[route[i]].second;
flag[x][y] = true;
mmap[x][y] = ' ';
for(int j = 0 ; j<4 ; j++){
int nx = x + d[j][0];
int ny = y + d[j][1];
if(!flag[nx][ny]){
flag[nx][ny] = true;
mmap[nx][ny] = '*';
}
if(nx < bx1) bx1 = nx;
if(nx > bx2) bx2 = nx;
if(ny < by1) by1 = ny;
if(ny > by2) by2 = ny;
}
}//移动后处理
for(int i = bx1;i<=bx2;i++){
for(int j = by1;j<=by2;j++){
if(check(i,j) && !flag[i][j]){
mmap[i][j] = '*';
}
memset(outcheck,false,sizeof(outcheck));
}
} //验证未访问的点是外界 还是需要补墙
for(int i = bx1;i<=bx2;i++){
for(int j = by1;j<=by2;j++){
cout << mmap[i][j];
}
cout << endl;
}
return 0;
}
//23
//RRRRRUULLLLDDDDDRRURRDD
```
查看全文
0
0
0
3



