zoukankan      html  css  js  c++  java
  • pku 2488 A Knight's Journey

    http://poj.org/problem?id=2488

    题目是说有一个骑士,在一个地图里以“日”字型走,看是否存在一条路径能使得他把每一个格子都走完,并且每个格子只走一次。

    如果存在这样的路径就按字典序输出这样的路径,如果不存在就输出impossible。

    其中地图中 列是用A—Z表示,行用数字表示

    提醒一下,深搜的时候一定要按字典序的形式进行搜索,否则输出的路线就不是字典序了。本来是一道很水的题,就是因为没有注意搜的方向,错了好几次

     1 #include<stdio.h>
    2 #include<string.h>
    3 #include<iostream>
    4 using namespace std;
    5 #define N 110
    6 int move[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; //这里要注意了
    7 int v[N][N];
    8 int n,m;
    9 struct node
    10 {
    11 int x;
    12 int y;
    13 int sum;
    14 };
    15 node path[N];
    16 int dfs(node a)
    17 {
    18 int i;
    19 node t;
    20 if (a.sum==n*m)
    21 {
    22 return 1;
    23 }
    24 for(i=0;i<8;i++)
    25 {
    26 t.x=a.x+move[i][0];
    27 t.y=a.y+move[i][1];
    28 t.sum=a.sum+1;
    29 if(t.x<=n&&t.x>0&&t.y<=m&&t.y>0&&!v[t.x][t.y]&&t.sum<=n*m)
    30 {
    31 v[t.x][t.y]=1;
    32 path[t.sum]=t;
    33 if(dfs(t)) return 1;
    34 v[t.x][t.y]=0;
    35 path[t.sum].x=-1;
    36 path[t.sum].y=-1;
    37 }
    38 }
    39 return 0;
    40 }
    41 int main()
    42 {
    43 node p;
    44 int i,cs=0;
    45 int t;
    46 cin>>t;
    47 while(t--)
    48 {
    49 cin>>n>>m;
    50 memset(v,0,sizeof(v));
    51 memset(path,-1,sizeof(path));
    52 p.x=1;p.y=1;p.sum=1;
    53 v[1][1]=1;
    54 path[1]=p;
    55 printf("Scenario #%d:\n",++cs);
    56 if(dfs(p))
    57 {
    58 for(i=1;i<=n*m;i++)
    59 {
    60 printf("%c%d",path[i].y+'A'-1,path[i].x);
    61 }
    62 }
    63 else cout<<"impossible";
    64 cout<<endl;
    65 if(t) cout<<endl;
    66 }
    67 return 0;
    68 }
  • 相关阅读:
    POJ 1401 Factorial
    POJ 2407 Relatives(欧拉函数)
    POJ 1730 Perfect Pth Powers(唯一分解定理)
    POJ 2262 Goldbach's Conjecture(Eratosthenes筛法)
    POJ 2551 Ones
    POJ 1163 The Triangle
    POJ 3356 AGTC
    POJ 2192 Zipper
    POJ 1080 Human Gene Functions
    POJ 1159 Palindrome(最长公共子序列)
  • 原文地址:https://www.cnblogs.com/fxh19911107/p/2382567.html
Copyright © 2011-2022 走看看