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;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。