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;

}

 

 

你可能感兴趣的:(curl)