POJ3083
来源:互联网 发布:c语言考题 编辑:程序博客网 时间:2024/06/17 07:48
#include "stdafx.h"#include <iostream>#include <limits>using namespace std;int n,row,col;int leftDir,rightDir;int leftStep,rightStep,minStep;int totalStep = 1;int sPositionX,sPositionY,ePositionX,ePositionY;char data[50][50];int leftBonus[4][2] = {{1,0},{0,-1},{-1,0},{0,1}}; // 顺序:下左上右,朝着屏幕左方走时的优先顺序(优先向左)int rightBonus[4][2] = {{0,1},{-1,0},{0,-1},{1,0}}; // 顺序:右上左下,朝着屏幕上方走时的优先顺序(优先向右)int plus[8] = {1,0,0,1,0,-1,-1,0};int dfs(int x, int y, int dir, char method){ int bonus[4][2]; int newX,newY,newDir; int dis = 0; for(int i=0; i<4; i++){ dis = (i+dir)%4; //这两个值费了一些周折。总之,屏幕的朝向和优先顺序的值,不要混为一谈。 newDir = (dis + 3)%4; if(method == 'l'){ newX = x + leftBonus[dis][0]; newY = y + leftBonus[dis][1]; }else{ newX = x + rightBonus[dis][0]; newY = y + rightBonus[dis][1]; } if(data[newX][newY] == '#' || data[newX][newY] == 'S') continue; else if(data[newX][newY] == 'E'){ totalStep++; return totalStep; }else{ totalStep++; return dfs(newX, newY, newDir, method); } }}int dp(int i, int j){ // 这里也费了一番周折。主要是在选择了一个地点之后,就应该把这个地点的状态改变。另外关于边界条件要注意,考虑周到 if(i>=1 && i<=row && j>=1 && j<=col){ int num = 0; if(data[i][j] == 'S'){ data[i][j] = '#'; return 1 + std::min(std::min(dp(i+1,j),dp(i,j+1)),std::min(dp(i-1,j),dp(i,j-1))); } if(data[i][j] == '#'){ return 9999; } if(data[i][j] == 'E'){ return 1; } for(int k=0; k<8; k+=2){ if(data[i+plus[k]][j+plus[k+1]] == '#'){ num++; } } if(num == 3){ data[i][j] = '#'; return 1 + std::min(std::min(dp(i+1,j),dp(i,j+1)),std::min(dp(i-1,j),dp(i,j-1))); } if(data[i][j] == '.'){ data[i][j] = '#'; return 1 + std::min(std::min(dp(i+1,j),dp(i,j+1)),std::min(dp(i-1,j),dp(i,j-1))); } }else{ return 99999; }}// 表示正在朝着屏幕哪个方向走。0表示朝着屏幕上方走,1表示右,2表示下,3表示左int getDirection(int x, int y, char method){ if(method == 'l'){ if(x == row) return 1; if(y == 1) return 2; if(x == 1) return 3; if(y == col) return 0; }else{ if(x == row) return 0; if(y == 1) return 3; if(x == 1) return 2; if(y == col) return 1; }}int main(){ cin >> (int)n; for(int i=0; i<n; i++){ cin >> col; cin >> row; memset(data,'#',sizeof(data)); for(int j=1; j<=row; j++){ for(int k=1; k<=col; k++){ cin >> data[j][k]; if(data[j][k] == 'S'){ sPositionX = j; sPositionY = k; }else if(data[j][k] == 'E'){ ePositionX = j; ePositionY = k; } } } leftDir = getDirection(sPositionX, sPositionY,'l'); rightDir = getDirection(sPositionX, sPositionY,'r'); leftStep = dfs(sPositionX,sPositionY,leftDir,'l'); cout << "leftStep:" << leftStep << endl; totalStep = 1; rightStep = dfs(sPositionX,sPositionY,rightDir,'r'); cout << "rightStep:" << rightStep << endl; totalStep = 1; minStep = dp(sPositionX, sPositionY); cout << "minStep:" << minStep << endl; } return 0;}
1 0
- poj3083
- poj3083
- poj3083
- poj3083
- poj3083
- POJ3083
- poj3083
- poj3083
- POJ3083
- poj3083
- poj3083解题报告
- POJ3083 广搜&模拟
- poj3083 dfs + bfs
- poj3083 各种搜
- bfs+dfs(poj3083)
- [DFS && BFS]poj3083
- POJ3083 -- BFS和DFS
- POJ3083 DFS&BFS
- Thinkphp 插入数据丢失
- Android Studio提示:Plugin with id 'com.github.dcendents.android-maven' not found
- 百练 肿瘤检测
- 盒子在浏览器中所占空间-width\height\padding\border\margin
- 广告条自动轮播
- POJ3083
- BYTE WORD DWORD
- Java break&continue标签的使用
- datagridview 随窗口大小改变自动调整列宽
- Java 求前n项偶数和
- 在c#中当引入的dll与当前的.net版本不支持时,需要加入下列语句
- 用UML建模需要注意的问题--强烈推荐的好文章
- 归并排序
- Git的工作区与版本库的关系