poj A Knight's Journey(DFS)

题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=190592

题意:给出p*q的棋盘,从(A,1)开始,走“日”字,问能否走完棋盘上所有的点,如果能,按字典序输出路径;

思路:DFS,并保存路径即可,注意处理走的方向顺序int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};

#include <stdio.h>
#include <string.h>
int vis[30][30], ansx[30],ansy[30];
int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
int p,q,flag;
void dfs(int x, int y, int num)
{
    int i, j;
    ansx[num]=x;
    ansy[num]=y;
    if(num==p*q)
    {
        flag=1;
        return ;
    }
    for(i=0; i<8; i++)
    {
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];

        if(xx<=q && xx>=1 && yy<=p && yy>=1 && !vis[xx][yy] && !flag)
        {
            //printf("%d %d\n",xx,yy);
            //getchar();
            vis[xx][yy]=1;
            dfs(xx,yy,num+1);
            vis[xx][yy]=0;
        }
    }
}
int main()
{
    int t,_case=1;
    scanf("%d",&t);
    while(t--)
    {
        if(_case!=1) printf("\n");
        printf("Scenario #%d:\n",_case++);
        scanf("%d%d",&p,&q);
        memset(vis,0,sizeof(vis[0]));
        vis[1][1]=1;
        flag=0;
        dfs(1,1,1);
        if(flag)
        for(int i=1; i<=p*q; i++)
        {
            printf("%c%d",ansx[i]+‘A‘-1,ansy[i]);
        }
        else printf("impossible");
        printf("\n");

    }
    return 0;
}

  

poj A Knight's Journey(DFS)

时间: 2024-12-26 20:03:43

poj A Knight's Journey(DFS)的相关文章

POJ 2488-A Knight&#39;s Journey(DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10813 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 (DFS)

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

A Knight&#39;s Journey (DFS)

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

POJ 2488-A Knight&amp;#39;s Journey(DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10813 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(dfs+字典序路径输出)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2488 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢

POJ2488-A Knight&#39;s Journey(DFS+回溯)

题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36695   Accepted: 12462 Description Background The knight is getting bored of seeing the same black and white squares again and again

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

poj 3009 Curling 2.0 (dfs )

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

POJ 2718:Smallest Difference(dfs)

原题地址:点击打开链接 (原题是英文,下面的是用有道翻译的) 描述 鉴于许多不同的小数位数,你可以通过选择一个非空的子集形成一个整数的位数和写一些秩序.剩下的数字可以写在一些秩序形成第二个整数.除非得到的整数是0,整数可能不会从数字0开始. 例如,如果您有数字0,1,2,4,6和7,您可以编写两个整数10和2467.当然,有很多方法可以形成这样的双整数:210年和764年,204年和176年,等.整数之间的差异的绝对值在过去的28岁,事实证明,没有其他一对由上面的规则可以实现一个更小的差异. (