POJ3009 Curling 2.0(DFS)

迷宫问题求最短路。略有不同的是如果不碰到石头的话会沿着一个方向一直前进,出界就算输了。碰到石头,前方石头会消失,冰壶停在原地。把这个当作状态的转移。DFS可以求出其最小操作数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#define ll __int64
#define INF 0x3f3f3f
using namespace std;

int n,m;
int ex,ey;
int Min;
int dir[4][2]= {{1,0},{0,1},{0,-1},{-1,0}};
int map[25][25];

void dfs(int x,int y,int count)
{
    if(count>10) return;
    for(int i=0; i<4; i++)
    {
        int move_ok=0;
        int xx=x,yy=y;
        if(map[x+dir[i][0]][y+dir[i][1]]==1) continue;
        while(true)
        {
            xx=xx+dir[i][0];
            yy=yy+dir[i][1];
            if(xx<0||xx>=n||yy<0||yy>=m) break;
            if(map[xx][yy]==1)
            {
                move_ok=1;
                break;
            }
            if(map[xx][yy]==3)
            {
                if(Min>count)
                {
                    Min=count;
                    break;
                }
            }
        }
        if(move_ok)
        {
            map[xx][yy]=0;
            dfs(xx-dir[i][0],yy-dir[i][1],count+1);
            map[xx][yy]=1;
        }
    }
}

int main()
{
    //freopen("d:\\test.txt","r",stdin);
    int sx,sy;
    while(cin>>m>>n)
    {
        if(m==0&&n==0) break;
        memset(map,0,sizeof(map));
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>map[i][j];
                if(map[i][j]==2)
                {
                    sx=i;
                    sy=j;
                }
            }
        }
        Min=11;
        dfs(sx,sy,1);
        if(Min>10) cout<<"-1"<<endl;
        else cout<<Min<<endl;
    }
    return 0;
}
时间: 2024-12-18 21:53:38

POJ3009 Curling 2.0(DFS)的相关文章

POJ3009——Curling 2.0(DFS)

Curling 2.0 DescriptionOn 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

POJ3009 Curling 2.0(DFS 好题)

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15262   Accepted: 6334 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

poj3009 Curling 2.0(DFS回溯)

题目大意是:给你一个球,刚开始是静止的,可以通过触碰给他一个初速度,一旦球运动起来就不会停止,除非遇到一个石头.遇到石头以后小球就原地停止了,然后石头就被砸碎了.小球越界就算失败了.问你最少进行多少次操作,可以让小球到达终点.题中还有一个要求,如果超过10步,就算失败了. 这道题目做了好久啊.可能方法太麻烦了. #include<stdio.h> #include<string.h> int map[105][105],si,sj,ei,ej,w,h; //1 right    2

poj3009 Curling 2.0 DFS水

http://poj.org/problem?id=3009 题意:给定一个m*n的网格,在这些网格上一些地方有障碍物,给定起点与终点的位置,当石头从起点开始走,撞上障碍才会转弯,否则会一直沿着来时的方向继续前进.撞到障碍后停在障碍前的位置,障碍消失.然后石头可以选择四个方向(相邻处无障碍的方向)前进,问至少需要停多少次才能从起点到达终点.不能到达或者多余10步后游戏失败.如果能到达输出最少的步数,否则输出-1. 思路:DFS,多余10步为剪枝条件. 1 #include<iostream>

poj 3009 Curling 2.0 dfs回溯

// poj3009 Curling 2.0 // dfs水题,开始的时候没有想到在走了10步以后就不走了这个重要的剪枝, // 结果tle了... // 后来想了个vis数组记录走过的路径,结果发现并不能这样标记,因为每个点可能 // 走多次,所以这样是不对的 // // 哎,继续练吧,水题都差不多搜了一个小时,哎,... #include <algorithm> #include <bitset> #include <cassert> #include <cc

【POJ】3009 Curling 2.0 ——DFS

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 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

poj3009 Curling 2.0 (DFS按直线算步骤)

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14563   Accepted: 6080 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

poj3009 Curling 2.0

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. They use only a single stone. The

Curling 2.0(dfs)

The movement of the stone obeys the following rules: At the beginning, the stone stands still at the start square. The movements of the stone are restricted to x and y directions. Diagonal moves are prohibited. When the stone stands still, you can ma