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; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- [数论]poj2635__The Embarrassed Cryptographer
- [二分图匹配]poj2446__Chessboard
- POJ1050 最大子矩阵和
- 用单调栈解决最大连续矩形面积问题