POJ 1101
#include <iostream>
#include <string>
#define MAXN 78
#define min _min
#define inf 123456789
using namespace std; char _m[MAXN][MAXN];
bool mark[MAXN][MAXN];
int dp[MAXN][MAXN];
int dis[][] = {,,,-,,,-,};
int dis_mark[] = {,,,};
int min;
int x_2;
int y_2;
int real_w;
int real_h;
void dfs(int x,int y,int dir,int step);
int main()
{
//freopen("acm.acm","r",stdin);
// freopen("out.txt","w",stdout);
int w;
int h;
int i;
int j;
int x_1;
int y_1;
int temp_x;
int temp_y;
string s;
int time = ;
while(cin>>w>>h,w||h)
{
cout<<"Board #"<<++ time<<":"<<endl;
getchar();
int time_in = ;
real_w = w+;
real_h = h+;
memset(_m,' ',sizeof(_m));
for(i = ; i <= h; ++ i)
{
getline(cin,s);
for(j = ; j <= w; ++ j)
{
_m[i][j] = s[j-];
}
} while(cin>>y_1>>x_1>>y_2>>x_2,x_1||y_1||x_2||y_2)
{ cout<<"Pair "<<++ time_in<<": ";
memset(mark,false,sizeof(mark));
for(i = ; i < MAXN; ++ i)
{
for(j = ; j < MAXN; ++ j)
{
dp[i][j] = inf;
}
}
min = inf;
for(i = ; i < ; ++ i)
{
temp_x = x_1 + dis[i][];
temp_y = y_1 + dis[i][];
if((_m[temp_x][temp_y] != 'X' ||_m[temp_x][temp_y] == 'X' && temp_x == x_2 && temp_y == y_2) && !mark[temp_x][temp_y])
{
mark[temp_x][temp_y] = true;
dfs(temp_x,temp_y,dis_mark[i],); mark[temp_x][temp_y] = false;
}
}
if(min == inf)
{
cout<<"impossible."<<endl;
}
else
{
cout<<min;
cout<<" segments."<<endl;
}
}
cout<<endl;
}
}
//上 1
//下 2
//左 3
//右 4
void dfs(int x,int y,int dir,int step)
{
if(x == x_2 && y == y_2)
{
if(step < min)
{
min = step;
}
return;
} if(step < dp[x][y])
{
dp[x][y] = step;
}
else
{
return;
} int i;
int j;
int temp_x;
int temp_y;// 右
for(i = ; i < ; ++ i)
{
temp_x = dis[i][] + x;
temp_y = dis[i][] + y;
if(temp_x >= && temp_x < real_h && temp_y >= && temp_y <= real_w &&( _m[temp_x][temp_y] != 'X' || _m[temp_x][temp_y] == 'X' && temp_x == x_2 && temp_y == y_2)&& !mark[temp_x][temp_y])
{
mark[temp_x][temp_y] = true;
if(dis_mark[i] != dir)
{
dfs(temp_x,temp_y,dis_mark[i],step+);
if(step+ < dp[temp_x][temp_y])
dp[temp_x][temp_y] = step+;
}
else
{
dfs(temp_x,temp_y,dis_mark[i],step);
if(step < dp[temp_x][temp_y])
dp[temp_x][temp_y] = step;
}
mark[temp_x][temp_y] = false;
}
} }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。

技术网站地址: vmfor.com
POJ 1101的更多相关文章
- OpenJudge 2802 小游戏 / Poj 1101 The Game
1.链接地址: http://bailian.openjudge.cn/practice/2802 http://poj.org/problem?id=1101 2.题目: 总时间限制: 1000ms ...
- 搜索 + 剪枝 --- POJ 1101 : Sticks
Sticks Problem's Link: http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...
- POJ 1101 简单BFS+题意
The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...
- POJ - 1101 The Game dfs
题意:给你一个地图,上面有一些‘X',给你起点终点,让你输出从起点到终点的路径中转向(改变方向)次数最少的路径,注意,不能穿过别的’X'并且可以超过边界 题解:关于超过边界,只要在外围多加一圈‘ ’. ...
- POJ 1101 译文
The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...
- POJ 1101 The Game(BFS+判方向)
The Game Description One morning, you wake up and think: "I am such a good programmer. Why ...
- POJ 2570(floyd)
http://poj.org/problem?id=2570 题意:在海底有一些网络节点.每个节点之间都是通过光缆相连接的.不过这些光缆可能是不同公司的. 现在某个公司想从a点发送消息到b点,问哪个公 ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- POJ 3274 Gold Balanced Lineup(哈希)
http://poj.org/problem?id=3274 题意 :农夫约翰的n(1 <= N <= 100000)头奶牛,有很多相同之处,约翰已经将每一头奶牛的不同之处,归纳成了K种特 ...
随机推荐
- Remove duplicates
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- 2018.07.22 codeforces750E(线段树维护状态转移)
传送门 给出一个数字字串,给出若干个询问,询问在字串的一段区间保证出现2017" role="presentation" style="position: re ...
- 右值引用和std::move函数(c++11)
1.对象移动 1)C++11新标准中的一个最主要的特性就是移动而非拷贝对象的能力 2)优势: 在某些情况下,从旧内存拷贝到新内存是不必要的,此时对对象进行移动而非拷贝可以提升性能 有些类如IO类或un ...
- ArcGIS Desktop python Add-in Python 插件的文件结构
如上图所示: 插件文件夹在根目录下有一个config.xml文件,这个文件保存有在向导添加的描述该插件的定制信息. 插件还有一个安装文件夹,这个文件夹的主要功能是存放Python脚本. 你可以在安装文 ...
- 安装hue-3.11.0
1) 配置HDFS HttpFS和WebHDFS 如果HDFS是HA方式部署的,则只能使用HttpFS,而不能用WebHDFS. 2) 安装依赖: apr-iconv-1.2.1 confuse-3. ...
- 解决Error creating bean with name 'huayuanjingguanDaoimp' defined in file [D:\apache-tomcat-7.0.52\webapps\landscapings\WEB-INF\classes\com\itheima\landscaping\dao\imp\huayuanjingguanDaoimp.class]: Invo
问题描述: 10:23:13,585 ERROR ContextLoader:307 - Context initialization failedorg.springframework.beans. ...
- python正则表达式转义注意事项
无论哪种语言,在使用正则表达式的时候都避免不了一个问题,就是在匹配元字符的时候,需要对元字符进行转义,让 正则表达式引擎将其当做普通字符来匹配.本文主要以python为例,说明一下转义中需要注意的问题 ...
- (求树的直径)Warm up -- HDU -- 4612
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 给一个无向图, 加上一条边后,求桥至少有几个: 那我们加的那条边的两个顶点u,v:一定是u,v之 ...
- gridview的编辑,更新,取消,自动分页等
gridview编辑列,把左下角的"自动生成字段"的复选框的勾去掉 添加boundfield(绑定列)将其datafield设置为productname,headertext设置为 ...
- 基于MATLAB的RGB转YCBCR色彩空间转换
使用MATLAB进行图片的处理十分方便,看它的名字就知道了,矩阵实验室(matrix laboratory).一副图片的像素数据可以看成是一个二维数组一个大矩阵,MTABLAB就是为矩阵运算而生. M ...