• POJ 2488 A Knight's Journey


    很明显的深搜问题。

    继续加深对递归过程的理解。先贴个WA的代码,原因是每次递归的时候只能确定当前值,而返回是从最后调用的依次向最先调用的返回,刚开始输出不知如何处理,因为只能逆序比较简单。

    当时头脑发热,想从最后(p,q)点迭代,错误的以为这是从(1,1)迭代的逆过程。

    虽然样例能过,但是显然没有理解深搜的概念,因为逆过去了就相当于从(p,q)点开始深度优先。

    View Code
     1 /*解题思路:最难理解的是按字典序输出。一直在考虑应该怎样才算是按字典序输出,应该是所有可能的输出序列中按字典序的第一个。
    2 这样可以知道每次DFS的时候选取8个方向的时候有个选取顺序问题。*/
    3
    4 #include<iostream>
    5 #include<cstring>
    6 using namespace std;
    7
    8 int direction[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
    9
    10 int p,q;
    11 int map[100][100];
    12
    13 bool dfs(int i,int j,int step)
    14 {
    15 if(i<1||i>q||j<1||j>p||map[i][j]!=-1)
    16 return false;
    17 if(step==p*q)
    18 {
    19 char c=i-1+'A';
    20 cout<<c<<j;
    21 return true;
    22 }
    23 if(map[i][j]==-1)
    24 {
    25 map[i][j]=1;
    26 for(int k=1;k<=8;k++)
    27 {
    28 if(dfs(i+direction[8-k][0],j+direction[8-k][1],step+1))
    29 {
    30 char c=i-1+'A';
    31 cout<<c<<j;
    32 return true;
    33 }
    34 }
    35 map[i][j]=-1;
    36 return false;
    37 }
    38 return false;
    39 }
    40
    41
    42 void init()
    43 {
    44 int cases;
    45 int num=0;
    46 cin>>cases;
    47 while(cases--)
    48 {
    49 memset(map,-1,sizeof(map));
    50 cin>>p>>q;
    51 cout<<"Scenario #"<<++num<<":"<<endl;
    52 if(!dfs(q,p,1))
    53 {
    54 cout<<"impossible";
    55 }
    56 cout<<endl;
    57 cout<<endl;
    58 }
    59 }
    60
    61
    62 int main()
    63 {
    64 init();
    65 }

    刚刚上厕所的时候想到用字符串传个参数可以实现,改了后应该能AC。。。。

    果断A了,但是字符串不会使用啊,所以只好用一个字符数组(用到索引)存储了。

    View Code
     1 #include<iostream>
    2 #include<cstring>
    3 using namespace std;
    4
    5 int direction[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
    6
    7 int p,q;
    8 int map[100][100];
    9 char str[100][2];
    10
    11 bool dfs(int i,int j,int step)
    12 {
    13 if(i<1||i>q||j<1||j>p||map[i][j]!=-1)
    14 return false;
    15 if(step==p*q)
    16 {
    17 char c1=i-1+'A';
    18 char c2=j+'0';
    19 str[step][0]=c1;
    20 str[step][1]=c2;
    21 return true;
    22 }
    23
    24 if(map[i][j]==-1)
    25 {
    26 map[i][j]=1;
    27 for(int k=1;k<=8;k++)
    28 {
    29 if(dfs(i+direction[k-1][0],j+direction[k-1][1],step+1))
    30 {
    31 char c1=i-1+'A';
    32 char c2=j+'0';
    33 str[step][0]=c1;
    34 str[step][1]=c2;
    35 return true;
    36 }
    37 }
    38 map[i][j]=-1;
    39 return false;
    40 }
    41 return false;
    42 }
    43
    44
    45 void init()
    46 {
    47 int cases;
    48 int num=0;
    49 cin>>cases;
    50 while(cases--)
    51 {
    52 memset(str,'0',sizeof(str));
    53 memset(map,-1,sizeof(map));
    54 cin>>p>>q;
    55 cout<<"Scenario #"<<++num<<":"<<endl;
    56 if(!dfs(1,1,1))
    57 {
    58 cout<<"impossible";
    59 }
    60 else
    61 {
    62 for(int i=1;i<=p*q;i++)
    63 cout<<str[i][0]<<str[i][1];
    64 }
    65 cout<<endl;
    66 cout<<endl;
    67 }
    68 }
    69
    70
    71 int main()
    72 {
    73 init();
    74 }

  • 相关阅读:
    Java实战之03Spring-01Spring概述
    Java实战之02Hibernate-08二级缓存
    Java实战之02Hibernate-07与效率性能相关配置
    css3 移入移出动画
    ng2 搭建本地开发环境
    ng2模板语法/内置指令速查表
    node + npm 命令
    什么是作用域?什么是上下文?浅解
    get? post? put? delete? head? trace? options? http请求方法
    什么是http?
  • 原文地址:https://www.cnblogs.com/YipWingTim/p/2220923.html
Copyright © 2020-2023  润新知