迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs)
#include <iostream>
#include <queue>
#include <stack>
#include <cstring> using namespace std; /*广度优先搜索*/
/*将每个未访问过的邻接点进队列,然后出队列,知道到达终点*/ typedef class
{
public:
int x;
int y;
}coordinate; int maze[][]; //迷宫
int road[][] = { { , - }, { , }, { -, }, { , } };
coordinate pre[][]; //记录当前坐标的前一个坐标 int visited[][] = { }; /*利用一个栈,倒序输出pre中存入的坐标*/
void print()
{
stack<coordinate> S;
coordinate rhs = { , }; while ()
{
S.push(rhs);
if (rhs.x == && rhs.y == )
break;
rhs = pre[rhs.x][rhs.y];
} while (!S.empty())
{
rhs = S.top();
S.pop();
cout << "(" << rhs.x << ", " << rhs.y << ")" << endl;
}
} void bfs(int x, int y)
{
queue<coordinate> Q; //用来帮助广度优先搜索
coordinate position; position.x = x, position.y = y; Q.push(position); while (!Q.empty())
{
position = Q.front();
Q.pop(); visited[position.x][position.y] = ;
coordinate position2; if (position.x == && position.y == ) //如果找到终点,停止搜索
{
print();
return;
} for (int i = ; i < ; i++)
{ position2.x = position.x + road[i][];
position2.y = position.y + road[i][]; if (position2.x >= && position2.x <= && position2.y >= && position2.y <= && maze[position2.x][position2.y] == && !visited[position2.x][position2.y]) //如果这个邻接点不是墙且未访问过,则进队列
{
Q.push(position2);
pre[position2.x][position2.y] = position; //记录当前坐标的前一个坐标位置
visited[position2.x][position2.y] = ;
} }
}
} int main()
{
memset(maze, , sizeof(pre));
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
{
cin >> maze[i][j];
} //memset(pre, 0, sizeof(pre)); //现将pre初始化 bfs(, ); return ;
}
A Knight's Journey
#include <cstring>
#include<iostream>
using namespace std; char route[][]; //记录行驶的路线
int board[][]; //棋盘 int total = ;
int length;
/*dfs深度优先搜索*/ int go[][] = { { -, - }, { , - }, { -, - }, { , - }, { -, }, { , }, { -, }, { , } };//字典序方向 /*一次跳跃*/
void knight(int p, int q,int len ,int row, int col) //len为行走的距离,用来判断是否遍历整个棋盘, row和col为当前坐标
{
board[row][col] = ;
route[len][] = col + 'A' - , route[len][] = row + '';
length = len;
/*让马分别尝试8个方向的跳跃,知道跳不动为止*/ int x, y; for (int i = ; i < ; i++)
{ x = row + go[i][];
y = col + go[i][]; if (x > && x <= p && y > && y <= q && board[x][y] == )
knight(p, q, len + , x, y);
} /*if (row - 1 >= 1 && col - 2 > 0 && board[row - 1][col - 2] == 0)
knight(p, q, len + 1, row - 1, col - 2); if (row + 1 <= p && col - 2 > 0 && board[row + 1][col - 2] == 0)
knight(p, q, len + 1, row + 1, col - 2); if (row - 2 >= 1 && col - 1 > 0 && board[row - 2][col - 1] == 0)
knight(p, q, len + 1, row - 2, col - 1); if (row + 2 <= p && col - 1 > 0 && board[row + 2][col - 1] == 0)
knight(p, q, len + 1, row + 2, col - 1); if (row - 2 >= 1 && col + 1 <= q && board[row - 2][col + 1] == 0)
knight(p, q, len + 1, row - 2, col + 1); if (row + 2 <= p && col + 1 <= q && board[row + 2][col + 1] == 0)
knight(p, q, len + 1, row + 2, col + 1); if (row - 1 >= 1 && col + 2 <= q && board[row - 1][col + 2] == 0)
knight(p, q, len + 1, row - 1, col + 2); if (row + 1 <= p && col + 2 <= q && board[row + 1][col + 2] == 0)
knight(p, q, len + 1, row + 1, col + 2);*/ } int main()
{
int N, p, q; //行数, 每次的宽和高
cin >> N; while (N--)
{
cin >> p >> q;
knight(p, q, , , ); //以行走了0个单元
cout << "Scenario #" << ++total << ":" << endl;
if (length == p * q - )
{ for (int i = ; i <= length; i++)
cout << route[i][] << route[i][];
cout << endl << endl;
}
else
cout << "impossible" << endl << endl;
memset(board, , sizeof(board)); //清空棋盘的足迹
memset(route, , sizeof(route)); //清空路线
} return ;
}
迷宫问题bfs, A Knight's Journey(dfs)的更多相关文章
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- A Knight's Journey(dfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25950 Accepted: 8853 Description Back ...
- POJ2488:A Knight's Journey(dfs)
http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- POJ2248 A Knight's Journey(DFS)
题目链接. 题目大意: 给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点. 列为数字,行为字母,搜索按字典序. 分析: 用 vis[x][y] 标记是否已经访问.因为要搜索所 ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ 2488 A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏
A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...
- poj2488 A Knight's Journey裸dfs
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35868 Accepted: 12 ...
随机推荐
- Redhat配置yum源(使用阿里云yum Repo)
1. 查看版本号和系统类别: cat /etc/redhat-release archor cat /etc/issue && arch 2.检查yum是否安装,以及安装了哪些依赖源并 ...
- Axure 交互样式和选项组的设置
1.点击元件,点击属性,点击选中,点击图片,选择相应的图片,选中功能是当点击这个元件的时候,元件的变成相应的图片,然后再设置鼠标单击时的动作是选中,就可以实现选中某个元件的时候,元件会变成其他的图片. ...
- 异步核心接口IAsyncResult的实现
要实现异步编程,就需要正确的实现IAsyncResult接口.IAsyncResult共有四个属性: public interface IAsyncResult { object AsyncState ...
- three.js 第二篇:场景 相机 渲染器 物体之间的关系
w我用画画来形容他们之间的关系 场景就是纸张 相机就是我们的眼睛 物体就是在我们脑海中构思的那个画面 渲染器就是绘画这个动作 场景(Scene): 初始化:var scene = new THREE. ...
- Django框架简介-模板系统
2.4 模板 官方文档 2.4.1 常用语法 只需要记两种特殊符号: {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 2.4.1.1 变量 {{ 变量名 }} 变量名由字母数 ...
- Matlab:显(隐)式Euler和Richardson外推法变步长求解刚性问题
一.显示Euler 函数文件:Euler.m function f=Euler(h,Y) f(1,1)=Y(1)+h*(0.01-(1+(Y(1)+1000)*(Y(1)+1))*(0.01+Y(1) ...
- 在500jsp错误页面获取错误信息
自定义异常发生时的错误处理页面: 1) 只要定义page指示元素的errorPage属性就可以指定当前页面发生异常时应该交给哪个页面进行处理,例如:<%@page errorPage=" ...
- VersionControl:git
关于VersionControl VersionControl即版本控制,版本控制是一种记录文件或文件集随时间变化的系统,以便以后可以查阅调用特定版本,版本控制系统不仅可以应用于软件源代码的文本文件, ...
- JS调用webservice服务
webservice服务 webservice服务代码 using System; using System.Collections.Generic; using System.Linq; using ...
- 搜狗浏览器或者360浏览器安装chrome 浏览器插件
https://www.cnblogs.com/ingvar/p/6403777.html#undefined