【sicily】 1135.飞越原野

#include<iostream>
using namespace std;
#include <queue>
#include <stdio.h>
int m,n,d;
char mat[101][101];
struct Node{
    int row,col,ntime,left;              //ntime表示所花费的时间,left表示剩余可飞行距离
    Node (int r,int c,int t,int l):row(r),col(c),ntime(t),left(l){}
    Node(){}
}temp;
int visited[101][101][101];
queue<Node> q;
int rowc[4] = {0,1,0,-1}, colc[4] = {1,0,-1,0};
void bfs(){
    while (!q.empty()){
        temp = q.front(); q.pop();
        
        int cur_left = temp.left;
        int cur_time = temp.ntime;

        if (temp.row == (m-1) && temp.col == (n-1)){
            printf("%d\n",cur_time); return ; }   

        for (int i=0;i<4;++i){
            int cur_row = temp.row + rowc[i];
            int cur_col = temp.col + colc[i];
            
            if (cur_row<0 || cur_row>m-1 || cur_col<0 || cur_col>n-1) continue;

            if (mat[cur_row][cur_col] == 'L') {}
            else{
                if(!visited[cur_row][cur_col][cur_left]){
                    q.push(Node(cur_row, cur_col, cur_time+1, cur_left));
                    visited[cur_row][cur_col][cur_left] = 1;
                }
            }

            for (int j=2;j<=cur_left;++j){
                int fly_row = temp.row + rowc[i]*j;
                int fly_col = temp.col + colc[i]*j;

                if (fly_row<0 || fly_row>m-1 || fly_col<0 || fly_col>n-1) break;
                if (mat[fly_row][fly_col] == 'L') continue;

                if (!visited[fly_row][fly_col][cur_left-j]){
                    q.push( Node(fly_row, fly_col, cur_time+1, cur_left-j));
                    visited[fly_row][fly_col][cur_left-j] = 1;
                }
            }
        }
    }
    cout<<"impossible\n";
}
int main(){
    scanf("%d%d%d",&m,&n,&d);
    for (int i=0;i<m;++i){
        for (int j=0;j<n;++j){
            cin>>mat[i][j];
            for (int k=0;k<=d;++k) visited[i][j][k] = 0;
        }
    }
    q.push(Node(0,0,0,d));
    visited[0][0][d] = 1;
    bfs();
    return 0;
}

posted @ 2011-01-06 00:48  Harrison_  阅读(675)  评论(0编辑  收藏  举报