我用的BFS,建图的时候建对,就没什么问题了。 附上代码: 1#include<stdio.h>2#include<queue>3#include<string.h>4usingnamespacestd;5intvisit[40][40][40];6intstep[40][40][40];7intdx[6]= {0,0,0,0,1,-1};8intdy[6]= {0,0,1,-1,0,0};9intdz[6]= {1,-1,0,0,0,0};10...
看着有点类似于象棋中的马。 二、题目思路以及AC代码 这道题也比较简单,一开始我以为是BFS,但后来稍微琢磨了一下,发现直接DFS就可以找到,其实和之前做的POJ 1753, POJ 2965没有什么太大的区别。无非就是DFS遍历加存储成功路径。这里只不过需要在骑士行走的时候,保证一下其字典序最小的问题。 大致思路: 因为起始...
int d[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; bool vis[30][30]; int xx[30]; int yy[30]; int flag; int a,b; void DFS(int x,int y,int num) { if(flag) return;
POJ 2488 A Knight's Journey DFS(深度优先搜索) AKnight'sJourneyTimeLimit:1000MSMemoryLimit:65536KTotalSubmissions: 53688Accepted: 18228DescriptionBackgroundTheknightisgettingboredofseeingthesameblackandwhitesquaresagain HDU1372 bfs /Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s): 11766Accep...
1///2014.3.102///poj248834//16MS56/**7*简单的DFS,但是题目有个坑人的地方,就是字典序8*因为要求字典序,在搜索时要注意搜索步骤9*在搜索步骤上浪费了两个小时啊!!!10*/1112#include <iostream>13#include <cstdio>14usingnamespacestd;1516intp,q;17intcoordinate_x[30];///记录每一步的坐标18int...
给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径。 经典的“骑士游历”问题 输入:第一行,整数n,接下来是n行,每一行为p和q,p为行数,q为列数,p用1...p编号,q用A...Q编号 马的走法:每步棋先横走或直走一格,然后再往外斜走一格;或者先斜走一格,最后再往...
用step来判断是否结束dfs,同时开一个二维数组来储存每一步的位置,用flag来判断是否找到。 代码: #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<algorithm> #include<stack> #include<queue> usingnamespace std;
markk=true;return; }inttx,ty;for(inti=0;i<8;i++){ ty=ll+movee[i][1]; tx=hh+movee[i][0];if(v[tx][ty]||tx<=0||tx>h||ty<=0||ty>l||markk)continue; v[tx][ty]=1; dfs(tx,ty,num+1); v[tx][ty]=0; }return; ...
{intnx=x+dx[i];intny=y+dy[i];if(nx>0&& nx<=a && ny>0&& ny<=b && visit[nx][ny]==false) { path[num+1][0]=nx+'0'; path[num+1][1]=ny+'A'-1; visit[nx][ny]=true;if(dfs(nx,ny,num+1))returntrue; visit[nx][ny]=false; ...
Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the...