poj 2488 骑士游历
来源:互联网 发布:战斗机飞行员 知乎 编辑:程序博客网 时间:2024/04/29 15:03
深度优先搜索与回溯结合进行
单纯的搜索与回溯的写法是不一样的,
回溯尤其是要记录搜索路径的时候,需要更多的处理
这题目感觉用数组,然后记录搜素深度来记录路径更好,还是用了双端队列,更麻烦了,调了一会
关键:如果DFS能达到,则从任何一点出发都能达到,所以以A1为起点,注意按照字典顺序
#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <vector>#include <set>#include <cctype>#include <algorithm>#include <cmath>#include <deque>#include <queue>#include <map>#include <queue>#include <list>#include <iomanip>using namespace std; //////#define INF 0xffffff7#define maxn 30#define max(a,b)(a>b?a:b)///int color[maxn][maxn];//是否访问过int p, q;struct pos{int q;int p;};bool finish;deque<pos> stk;//记录搜索路径int dy[] = {-2, -2, -1, -1, 1, 1, 2, 2};int dx[] = {-1, 1, -2, 2, -2, 2, -1, 1};void dfs(int cur){int i, j;if ((cur == p * q) && !finish){finish = true;while (!stk.empty()){pos tmpPos;tmpPos = stk.front();stk.pop_front();char temp = tmpPos.q + 'A';printf("%c%d", temp, tmpPos.p + 1);}printf("\n");return;}pos curpos = stk.back();for (i = 0; i < 8; i++){pos newpos;newpos.q = curpos.q + dy[i];newpos.p = curpos.p + dx[i];if (newpos.q >= 0 && newpos.q < q && newpos.p >= 0 && newpos.p < p && color[newpos.q][newpos.p] == 0 && !finish){color[newpos.q][newpos.p] = 1;stk.push_back(newpos);dfs(cur + 1);if (!finish)stk.pop_back();color[newpos.q][newpos.p] = 0;}}}int main(){int i, j, k;int ncases;int cases = 1;scanf("%d", &ncases);while (ncases--){scanf("%d %d", &p, &q);//q代表字母个数,p代表数字个数memset(color, 0, sizeof(color));finish = false;while (!stk.empty())stk.pop_back();printf("Scenario #%d:\n", cases);cases++;pos startpos;startpos.q = startpos.p = 0;color[startpos.q][startpos.p] = 1;stk.push_front(startpos);dfs(1);if (!finish)printf("impossible\n");printf("\n");}/// return 0;}
- poj 2488 骑士游历
- poj 1915 骑士游历问题
- poj 2243 骑士游历2
- 骑士游历
- 骑士游历
- 骑士游历
- 骑士游历
- 骑士游历问题
- ZJNU 1006 骑士游历
- 骑士游历问题
- UVA 439 骑士游历
- wikioi p1219 骑士游历
- wikioi 1219 骑士游历
- POJ2488 骑士游历(DFS)
- Wikioi 骑士游历
- wikioi骑士游历c
- 维基 1219 骑士游历
- codevs1219 骑士游历
- STM32 Unique device ID reading
- JavaWeb体系结构的理解-9.程序调试与发布
- MSSQL中的自连接
- 详解Java解析XML的四种方法
- Linux内核抢占实现机制分析
- poj 2488 骑士游历
- 软件架构师应该知道的97件事(分享领导力、技能、思维模式、沟通、博弈,53位优秀架构师各展绝技)
- request.getAttribute 和 request.getParameter的异同
- ArcGIS Runtime for Android开发教程V2.0(1)基本概念
- linux下管道
- 译:理解 Win32 "OutputDebugString"
- GetAdaptersInfo 获取网卡配置和Ip地址信息
- Android系列讲座(一):环境搭建
- requires unavailable shared library com.google.android.maps; failing!解决方案