Curling 2.0(POJ-3009)

典型DFS搜索。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int w,h,maxn;
bool ok ;
int board[25][25];
int dx[] = {1,0,-1,0} ;
int dy[] = {0,1,0,-1} ;
struct pa{
    int x,y;
    pa(int x = 0,int y = 0) : x(x),y(y) {}
};
void dfs(int cur,pa a) {
    for(int j=0;j<4;j++) {
        if(cur+1>10) return ;
        if(cur+1>=maxn) return ;
        if(j==0) {
            for(int i=a.x-1;i>=0;i--) {
                if(board[i][a.y]==0) continue;
                else if(board[i][a.y]==1&&i==a.x-1) break;
                else if(board[i][a.y]==1){
                    board[i][a.y]=0;
                    dfs(cur+1,pa(i+1,a.y));
                    board[i][a.y] = 1;
                    break;
                }
                else if(board[i][a.y]==3) {
                    maxn = cur+1;
                    ok = true;
                    break;
                }
            }
        }
        else if(j==2) {
            for(int i=a.x+1;i<h;i++) {
                if(board[i][a.y]==0) continue;
                else if(board[i][a.y]==1&&i==a.x+1) break;
                else if(board[i][a.y]==1) {
                    board[i][a.y] = 0;
                    dfs(cur+1,pa(i-1,a.y));
                    board[i][a.y] = 1;
                    break;
                }
                else if(board[i][a.y]==3) {
                    maxn = cur+1;
                    ok = true;
                    break;
                }
            }
        }
        else if(j==3) {
            for(int i=a.y-1;i>=0;i--) {
                if(board[a.x][i]==0) continue;
                else if(board[a.x][i]==1&&i==a.y-1) break;
                else if(board[a.x][i]==1) {
                    board[a.x][i] = 0;
                    dfs(cur+1,pa(a.x,i+1));
                    board[a.x][i] = 1;
                    break;
                }
                else if(board[a.x][i]==3) {
                    maxn = cur+1;
                    ok = true;
                    break;
                }
            }
        }
        else {
            for(int i=a.y+1;i<w;i++) {
                if(board[a.x][i]==0) continue;
                else if(board[a.x][i]==1&&i==a.y+1) break;
                else if(board[a.x][i]==1) {
                    board[a.x][i] = 0;
                    dfs(cur+1,pa(a.x,i-1));
                    board[a.x][i] = 1;
                    break;
                }
                else if(board[a.x][i]==3) {
                    maxn = cur+1;
                    ok = true;
                    break;
                }
            }
        }
    }
}
int main() {
    while(scanf("%d%d",&w,&h)!=EOF) {
        if(w==0&&h==0) return 0;
        pa s,g;
        for(int i=0;i<h;i++)
        for(int j=0;j<w;j++){
            scanf("%d",&board[i][j]) ;
            if(board[i][j]==2) { s = pa(i,j); board[i][j] = 0; }
            if(board[i][j]==3) g = pa(i,j);
        }
        maxn = 100000000;
        ok = false;
        dfs(0,s);
        if(ok) printf("%d\n",maxn);
        else printf("-1\n");
    }
    return 0;
}
时间: 2024-08-29 14:37:42

Curling 2.0(POJ-3009)的相关文章

poj 3009 Curling 2.0 (dfs )

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

POJ 3009 Curling 2.0 (dfs)

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

http://poj.org/problem?id=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. The

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

poj 3009 Curling 2.0 (BFS)

题目大意要求把一个冰壶从起点"2"用最少的步数移动到终点"3" 其中0为移动区域,1为石头区域,冰壶一旦想着某个方向运动就不会停止,也不会改变方向(想想冰壶在冰上滑动),除非冰壶撞到石头1 或者 到达终点 3 冰壶撞到石头后,冰壶会停在石头前面,此时(静止状态)才允许改变冰壶的运动方向,而该块石头会破裂,石头所在的区域由1变为0. 也就是说,冰壶撞到石头后,并不会取代石头的位置. 终点是一个摩擦力很大的区域,冰壶若到达终点3,就会停止在终点的位置不再移动. 要先明确

简单深搜(poj 3009)

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

poj3009 Curling 2.0(很好的题 DFS)

https://vjudge.net/problem/POJ-3009 做完这道题,感觉自己对dfs的理解应该又深刻了. 1.一般来说最小步数都用bfs求,但是这题因为状态记录很麻烦,所以可以用dfs. 2.在用dfs的时候,mp时一个全局变量,对于平等的走法,每一个走法结束后一定要状态复原!!!(也就是代码36-38行)否则会对其他走法产生影响. 1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #

poj 3009 Curling 2.0 【DFS】

题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者3)处才能停止,(就是反射来反射去知道反射经过3).如果反射10次还不能到达3,就输出-1. 策略:深搜. 易错点,方向不容易掌握,并且,出题人把n, m顺序反了. 代码: #include<stdio.h> #include<string.h> int map[25][25]; int ans, n, m; const int dir[4][2] = {

【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