程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU1401:Solitaire(BFS)

HDU1401:Solitaire(BFS)

編輯:C++入門知識

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 fZ喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vciBlYWNoIHBpZWNlIGluIHRoZSBjb25maWd1cmF0aW9uIHNob3duIGFib3ZlLiBBcyBhbiBleGFtcGxlIGxldA=="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


題意就是有四顆棋子,能否在8步內走到目標狀態

開個8維數組記錄狀態即可

#include 
#include 
#include 
#include 
using namespace std;

bool vis[8][8][8][8][8][8][8][8];
bool map[10][10];
int to[4][2] = {1,0,-1,0,0,1,0,-1};

struct point
{
    int x[4],y[4],step;
} s,e;

int check(point a)//判斷是否為最終態
{
    for(int i = 0; i<4; i++)
    {
        if(!map[a.x[i]][a.y[i]])
            return 0;
    }
    return 1;
}

int empty(point 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 0;
    }
    return 1;
}

int judge(point next)//判斷是否符合要求
{
    int i;
    for(i = 0; i<4; i++)
    {
        if(next.x[i]<0 || next.x[i]>=8 || next.y[i]<0 || next.y[i]>=8)
            return 1;
    }
    if(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]])
        return 1;
    return 0;
}

int bfs()
{
    memset(vis,false,sizeof(vis));
    int i,j;
    queue Q;
    point a,next;
    a.step = 0;
    for(i = 0; i<4; i++)
    {
        a.x[i] = s.x[i];
        a.y[i] = s.y[i];
    }
    Q.push(a);
    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;
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        if(a.step>=8)//因為後面循環有判斷減枝,所以這裡要包括8步
            return 0;
        if(check(a))
            return 1;
        for(i = 0; i<4; i++)
        {
            for(j = 0; j<4; j++)
            {
                next = a;
                next.x[i]+=to[j][0];
                next.y[i]+=to[j][1];
                next.step++;
                if(judge(next))
                    continue;
                if(empty(next,i))//要去的那一格是空
                {
                    if(check(next))
                        return 1;
                    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//非空則繼續往前
                {
                    next.x[i]+=to[j][0];
                    next.y[i]+=to[j][1];
                    if(judge(next) || !empty(next,i))//繼續往前也要滿足要求且是空格
                        continue;
                    if(check(next))
                        return 1;
                    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 0;
}

int main()
{
    int i;
    while(~scanf("%d%d",&s.x[0],&s.y[0]))
    {
        s.x[0]--;
        s.y[0]--;
        for(i = 1; i<4; i++)
        {
            scanf("%d%d",&s.x[i],&s.y[i]);
            s.x[i]--;
            s.y[i]--;
        }
        memset(map,false,sizeof(map));
        for(i = 0; i<4; i++)
        {
            scanf("%d%d",&e.x[i],&e.y[i]);
            e.x[i]--;
            e.y[i]--;
            map[e.x[i]][e.y[i]] = true;
        }
        int flag = bfs();
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }

    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved