poj 3009 Curling 2.0 (BFS)

题目大意要求把一个冰壶从起点“2”用最少的步数移动到终点“3”

其中0为移动区域,1为石头区域,冰壶一旦想着某个方向运动就不会停止,也不会改变方向(想想冰壶在冰上滑动),除非冰壶撞到石头1 或者 到达终点 3

冰壶撞到石头后,冰壶会停在石头前面,此时(静止状态)才允许改变冰壶的运动方向,而该块石头会破裂,石头所在的区域由1变为0. 也就是说,冰壶撞到石头后,并不会取代石头的位置。

终点是一个摩擦力很大的区域,冰壶若到达终点3,就会停止在终点的位置不再移动。

要先明确:

0为滑动区域

1为石头区域

2为起点,也是可滑动区域

3为终点,不可滑动区域

#include <iostream>

using namespace std;

struct SE
{
    int r, c;
    bool status;
}s, e;

int w, h;
int MinStep;
int board[30][30];

void DFS(int i, int j, bool status, int direction, int step, bool flag)
{
    //direction:冰壶当前运动方向  North:0  West:1  South:2  East:3
    //flag:是否消除direction方向下一格位置的石头
    if(step>10)
        return;

    if(board[i][j]==3)
    {
        if(MinStep>step)
            MinStep=step;
        return;
    }

    if(flag)
    {
        switch(direction)
        {
            case 0:{board[i-1][j]=0;break;}
            case 1:{board[i][j-1]=0;break;}
            case 2:{board[i+1][j]=0;break;}
            case 3:{board[i][j+1]=0;break;}
        }
    }

     if(!status)  //静止
    {
        if(i-1>=1 && (board[i-1][j]==0 || board[i-1][j]==3))  //North
            DFS(i-1,j,true,0,step+1,false);

        if(j-1>=1 && (board[i][j-1]==0 || board[i][j-1]==3))  //West
            DFS(i,j-1,true,1,step+1,false);

        if(i+1<=h && (board[i+1][j]==0 || board[i+1][j]==3))  //South
            DFS(i+1,j,true,2,step+1,false);

        if(j+1<=w && (board[i][j+1]==0 || board[i][j+1]==3))  //East
            DFS(i,j-1,true,3,step+1,false);
    }
    else if(status)  //运动
    {
        switch(direction)
        {
            case 0:
                {
                    if(i-1<1)  //预判下一步是否越界
                        return;
                    else
                    {
                        if(board[i-1][j]==0)          //下一位置为0且不越界,继续运动
                            DFS(i-1,j,true,0,step,false);
                        else if(board[i-1][j]==1)          //下一位置为1且不越界,停止运动,并消除下一位置的石头
                            DFS(i,j,false,0,step,true);
                        else if(board[i-1][j]==3)          //下一位置为3且不越界,运动到位置3后停止运动,游戏结束
                            DFS(i-1,j,false,0,step,false);
                    }

                    break;
                }
            case 1:
                {
                    if(j-1<1)  //预判下一步是否越界
                        return;
                    else
                    {
                        if(board[i][j-1]==0)          //下一位置为0且不越界,继续运动
                            DFS(i,j-1,true,1,step,false);
                        else if(board[i][j-1]==1)          //下一位置为1且不越界,停止运动,并消除下一位置的石头
                            DFS(i,j,false,1,step,true);
                        else if(board[i][j-1]==3)          //下一位置为3且不越界,运动到位置3后停止运动,游戏结束
                            DFS(i,j-1,false,1,step,false);
                    }

                    break;
                }
            case 2:
                {
                    if(i+1>h)  //预判下一步是否越界
                        return;
                    else
                    {
                        if(board[i+1][j]==0)          //下一位置为0且不越界,继续运动
                            DFS(i+1,j,true,2,step,false);
                        else if(board[i+1][j]==1)          //下一位置为1且不越界,停止运动,并消除下一位置的石头
                            DFS(i,j,false,2,step,true);
                        else if(board[i+1][j]==3)          //下一位置为3且不越界,运动到位置3后停止运动,游戏结束
                            DFS(i+1,j,false,2,step,false);
                    }

                    break;
                }
            case 3:
                {
                    if(j+1>w)  //预判下一步是否越界
                        return;
                    else
                    {
                        if(board[i][j+1]==0)          //下一位置为0且不越界,继续运动
                            DFS(i,j+1,true,3,step,false);
                        else if(board[i][j+1]==1)          //下一位置为1且不越界,停止运动,并消除下一位置的石头
                            DFS(i,j,false,3,step,true);
                        else if(board[i][j+1]==3)          //下一位置为3且不越界,运动到位置3后停止运动,游戏结束
                            DFS(i,j+1,false,3,step,false);
                    }

                    break;
                }
        }
    }

    if(flag)  //回溯前还原石头,即还原上一步的棋盘状态
    {
        switch(direction)
        {
            case 0: {board[i-1][j]=1; break;}
            case 1: {board[i][j-1]=1; break;}
            case 2: {board[i+1][j]=1; break;}
            case 3: {board[i][j+1]=1; break;}
        }
    }

    return;
}

int main(void)
{
    while(cin>>w>>h)
    {
        if(!w && !h)
            break;

        /*Structure the Board*/

        MinStep=11;

        for(int i=1;i<=h;i++)
            for(int j=1;j<=w;j++)
            {
                cin>>board[i][j];

                if(board[i][j]==2)
                {
                    s.r=i;
                    s.c=j;
                    s.status=false;
                    board[i][j]=0;  //记录起点位置后,把它作为0处理
                }
                if(board[i][j]==3)  //终点是特别位置,冰壶经过或到达该格都会停止
                {
                    e.r=i;
                    e.c=j;
                }
            }

        DFS(s.r , s.c , s.status , 0 , 0 , false);

        if(MinStep<=10)
            cout<<MinStep<<endl;   //DFS里面虽然剪枝了,但是可能把全部走法都剪了,因此还是要判断
        else
            cout<<-1<<endl;

    }
    return 0;
}
时间: 2024-08-24 14:52:38

poj 3009 Curling 2.0 (BFS)的相关文章

poj 3009 Curling 2.0 (dfs )

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

POJ 3009 Curling 2.0 (dfs)

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12700   Accepted: 5343 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

poj 3009 Curling 2.0 【DFS】

题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者3)处才能停止,(就是反射来反射去知道反射经过3).如果反射10次还不能到达3,就输出-1. 策略:深搜. 易错点,方向不容易掌握,并且,出题人把n, m顺序反了. 代码: #include<stdio.h> #include<string.h> int map[25][25]; int ans, n, m; const int dir[4][2] = {

POJ3009:Curling 2.0(dfs)

http://poj.org/problem?id=3009 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. The

POJ 3009 Curling 2.0 {广度优先搜索}

原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose

POJ 3009 ( Curling 2.0 )

题目链接:http://poj.org/problem?id=3009 题意: (1) 一个球只能沿着上下左右方向移动,且只能走直线,沿着一个方向一直走下去: (2)若碰到障碍物冰块就会停止,停止位置是在障碍物之前的位置,并且停止位置的下一位置的障碍物消失,这种情况算滚动1次: (3)游戏结束的情况是:滚动次数超过10次,或者球滚出界了,游戏结束并且输出 - 1: 思路:         这道题一看感觉无从下手,和迷宫类的搜索不一样,搜索不是一步一步的,而是整条直线的搜,但是正是滚动次数不超过1

poj 3009 Curling 2.0 (dfs)

id=3009">链接:poj 3009 题意:在一个冰面网格板上,有空白处(无障碍),和障碍块.有一个小石头,给定其起点和终点.求从起点到终点的最小步数 规则:小石头不能在障碍区运动,一旦从某一方向開始运动,不会改变方向,也不会停止.除非碰到障碍物或到达终点才会停止,这为一步.若碰到障碍物.小石头将停在障碍物的旁边,被碰到的一个障碍物将消失. 输入:1代表障碍物(不可到达),0代表空白区,2,代表起点.3代表终点 输出:若小石头能到达终点,且步数最多为十步,输出最小步数,否则输出-1.

【POJ - 3669】Meteor Shower(bfs)

-->Meteor Shower 直接上中文了 Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平面坐标系的原点放牧,打算在群星断其生路前转移至安全地点. 此次共有M (1 ≤ M ≤ 50,000)颗流星来袭,流星i将在时间点Ti (0 ≤ Ti  ≤ 1,000) 袭击点 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300).每颗流星都将摧毁落点及其相邻四点的区

POJ 3009 Curling 2.0

Curling 2.0 Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 300964-bit integer IO format: %lld      Java class name: Main On Planet MM-21, after their Olympic games this year, curling is getting popular. But