题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=725
样例输入:
2
XXXXXXXXX
X X X
X * X
X X X
XXXXXXXXX
X X
X X
X X
XXXXX
_____
XXXXX
X X
X * X
X X
XXXXX
_____
样例输出:
XXXXXXXXX
X###X###X
X#######X
X###X###X
XXXXXXXXX
X X
X X
X X
XXXXX
_____
XXXXX
X###X
X###X
X###X
XXXXX
_____
分析:
又是一道搜索入门的简单题,不解释。 天天刷水题,今天特别多。今天的度假很happy,在这样的大热天,就应该水水降降温。
代码1: DFS
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
char map[35][100];
int vis[35][100], row;
int dir[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};
inline bool isWall(char ch){
if(isprint(ch) && ch!=' ' && ch!='*' && ch!='-') return true;
return false;
}
void dfs(int x,int y){
for(int i=0; i<4; ++i){
int dx=x+dir[i][0], dy=y+dir[i][1];
if(dx>=0 && dx<row && dy>=0 && dy<strlen(map[dx]) && !isWall(map[dx][dy]) && map[dx][dy]!='#' && !vis[dx][dy]){
vis[dx][dy] = true;
map[dx][dy] = '#';
dfs(dx, dy);
}
}
}
int main(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif
int T, start_x, start_y;
scanf("%d%*c", &T);
while(T--){
row=0;
bool isFind = false;
while(gets(map[row])){
if(map[row][0]=='_') break;
if(!isFind){
for(int i=0; i<strlen(map[row]); ++i){
if(map[row][i]=='*'){
start_x = row, start_y = i;
isFind = true;
break;
}
}
}
++row;
}
memset(vis, 0, sizeof(vis));
dfs(start_x, start_y);
for(int i=0; i<=row; ++i)
puts(map[i]);
}
return 0;
}
代码2:BFS
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
char map[35][100];
int vis[35][100], row;
int dir[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};
struct Node{int x,y; };
Node que[10000];
inline bool isWall(char ch){
if(isprint(ch) && ch!=' ' && ch!='*' && ch!='-') return true;
return false;
}
void bfs(int x,int y){
int front=0, rear=1;
que[0].x = x, que[0].y = y;
while(front<rear){
Node t = que[front++];
for(int i=0; i<4; ++i){
int dx=t.x+dir[i][0], dy=t.y+dir[i][1];
if(dx>=0 && dx<row && dy>=0 && dy<strlen(map[dx]) && !isWall(map[dx][dy]) && map[dx][dy]!='#' && !vis[dx][dy]){
vis[dx][dy] = true;
map[dx][dy] = '#';
Node q;
q.x = dx, q.y = dy;
que[rear++] = q;
}
}
}
}
int main(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif
int T, start_x, start_y;
scanf("%d%*c", &T);
while(T--){
row=0;
bool isFind = false;
while(gets(map[row])){
if(map[row][0]=='_') break;
if(!isFind){
for(int i=0; i<strlen(map[row]); ++i){
if(map[row][i]=='*'){
start_x = row, start_y = i;
isFind = true;
break;
}
}
}
++row;
}
memset(vis, 0, sizeof(vis));
bfs(start_x, start_y);
for(int i=0; i<=row; ++i)
puts(map[i]);
}
return 0;
}
分享到:
相关推荐
uva705 Slash Maze 的代码,在UVaOJ上通过
matlab开发-Maze。创建一个迷宫并测量玩家的完成时间。
matlab开发-Maze3D。在多层次的三维迷宫中导航
逃离迷宫 一个简单而微小的CUI迷宫游戏安装npm install -g escape-from-the-maze用法开始游戏: escape-from-the-maze 演出排名: escape-from-the-maze --rankingescape-from-the-maze -r 显示帮助: escape-from-...
Algorithm-maze-generator.zip,基于深度优先搜索算法的实时javascript迷宫生成器,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
Algorithm-maze.zip,在python中创建和解决迷宫。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
Vue-maze由Vue.js组件制作的迷宫游戏PLAY DEMO自动适应组件大小Mousemove / Touchmove / ArrowKey移动p Vue-maze由Vue.js组件制作的迷宫游戏PLAY DEMO Fit自动更改为组件大小Mousemove / Touchmove / ArrowKey移动...
Modbus.Poll.v5.0.1.Build.450.Cracked-MAZE ; Modbus主机仿真
Modbus.Slave.v4.4.5.Build.448.Cracked-MAZE;Modbus从机仿真
The A-Maze-ing escape is a webbrowser based 3D game.On slow PCs or mobiles you have to increase the resolution slider and decrease the viewDist.
Reinforcement learning, a Q learning algorithm, implementation on a robot that tryies to solve randomly created maze and reach the goal. Note that you can run .m files both on Matlab and Octave.
± RAR.Password.Unlocker.v4.2.0.0.Cracked-MAZE ± ± ± ÜÜ Ü ÛÜÜÜ Ü Ü ÜÜÜ Ü Ü Ü Üܱßß ß ß [ rELEASE NOTES ] ² ² Û sUPPLiER úúúúúúúúúúúú TEAM MAZE Ü ² ...
MATLAB基于guide的迷宫游戏-maze_game.zip maze_game.zip 包含深度优先,递归分割,prim算法三种类型 gaze.png gg.png
This is python script that generates maze with Oldos-Broder algorythm in 3d blender.
TamoSoft.SmartWhois.v5.1.268.Incl.Keymaker.and.Patch-MAZE
RAR文件密码恢复软件(RAR.Password.Unlocker.v4.2.0.0.Cracked-MAZE)
A python implementation of A* (A star) algorithm for find a shortest path in a maze.
迷宫问题的代码,能用哟,给那些需要的人使用,并欢迎大家讨论研究 -Maze problem code, yo can use to those who need to use, and welcomed everyone to discuss the research
This is a java base maze travailing algorithm, backtracking based. Recursively travels trough the maze and find the target in the maze.
题目:https://adworld.xctf.org.cn/task/task_list?type=reverse&number=4&grade=0&page=1 我的解题记录:https://www.yuque.com/jianouzuihuai/study/fr45py