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种特 ...
随机推荐
- 2018.10.05 NOIP模拟 上升序列(状压dp)
传送门 状压dp好题. 首先需要回忆O(nlogn)O(nlog n)O(nlogn)求lislislis的方法,我们会维护一个单调递增的ddd数组. 可以设计状态f(s1,s2)f(s1,s2)f( ...
- mybatis后台中传参到sql语句中,使用@Param注解
- Android draw Rect 坐标图示
前两天在博客发了在例子 android Canvas类介绍 http://byandby.javaeye.com/blog/825330 建议大家 点进去 看一看 不然下边没办法 继续啊. 我还是把这 ...
- day16(jdbc进阶,jdbc之dbUtils)
1.jdbc进阶 jdbc事务管理 jdbc中的事务管理其实就是交给了连接对象去管理.先写一个简单的事务管理 public class Demo01 { private static Connecti ...
- AtCoder - 4351 Median of Medians(二分+线段树求顺序对)
D - Median of Medians Time limit : 2sec / Memory limit : 1024MB Score : 700 pointsProblem Statement ...
- Spring MVC深入讲解
一.前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring3 MVC结构简单,应了那句话简单就是美,而 ...
- Linux应用监控工具
Linux下的监控工具丰富繁杂,如果只知道top.free之类的就太少了,而且也不能胜任日常的Linux管理工作,尤其是在排除Web服务器问题时. 本文给出5个Linux下功能更为强大的监控工具,有了 ...
- ==和Equals与值类型和引用类型
==和Equals 对于值类型来说判断的是值,对于引用类型来说判断的是堆地址 注意:string 是引用类型(也可看做只读char[]数组)(字符串的不可变性·拘留池)特殊的值类型(使用==.Equa ...
- vs 2015 结合新配置的IIS 发布网站过程中遇到的问题及解决办法?
1.由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序 错误: HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添 ...
- C#判断本地文件,网络文件是否存在是否存在
File.Exists 方法 (String) 确定指定的文件是否存在. 命名空间: System.IO程序集: mscorlib(位于 mscorlib.dll) 参数 path Type: ...