POJ 2488(dfs+字典序)

poj2488

题意:

问能不能不重复地走能遍历所有的棋格,走法按中国象棋马的方法。

分析: dfs+字典序输出...

我的字典序处理是用的String 来存,每次dfs后将该次有效遍历的地址加到串里面。从(0,0)开始。其他的就是基本的dfs知识~

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#define Max 30
using namespace std;
int n,m;
int temp;
int vis[Max][Max];
int dir[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
int dfs(int x,int y,int step,string tt)
{
    if(step==temp)
    {
         cout<<tt<<endl<<endl;
         return 1;
    }
    else
    {
        for(int i=0;i<8;i++)
    {
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        char ans1=yy+'A';
        char ans2=xx+'1';
        if(xx>=0 && xx<n && yy>=0 && yy<m && vis[xx][yy]==0)
        {
            vis[xx][yy]=1;
            if(dfs(xx,yy,step+1,tt+ans1+ans2))
                return 1;
            vis[xx][yy]=0;
        }
    }

return 0;
}
}
int main()
{
    int t;
    cin>>t;
    int ans=1;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        temp=n*m;
        memset(vis,0,sizeof(vis));
        vis[0][0]=1;
        printf("Scenario #%d:\n",ans++);
        if(!dfs(0,0,1,"A1"))
        printf("impossible\n\n");
    }
    return 0;
}
时间: 2024-10-20 11:10:44

POJ 2488(dfs+字典序)的相关文章

poj 2488 dfs

背景:就是简单的遍历全图搜索,但要注意两点: 1.一开始以为起点不同会有不同结果,所以就枚举了起点,但实际上只要能遍历全图就能把A1作为起点,因为遍历全图就是每个点都要走到,那么A1也要走到,既然可以走到A点,那么也可以从A点走到其它点. 2.题目中的字典序输出,不看看很能想到题意是先满足列,然后满足行,这样写出满足条件的方向数组即可. //poj 2488 #include<map> #include<set> #include<stack> #include<

poj 2488 A Knight&#39;s Journey(dfs+字典序路径输出)

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

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

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

搜索 || DFS || POJ 2488 A Knight&#39;s Journey

给一个矩形棋盘,每次走日字,问能否不重复的走完棋盘的每个点,并将路径按字典序输出 *解法:按字典序输出路径,因此方向向量的数组按字典序写顺序,dfs+回溯,注意flag退出递归的判断,并且用pre记录路径 #include <iostream> #include <cstdio> #include <cstring> using namespace std; char a[30][30]; int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2};

dfs/poj 2488 A Knight&#39;s Journey

1 #include<cstdio> 2 using namespace std; 3 const int b[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; 4 int a[30][30],p,q; 5 struct 6 { 7 int x,y; 8 }step[910]; 9 10 bool dfs(int x,int y,int now) 11 { 12 if (now==p*q) return true;

POJ 2488 A Knight&#39;s Journey

A Knight's Journey Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 248864-bit integer IO format: %lld      Java class name: Main Background  The knight is getting bored of seeing the same black and white squa

A Knight&#39;s Journey POJ - 2488

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

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