zoukankan      html  css  js  c++  java
  • Solitaire

    Solitaire

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 190 Accepted Submission(s): 68
     
    Problem Description
    Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.

    There are four identical pieces on the board. In one move it is allowed to:

    > move a piece to an empty neighboring field (up, down, left or right),

    > jump over one neighboring piece to an empty field (up, down, left or right).



    There are 4 moves allowed for each piece in the configuration shown above. As an example let's consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right.

    Write a program that:

    > reads two chessboard configurations from the standard input,

    > verifies whether the second one is reachable from the first one in at most 8 moves,

    > writes the result to the standard output.
     
    Input
    Each of two input lines contains 8 integers a1, a2, ..., a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece - the row number and the column number respectively. Process to the end of file.
     
    Output
    The output should contain one word for each test case - YES if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word NO otherwise.
     
    Sample Input
    4 4 4 5 5 4 6 5
    2 4 3 3 3 6 4 6
     
    Sample Output
    YES
     
     
    Source
    Southwestern Europe 2002
     
    Recommend
    Ignatius.L
     
    /*
    题意:给你四个棋子的坐标,让你判断八步内能不能走到指定位置
    
    初步思路:bfs(),总共四个棋子,每个棋子都有最多四种走法,四个坐标是一种状态,vis八维记录四个棋子的状态
    
    #超内存:什么鬼...找到了,可能是bfs()的时候,爆了
    
    #改进:靠,手残了,判断是不是最后一步的时候,写残了,因为最后四个棋子并不是按照顺序到达四个位置的
    */
    #include<bits/stdc++.h>
    using namespace std;
    struct node{
        int step;
        int x[4],y[4];
    };//四个棋子的状态
    node Start;//初状态
    node last;//末状态
    bool mapn[8][8];
    bool vis[8][8][8][8][8][8][8][8];//用于记录四个棋子的八个坐标的状态
    int dir[4][2]={1,0,-1,0,0,1,0,-1};//方向数组
    
    bool Catch(node a){//判断是不是搜索到最后一步
        for(int i=0;i<4;i++){
            if(!mapn[a.x[i]][a.y[i]]) return false;
        }
        return true;
    }
    bool judge(node a,int k){//判断要去的那一步有没有棋子
        for(int i=0;i<4;i++){
            if(i!=k&&a.x[i]==a.x[k]&&a.y[i]==a.y[k]) return true;
        }
        return false;
    }
    bool ok(node a){//判断坐标是否合法
        for(int i=0;i<4;i++){
            if(a.x[i]<0||a.x[i]>=8||a.y[i]<0||a.y[i]>=8) return false;
        }
        if(vis[a.x[0]][a.y[0]][a.x[1]][a.y[1]]
                [a.x[2]][a.y[2]][a.x[3]][a.y[3]]==true) return false;
        return true;
    }
    bool bfs(){
        queue<node>q;
        node Next;
        
        vis[Start.x[0]][Start.y[0]][Start.x[1]][Start.y[1]]
            [Start.x[2]][Start.y[2]][Start.x[3]][Start.y[3]]=true;
        
        Start.step=0;
        
        q.push(Start);
        while(!q.empty()){
            Start=q.front();
            q.pop();
            // for(int i=0;i<4;i++){
                // cout<<Start.x[i]+1<<" "<<Start.y[i]+1<<" ";
            // }
            // cout<<endl;
            if(Start.step>=8) return false;
            for(int i=0;i<4;i++){//遍历四个棋子
                for(int j=0;j<4;j++){//遍历四个方向
                
                    Next=Start;
                    Next.x[i]+=dir[j][0];
                    Next.y[i]+=dir[j][1];//走向下一步
                    Next.step++;
                    if(ok(Next)==false) continue;//下一步违法的
                    
                    //cout<<"ok"<<endl;
                    
                    if(judge(Next,i)==true){//下一步有棋子了
                        Next.x[i]+=dir[j][0];
                        Next.y[i]+=dir[j][1];//再走一步
                        
                        if(ok(Next)==false||judge(Next,i)==true) continue;//下一步违法的或者下一步还是有棋子
                        
                        else{
                            if(Catch(Next)==true){
                                // cout<<q.size()<<endl;
                                return true;
                            }
                            vis[Next.x[0]][Next.y[0]][Next.x[1]][Next.y[1]]
                                [Next.x[2]][Next.y[2]][Next.x[3]][Next.y[3]]=true;
                                
                            q.push(Next);//放进队列中
                        }
                    }else{
                        if(Catch(Next)==true){
                            // cout<<q.size()<<endl;
                            return true;
                        }
                        vis[Next.x[0]][Next.y[0]][Next.x[1]][Next.y[1]]
                            [Next.x[2]][Next.y[2]][Next.x[3]][Next.y[3]]=true;
                            
                        q.push(Next);//放进队列中
                    }
                }
            }
        }
        return false;
    }
    void init(){
        memset(vis,false,sizeof vis);
        memset(mapn,false,sizeof mapn);
    }
    int main(){
        // freopen("in.txt","r",stdin);
        while(scanf("%d%d",&Start.x[0],&Start.y[0])!=EOF){
            init();
            Start.x[0]--;
            Start.y[0]--;
            for(int i=1;i<4;i++){
                scanf("%d%d",&Start.x[i],&Start.y[i]);
                Start.x[i]--;
                Start.y[i]--;
            }
            //标记初状态
            
            for(int i=0;i<4;i++){
                scanf("%d%d",&last.x[i],&last.y[i]);
                last.x[i]--;
                last.y[i]--;
                mapn[last.x[i]][last.y[i]]=true;
            }
            
            // for(int i=0;i<4;i++){
                // cout<<last.x[i]+1<<" "<<last.y[i]+1<<" ";
            // }
            // cout<<endl;
            
            bool flag=bfs();
            printf(flag==true?"YES
    ":"NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    C语言编程的两个工具:valgrind和core
    C语言动态库和静态库的使用及实践
    编译安装pgbouncer-checking for OpenSSL... configure: error: not found
    automake使用
    make笔记
    GCC命令
    gcc中关于静态库和动态库使用(转)
    zookeeper的c API 单线程与多线程问题 cli_st和cli_mt
    zookeeper数据一致性与paxos算法
    Deepgreen DB简介(转)
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6396055.html
Copyright © 2011-2022 走看看