• Sicily 1151: 简单的马周游问题(DFS)


      这道题嘛,直接使用DFS搜索,然后莫名其妙地AC了。后来看了题解,说是move的顺序不同的话可能会导致超时,这时便需要剪枝,真是有趣。原来自己是误打误撞AC了,hhh。题解还有另一种解法是先把一条完整的路储存在数组里,输入i的时候,就从i位置起把数组循环输出一遍,真是666的解法呀,果然不能被传统的思路所局限了呀!

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int _move[8][2] ={{1, -2}, {2, -1}, {2, 1}, {1, 2},{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}};
     4 
     5 
     6 struct Node{
     7     int x, y;
     8     vector<int>path;
     9 };
    10 Node node;
    11 
    12 bool dfs(int x, int y){
    13     
    14     node.x = x;
    15     node.y = y;
    16     for(int i = 0; i < 8; i++){
    17             int x = node.x + _move[i][0];
    18             int y = node.y + _move[i][1];
    19             if(0 <= x && x < 5 && 0 <= y && y < 6){
    20                 int flag = 1;
    21                 for(int j = 0; j < node.path.size(); j++){
    22                     if(node.path[j] == x*6 + y){
    23                         flag = 0;
    24                         break;
    25                     }
    26                 }    
    27                 if(flag){
    28                     node.path.push_back(x*6 + y);//走下一步 
    29                     
    30                     if(node.path.size() >= 30){//当path的size等于30,则说明已经走遍 
    31                         for(int i = 0; i < node.path.size(); i++){
    32                             cout << node.path[i] + 1 << ' ';
    33                         }
    34                         cout << endl;
    35                         return true;
    36                     }
    37                     if(dfs(x, y))return true;
    38                     node.path.pop_back();//还原到原来 
    39                     node.x -= _move[i][0];
    40                     node.y -= _move[i][1];    
    41                 }
    42             }
    43             
    44     }
    45     return false;
    46 }
    47 
    48 int main(){
    49     int n;
    50     while(cin >> n && n != -1){
    51         node.path.clear();
    52         n = n-1;
    53         int a = n / 6;
    54         int b = n % 6;
    55         node.path.push_back(a*6 + b);
    56         dfs(a, b);
    57     }
    58 } 
    59 
    60 /*
    61 #include<iostream>
    62 using namespace std;
    63 int a[]={1,14,25,21,29,18,5,16,12,4,8,19,27,23,10,6,17,30,22,26,13,2,15,7,3,11,24,28,20,9};
    64 int main()
    65 {
    66     int n;
    67     while(cin>>n)
    68     {
    69         int tmp;
    70         for(int i=0;i<30;i++) 
    71             if(a[i]==n)tmp=i;
    72         for(int i=0;i<30;i++)
    73         {
    74             cout<<a[(tmp+i)%30];
    75             if(i==29)cout<<endl;
    76             else cout<<" ";
    77         }
    78     }
    79 }
    80 */ 
  • 相关阅读:
    PHP post方式请求webservice接口以及解析返回的数据
    XYTipsWindow弹出层
    php使用rtrim时,可能会出现乱码
    带搜索的下拉框
    js实现自动给省市县地区自动赋值
    php 获取指定日期段内每一天的日期
    sqlserver数据库的备份和还原
    js判断哪个单选框应该被选中
    extjs3.1 解决列锁定,合计行不能滑动的问题
    考研数据结构-二叉树
  • 原文地址:https://www.cnblogs.com/Vincent-Bryan/p/6258072.html
Copyright © 2020-2023  润新知