POJ 3009 深搜



D - Curling 2.0

Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

SubmitStatusPracticePOJ
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. They use only a single stone. The purpose of the game
is to lead the stone from the start to the goal with the minimum number of moves.

Fig. 1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed
until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.

Fig. 1: Example of board (S: start, G: goal)

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 make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. 2(a)).
  • Once thrown, the stone keeps moving to the same direction until one of the following occurs:
    • The stone hits a block (Fig. 2(b), (c)).

      • The stone stops at the square next to the block it hit.
      • The block disappears.
    • The stone gets out of the board.
      • The game ends in failure.
    • The stone reaches the goal square.
      • The stone stops there and the game ends in success.
  • You cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.

Fig. 2: Stone movements

Under the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.

With the initial configuration shown in Fig. 1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. 3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. 3(b).

Fig. 3: The solution for Fig. D-1 and the final board configuration

Input

The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.

Each dataset is formatted as follows.

the width(=w) and the height(=h) of the board

First row of the board

...

h-th row of the board

The width and the height of the board satisfy: 2 <= w <= 20, 1 <=
h
<= 20.

Each line consists of w decimal numbers delimited by a space. The number describes the status of the corresponding square.

0 vacant square
1 block
2 start position
3 goal position

The dataset for Fig. D-1 is as follows:

6 6

1 0 0 2 1 0

1 1 0 0 0 0

0 0 0 0 0 3

0 0 0 0 0 0

1 0 0 0 0 1

0 1 1 1 1 1

Output

For each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.

Sample Input

2 1
3 2
6 6
1 0 0 2 1 0
1 1 0 0 0 0
0 0 0 0 0 3
0 0 0 0 0 0
1 0 0 0 0 1
0 1 1 1 1 1
6 1
1 1 2 1 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 3
0 0

Sample Output

1
4
-1
4
10
-1
题意:给定一个由0,1,2,3 组成的矩阵,0 表示p平地,可以走通,1表示障碍物,不可走,2 表示移动物,可以击碎障碍物并停留在障碍物之前,3是出口,题意要求让你判断找到在有限的步骤(10步)内到达出口;
思路:主要分为两步1.移动到障碍物之前,超出界就说明这个方向不能走2.如何进行下个点的搜索
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
int w,h;
int sx,sy;
int ex,ey;
int sum;
int gra[21][21];
int xx[]={0,0,1,-1};
int yy[]={1,-1,0,0};
int isok(int x,int y){
    if(x>=0&&x<h&&y>=0&&y<w){
        return 1;
    }
    return 0;
}//越界函数的判断
void dfs(int tol,int x,int y){
    int i;
    if(tol>=10){
        return;
    }//走的步数大于10 直接返回
    for(i=0;i<4;i++){//四个方向
        int a=x+xx[i],b=y+yy[i];
        if((gra[a][b]==0)||(gra[a][b]==3)){
            while(gra[a][b]==0){//移动到障碍物之前的方法
                a+=xx[i];
                b+=yy[i];
            }
            if(isok(a,b)){//没有越界
                if(gra[a][b]==3){
                    if(tol+1<sum){
                        sum=tol+1;
                    }
                }//正好到达目标处
                if(gra[a][b]==1){
                    gra[a][b]=0;
                    dfs(tol+1,a-xx[i],b-yy[i]);
                    gra[a][b]=1;
                }
            }
        }
    }
}
int main(){
    while(~scanf("%d%d",&w,&h)&&w&&h){
            sum=11;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                scanf("%d",&gra[i][j]);
                if(gra[i][j]==2){
                    gra[i][j]=0;
                    sx=i;
                    sy=j;
                }
                if(gra[i][j]==3){
                    ex=i;
                    ex=j;
                }
            }
        }
        dfs(0,sx,sy);
        if(sum==11){
            printf("-1\n");
        }
        else{
            printf("%d\n",sum);
        }
    }
    return 0;
}
注意:之前想到用if来移动,相当麻烦,还是要多想想,不能急的敲代码,多做做题。。。。。。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-10 18:18:19

POJ 3009 深搜的相关文章

POJ 2488 深搜dfs、

题意:模拟国际象棋中马的走棋方式,其实和中国象棋的马走的方式其实是一样的,马可以从给定的方格棋盘中任意点开始,问是否能遍历全部格子,能的话输出字典序最小的走棋方式,否则输出impossible 思路:只要能遍历全部的格子,就一定会走A1这个点,而且这个点的字典序是最小的,保证了这点的话还需要保证dfs的8个方向也要按照字典序最小来走,这样就可以确保所走的路径就是字典序最小的 坑爹:自己忘记输出Scenario #i,一连WA了几发,就是不知道自己错在哪里,顺便发一个对照的程序吧我的程序过16MS

POJ 1321 深搜dfs

思路其实挺简单的,为什么我想不到呢!!! 原因分析:(1)题目还是做少了 (2)做题目的时候在放音乐 (3)最近脑袋都不愿意想思路总是想一些无用的 改进:(1)以后做题目坚决不开音乐,QQ直接挂隐身 (2)想题目的时候一定要认真,开启完全状态 (3)对自己再认真一点,加油 1 #include<cstdio> 2 #include<cstring> 3 const int qq=10; 4 char map[qq][qq]; 5 int vis[qq]; 6 int n,k,ans

poj 3009 Curling 2.0 深搜

http://poj.org/problem?id=3009 题意:一个小球在一个格子里滑行,当你给它一个力时,他会一直滑,直到前方碰到一个雪球停止,这时前方的雪球会消失,你继续给该小球任意一个方向的力...问至少需要几步才能到达到终点. 分析: 一般在求  最短路    时会用到   广搜,但是  本题  在搜索时, 每走一步, 现场状态是需要改变的 ,如果该步不满足,又需要把现场状态还原回去  ,这样   深搜  才能满足 因此用  深搜     只能把   所有能到达终点的路的步数    

简单深搜(poj 3009)

题目链接:http://poj.org/problem?id=3009 题目:冰壶撞向目的地,只有遇到"1"才能停下来,并且把"1"撞成"0".只能横冲直撞,不允许蛇皮走位等等骚操作.从"2"要撞到"3",周围有"0",才能向有"0"的地方滑.运动员只能推十次,问最少要多少次才到"3"? 用深搜遍历每一个方向. 1 #include<stdi

(暴力+深搜)POJ - 2718 Smallest Difference

原题链接: http://poj.org/problem?id=2718 题意: 给你几个数字,可以分成两个子集,然后分别按一定顺序排列组成一个数,求出这两只值差的绝对值的最小值. 分析: 反正也是刷着玩,果断先交一波全排列枚举的代码,果断TLE,然后开始想正解. 稍微想想,既然要差最小,肯定是两个数各一半.所以只要深搜出所有n/2(n为给定数字的个数)的组合,另外个n-n/2个数就有了. 但是枚举出来后的操作又想了很久,想过很多算法,都不怎么满意,最终用二分解决. 先把n/2和n-n/2全排列

POJ 2411 Mondriaan&#39;s Dream(状态压缩+深搜)

每一行的填充仅与上一行有关系,每行的目的都是至少填充满上一行. 当填充到i行的时候,i-1行某列没填充必须用竖直的方格填充,这是固定的,剩下其余的则搜索填充. 用2进制的01表示不放还是放 第i行只和i-1行有关 枚举i-1行的每个状态,推出由此状态能达到的i行状态 如果i-1行的出发状态某处未放,必然要在i行放一个竖的方块,所以我对上一行状态按位取反之后的状态就是放置了竖方块的状态. 然后用搜索扫一道在i行放横着的方块的所有可能,并且把这些状态累加上i-1的出发状态的方法数,如果该方法数为0,

poj 2837 Silver Matrix 不使用栈的深搜

题意: 给定k,让构造一个2^k*2^k的矩阵,使得对任意i,第i行和第i列由1,2,...2^k-1这2^k-1个数组成. 分析: 很明显是个深搜题.设n=2^k,则搜素树高度(状态空间维度)为n,每个状态可扩展n个状态,复杂度n^n,大概是512^512...所以必须有强力的剪枝而且不要用递归去写.一般对深搜来说,搜索树高度固定的话可以用for循环直接枚举,不固定话要用递归或栈,这题搜索树高度不固定(n为输入),怎么办呢?..这样可以清楚明了地搞定:dfs的while循环写法. 代码: //

POJ 1129-Channel Allocation(四色定理+迭代深搜)

题目链接:传送门 题意:n个信号站,给出连接情况,要用信号覆盖所有信号站,要求相连的信号站不能用同一个信号. 等价问题==无向图染色==四色定理(每个平面地图都可以只用四种颜色来染色,而且没有两个邻接的区域颜色相同.已证明) 思路:深搜一条路(枚举颜色,判断当前点用已有的颜色能不能染,如不能则加一种颜色,判断强判就行了),搜到头答案就出来了..然后返回就可以了 注意单复数.. #include <algorithm> #include <iostream> #include <

POJ 1321 棋盘问题 深搜+回溯

棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24958 Accepted: 12333 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一