• poj2488骑士之旅


    题目大意:国际象棋里面的马,有那么8种跳法,然后题目给出一个棋盘的大小p*q, 求有没有路线可以使得这个马能把整个棋盘的格全部走一遍,有的话按照字典序将第一条路线打印出来。

    注意:国际象棋是行是数字,列是字母,按照字典序A1B3....,是需要按照先列后行来处理的。

    因为要找一条路径出来,所以考虑深度优先(DFS)

    贴一下烂代码(o(╯□╰)o):

    #include<iostream>
    #include<string>
    using namespace std;
    
    int chess[27][27];
    int path;
    bool exist=0;
    string rec;
    
    void DFS(int y,int x,int szy,int szx)
    {
        chess[x][y]=1;
        path++;
        rec.push_back(char(y+'A'));
        rec.push_back(char(x+'1'));
        //cout<<"Beginning:"<<rec<<endl;
        int increx,increy;//,flag=0;
        for(int n=0;n<8;n++)
        {
            switch(n){
                case 0:
                    increy=-2;increx=-1;break;
                case 1:
                    increy=-2;increx=1;break;
                case 2:
                    increy=-1;increx=-2;break;
                case 3:
                    increy=-1;increx=+2;break;
                case 4:
                    increy=+1;increx=-2;break;
                case 5:
                    increy=+1;increx=+2;break;
                case 6:
                    increy=+2;increx=-1;break;
                case 7:
                    increy=+2;increx=+1;break;
            }
            if(y+increy>=szy||y+increy<0||x+increx>=szx||x+increx<0) continue;
            else if(chess[x+increx][y+increy]==0)
            {
                DFS(y+increy,x+increx,szy,szx);
            }
        }
    
            if(path==szy*szx) 
            {
                //cout<<"result:"<<rec<<endl;
                exist=1;
            }
            else{
            path--;
            rec.erase(rec.end()-1);
            rec.erase(rec.end()-1);
            //cout<<"Delete:"<<rec<<endl;
            chess[x][y]=0;
            }
    
    }
    
    
    
            
    
    int main()
    {
        int instan,p,q,i,j,k;
        cin>>instan;
        for(i=0;i<instan;i++)
        {
            exist=0;
    
            memset(chess,0,sizeof(chess));
            cin>>p>>q;
            cout<<"Scenario #"<<i+1<<":"<<endl;
            for(j=0;j<q;j++)
            {
                for(k=0;k<p;k++)
                {
                    path=0;
                    rec.clear();
                    //cout<<"Start from "<<k<<" "<<j<<":"<<endl;
                    DFS(j,k,q,p);
                    if(exist==1) 
                    {
                        cout<<rec<<endl;
                        break;
                    }
                    
                }
                    if(exist==1) 
                    {
                        break;
                    }
            }
            if(exist==0) cout<<"impossible"<<endl;
            cout<<endl;
    
        }
        return 0;
    }
  • 相关阅读:
    Object.prototype.toString.call()进行类型判断
    JavaScript中的typeof操作符用法实例
    js ==与===区别(两个等号与三个等号)
    js nextSibling属性和previousSibling属性概述及使用注意
    Java 缓存技术之 ehcache
    不可不知 DDoS的攻击原理与防御方法
    jQuery的选择器中的通配符[id^='code']
    jquery $("[id$='d']").val();这句话什么意思?
    js 数组的操作
    【转】理解js中的原型链,prototype与__proto__的关系
  • 原文地址:https://www.cnblogs.com/soyscut/p/3185025.html
Copyright © 2020-2023  润新知