您的位置:首页 > 其它

poj2251(超时,待修改)

2016-07-29 17:13 375 查看
又是一个迷宫问题,目前把代码写出来了,但是超时了,需要调整

#include "stdafx.h"
#include <iostream>
using namespace std;

int data[35][35][35];
int totalLevel,totalRow,totalCol;
int sLevel,sRow,sCol,eLevel,eRow,eCol;
int newLevel,newRow,newCol;
int bonus[18] = {0,-1,0,0,1,0,0,0,-1,0,0,1,-1,0,0,1,0,0};
int minStep;
char c;

void dfs(int level, int row, int col, int step){
if(data[level][row][col] == 0) return;
else if(level == eLevel && row == eRow && col == eCol){
if(step < minStep) minStep = step;
}else{
for(int i=0; i<18; i+=3){
newLevel = level + bonus[i];
newRow = row + bonus[i+1];
newCol = col + bonus[i+2];
data[level][row][col] = 0;
dfs(newLevel,newRow,newCol,step+1);
}
data[level][row][col] = 1;
}
}

int main(){
while(true){
cout << "\n";
memset(data, 0, sizeof(data));
cin >> totalLevel;
cin >> totalRow;
cin >> totalCol;
if(totalLevel == 0 && totalRow == 0 && totalCol == 0){
break;
}else{
minStep = INT_MAX;
for(int i=1; i<= totalLevel; i++){
for(int j=1; j<=totalRow; j++){
for(int k=1; k<=totalCol; k++){
cin >> c;
if(c == 'S'){
data[i][j][k] = 1;
sLevel = i;
sRow = j;
sCol = k;
}else if(c == '.'){
data[i][j][k] = 1;
}else if(c == 'E'){
data[i][j][k] = 1;
eLevel = i;
eRow = j;
eCol = k;
}
}
}
cout << "\n";
}
}

dfs(sLevel,sRow,sCol,0);

if(minStep < INT_MAX){
cout << "Escaped in " << minStep << " minute(s)." << endl;
}else{
cout << "Trapped!" << endl;
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj