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种特 ...
随机推荐
- Centos记录所有用户登录和操作的详细日志
1.起因 最近Linux服务器上一些文件呗篡改,想追查已经查不到记录了,所以得想个办法记录下所有用户的操作记录. 一般大家通常会采用history来记录,但是history有个缺陷就是默认是1000行 ...
- VHDL的库
STD_LOGIC_ARITH 扩展了UNSIGNED.SIGNED.SMALL_INT(短整型)三个数据类型,并定义了相关的算术运算和转换函数. --======================== ...
- @WebService @WebMethod 详解
形象图解 首先AB均需要@WebService如果 @WebService(endpointInterface="package.B") public class A implem ...
- Spring源码解析 - AbstractBeanFactory 实现接口与父类分析
我们先来看类图吧: 除了BeanFactory这一支的接口,AbstractBeanFactory主要实现了AliasRegistry和SingletonBeanRegistry接口. 这边主要提供了 ...
- Amazon成本和产出的衡量方式
Amazon用一种T-Shirt Size 估计的方式来做项目. 产品经理会对每一条需求评估上业务影响力的尺寸,如:XXXL 影响一千万人以上或是可以占到上亿美金的市场,XXL,影响百万用户或是占了千 ...
- 【图论】信手拈来的Prim,Kruskal和Dijkstra
关于三个简单的图论算法 prim,dijkstra和kruskal三个图论的算法,初学者容易将他们搞混,所以放在一起了. prim和kruskal是最小生成树(MST)的算法,dijkstra是单源最 ...
- 结对编程--四则运算(Java)梅进鹏 欧思良
结对编程--四则运算(Java)梅进鹏 欧思良 Github项目地址:https://github.com/MeiJinpen/Arithmetic 功能要求 题目:实现一个自动生成小学四则运算题目的 ...
- Delphi Language Overview
Delphi is a high-level, compiled, strongly typed language that supports structured and object-orient ...
- 配置Jenkins构建失败触发邮件报警机制
系统管理 1.进入系统管理-->系统设置 定位到Jenkins Location配置项 配置系统管理员邮件地址 系统管理员邮件地址需要同发送报警邮件地址相同 定位到邮件通知 配置SMTP ...
- Configure Pi as simulation hardware for Simulink
1. Only version not older than R2013 supports Raspberry Pi. First, download support package in Matla ...