poj2488

#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> using namespace std; int T,p,q,vis[30][30],flag; int f[2][8]={{-2,-2,-1,-1, 1,1, 2,2},

{-1, 1,-2, 2,-2,2,-1,1}}; struct node{     int x;int y; }ans[30]; void dfs(int x,int y,int step,int tt) {     if (flag) return ;

vis[x][y]=1;     ans[step].x=x;     ans[step].y=y;

if (step==p*q && flag==0)     {         flag=1;         printf("Scenario #%d:\n",tt);         for (int i=1;i<=step;i++)         {             char c=ans[i].x-1+‘A‘;             cout<<c<<ans[i].y;         }         printf("\n");         printf("\n");         return ;     }     for (int i=0;i<8;i++)     {         int nex=x+f[0][i];         int ney=y+f[1][i];         if (nex>=1 && nex<=q && ney>=1 && ney<=p && vis[nex][ney]!=1)         dfs(nex,ney,step+1,tt);

}     vis[x][y]=0; } int main() {     scanf("%d",&T);     for (int t=1;t<=T;t++)     {                flag=0;         scanf("%d%d",&p,&q);         dfs(1,1,1,t);         if (!flag)         { memset(vis,0,sizeof(vis));             printf("Scenario #%d:\n",t);             printf("impossible\n");             printf("\n");         }     } }

时间: 2024-10-10 11:38:27

poj2488的相关文章

POJ2488 A Knight&#39;s Journey

Description BackgroundThe 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

POJ2488:A Knight&#39;s Journey(dfs)

http://poj.org/problem?id=2488 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 directi

搜索专题小结及例题:POJ2251&amp;POJ1426&amp;POJ3087&amp;POJ2488

图的遍历也称为搜索,就是从图中某个顶点出发,沿着一些边遍历图中所有的顶点,且每个顶点仅被访问一次,遍历可采取两种不同的方式:深度优先搜索(DFS)和广度优先搜索(BFS). 1.DFS算法思想` 从顶点v出发深度遍历图G的算法 ① 访问v0顶点,置vis[v0]=1,搜索v0未被访问的邻接点w,若存在邻接点w,则dfs(w),直到到达所有邻接点都被访问过的顶点u为止,接着退回一步,看是否还有其他没有被访问的邻接点.如果有,则访问此顶点,进行前述类似的访问,如果没有,就在退回一步进行搜索,重复上述

POJ2488 A Knight&#39;s Journey 骑士巡游 DFS

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

poj2488(A Knight&#39;s Journey)

题目地址:A Knight's Journey 题目大意: 骑士按照日字形走,给你p*q的棋盘,问你骑士能否走遍棋盘的所有位置,输出骑士走的路线序列p(1.2....)q(A.B...) 按照字典序输出,如果不能输出 'impossible' . 解题思路: 搜索.因为是遍历全图所有点,所以必然经过A1. 又因为按字典序,既然A1可以经过,所以必然可以从A1开始. 注意: 1.按照字典序输出意思为(先安A.B.C...)排序输出. const int dirx[]={-1,1,-2,2,-2,2

快速切题 poj2488 A Knight&#39;s Journey

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31195   Accepted: 10668 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 ar

poj2488 A Knight&#39;s Journey裸dfs

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35868   Accepted: 12227 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 ar

poj-2488 a knight&#39;s journey(搜索题)

Time limit1000 ms Memory limit65536 kB 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 a

POJ2488 深度优先搜索+回溯

POJ2488 题目 骑士按照下图所示的走法对棋盘进行巡逻,每个格子只允许巡逻一次,且必须巡逻所有格子.给定棋盘的行数p和列数q,输出一条骑士巡逻路径,若不存在这样一条路径,则输出impossible. 图1 骑士的8种走法 骑士巡逻问题的简化版本,是哈密顿路径问题的特殊形式,但是是线性时间内可以解决的\(^{[1]}\). Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenario #2: impossible Scena

POJ2488 dfs

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41972   Accepted: 14286 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 ar