pku 3009 Curling 2.0 DFS

http://poj.org/problem?id=3009

注意两点:

1:只有当冰壶的所有可能走的方向上有0可以移动时,他才能移动,这里就是这组数据的解释

6 1
1 1 2 1 1 3 因为2左右都是1不能移动座椅不能走。
2:但冰壶蹦到障碍物时,障碍物会破碎,此处就会变成可以走的路线了。
View Code
#include <cstdio>
#include <cstring>
#define maxn 24
using namespace std;

int sx,sy,ex,ey;
bool flag;
int ans,n,m,map[maxn][maxn];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

void dfs(int x,int y,int len)
{
    int i,tx,ty;
    if (len > 10) return ;
    for (i = 0; i < 4; ++i)
    {
        tx = x + dir[i][0];
        ty = y + dir[i][1];
        if (tx >= 0 && tx < n && ty >= 0 && ty < m && map[tx][ty] != 1)
        {
            //移动后一直在这个方向上走,知道遇见3或者1
            while ((map[tx][ty] == 0 || map[tx][ty] == 2) && tx >= 0 && tx < n && ty >= 0 && ty < m)
            {
                tx += dir[i][0];
                ty += dir[i][1];
            }
            //找到终点
            if (map[tx][ty] == 3)
            {
                if (ans > len) ans = len;
                flag = true;
                return ;
            }
            //障碍物破碎,重新选择方向
            else if (map[tx][ty] == 1)
            {
                map[tx][ty] = 0;
                dfs(tx - dir[i][0],ty - dir[i][1],len + 1);
                map[tx][ty] = 1;
            }
        }
    }
}

int main()
{
    //freopen("d.in","r",stdin);
    int i,j;
    while (scanf("%d%d",&m,&n))
    {
        if (!n && !m) break;
        memset(map,0,sizeof(map));
        for (i = 0; i < n; ++i)
        {
            for (j = 0; j < m; ++j)
            {
                scanf("%d",&map[i][j]);
                if (map[i][j] == 2)//记录起点
                {
                    sx = i; sy = j;
                }
                /*if (map[i][j] == 3)
                {
                    ex = i; ey = j;
                }*/ // 诧异 这一段在程序中根本没有用到,我带着这段代码提交wa去了这段代码AC求解啊??
                
            }
        }
        flag = false; ans = 15;
        dfs(sx,sy,1);
        if (flag) printf("%d\n",ans);
        else printf("-1\n");
    }
    return 0;
}

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值