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 down    3 left   4 up

int flag,t,dir[4][2]={0,1,1,0,0,-1,-1,0},mini;

int check(int x,int y)

{

if(x<=0||y<=0||x>h||y>w)return 0;

return 1;

}

void dfs(int x,int y,int cnt,int turn,int speed)   //x,y代表小球的坐标,cnt代表当前操作次数,turn代表方向,speed判断小球状态

{

if(check(x,y)==0){flag=1;return;}  //判断是否越界。

if(x==ei&&y==ej){

if(mini>cnt)mini=cnt;

flag=1;t=cnt;return;

}

if(cnt>10)return;

if(speed==0){     //如果小球静止,就对4个方向进行搜索。

for(int i=0;i<4;i++)

{

int fx=x+dir[i][0];

int fy=y+dir[i][1];

if(map[fx][fy]==1)
      //如果周围是石头就跳过

continue;

dfs(fx,fy,cnt+1,i+1,1);

}

}

else {              //如果小球在运动,就按原方向搜索

if(turn==1)

{

if(map[x][y+1]==1)

{

map[x][y+1]=0;

dfs(x,y,cnt,1,0);

map[x][y+1]=1;      .//回溯

}

else dfs(x,y+1,cnt,1,1);

}

if(turn==2){

if(map[x+1][y]==1)

{

map[x+1][y]=0;

dfs(x,y,cnt,2,0);

map[x+1][y]=1;

}

else dfs(x+1,y,cnt,2,1);

}

if(turn==3){

if(map[x][y-1]==1)

{

map[x][y-1]=0;

dfs(x,y,cnt,3,0);

map[x][y-1]=1;

}

else dfs(x,y-1,cnt,3,1);

}

if(turn==4){

if(map[x-1][y]==1)

{

map[x-1][y]=0;

dfs(x,y,cnt,4,0);

map[x-1][y]=1;

}

else dfs(x-1,y,cnt,4,1);

}

}

if(flag&&speed)return;     //这里判断越界以后的状态,因为越界以后回到上一个位置,这时判断小球的状态,如果是静止的,就继续进行搜索。如果是运动的,继续返回(因为小球在一条线上运动时不能自己停止)

else if(flag&&speed==0)flag=0;

}

int main()

{

int i,j,k;

while(scanf("%d%d",&w,&h)!=EOF)

{

if(w==0&&h==0)break;

memset(map,0,sizeof(map));    //注意要对迷宫进行初始化

flag=0;

mini=99999999;

for(i=1;i<=h;i++)

for(j=1;j<=w;j++)

{

scanf("%d",&map[i][j]);

if(map[i][j]==2){

si=i;sj=j;

}

if(map[i][j]==3){

ei=i;ej=j;

}

}

dfs(si,sj,0,1,0);

if(mini<99999999)

printf("%d\n",mini);

else printf("-1\n");

}

return 0;

}

时间: 2024-08-01 10:46:55

poj3009 Curling 2.0(DFS回溯)的相关文章

poj 3009 Curling 2.0 dfs回溯

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

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)

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

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

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

poj 3009 Curling 2.0 (dfs)

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