迷宫问题(bfs)

POJ - 3984

 
 #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

OpenJ_Bailian - 2488

 
 #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)的更多相关文章

  1. POJ2488A Knight's Journey[DFS]

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41936   Accepted: 14 ...

  2. A Knight's Journey(dfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25950   Accepted: 8853 Description Back ...

  3. POJ2488:A Knight's Journey(dfs)

    http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...

  4. [poj]2488 A Knight's Journey dfs+路径打印

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45941   Accepted: 15637 Description Bac ...

  5. POJ2248 A Knight's Journey(DFS)

    题目链接. 题目大意: 给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点. 列为数字,行为字母,搜索按字典序. 分析: 用 vis[x][y] 标记是否已经访问.因为要搜索所 ...

  6. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  7. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...

  8. 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 ...

  9. poj2488 A Knight's Journey裸dfs

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35868   Accepted: 12 ...

随机推荐

  1. MVC _Ajax的使用【七】

    一.本篇主要写的是在MVC项目中一种ajax的使用方法 1.  首先在控制器中创建两个方法,showCreate()和AddUserInfo() using System; using System. ...

  2. HRBUST 1186 青蛙过河 (思路错了)

    在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数轴上的一串 ...

  3. Angular4.x Event (DOM事件和自定义事件)

    Angular组件和DOM元素通过事件与外部进行通信,两者中的事件绑定语法是相同的-(eventName)="expression": <button (click)=&qu ...

  4. Bupt归队赛, gunfight

    只需要关心是否开枪,上个人和当前这个人的位置关系,转移可以前缀和优化 为了不重复,始终认为第一个就是1,最后答案乘以n #include<bits/stdc++.h> using name ...

  5. 如何解决ORA-28002 the password will expire within 7 days问题(密码快过期)

    1.问题描述: 今天登陆pl/sql工具时,提示 ORA-28002 the password will expire within 7 days 2.问题原因: oracle11g中默认在defau ...

  6. Mac安装brew(遇到的坑)

    1.安装方法: 网上都会有 命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/i ...

  7. Jumpserver跳板机的搭建和部署

    1.需要搭云yum仓库wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 2. ...

  8. window service 2008 解决80端口占用

    1.进入cmd使用netstat 命令查看指定端口netstat -ano | findstr :802.如下所示:本地的80端口被进程为4的占用 TCP    0.0.0.0:80          ...

  9. keras在win下的安装,使用等

    http://cache.baiducontent.com/c?m=9d78d513d99a16ef4fece42d4c01d6160e2482744cd7c7637ac3e34a84652b5637 ...

  10. CSS学习笔记-05 过渡模块的基本用法

    话说 1对情侣两情相悦,你情我愿.时机成熟,夜深人静...咳 ,如果就这么直奔主题,是不是有点猴急,所以,还是要来点前戏@. 铛 铛, 这个时候 过渡模块出现了. 划重点: 上代码: <!DOC ...