#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的更多相关文章

  1. OpenJudge 2802 小游戏 / Poj 1101 The Game

    1.链接地址: http://bailian.openjudge.cn/practice/2802 http://poj.org/problem?id=1101 2.题目: 总时间限制: 1000ms ...

  2. 搜索 + 剪枝 --- POJ 1101 : Sticks

    Sticks Problem's Link:   http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...

  3. POJ 1101 简单BFS+题意

    The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...

  4. POJ - 1101 The Game dfs

    题意:给你一个地图,上面有一些‘X',给你起点终点,让你输出从起点到终点的路径中转向(改变方向)次数最少的路径,注意,不能穿过别的’X'并且可以超过边界 题解:关于超过边界,只要在外围多加一圈‘ ’. ...

  5. POJ 1101 译文

    The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...

  6. POJ 1101 The Game(BFS+判方向)

        The Game Description One morning, you wake up and think: "I am such a good programmer. Why ...

  7. POJ 2570(floyd)

    http://poj.org/problem?id=2570 题意:在海底有一些网络节点.每个节点之间都是通过光缆相连接的.不过这些光缆可能是不同公司的. 现在某个公司想从a点发送消息到b点,问哪个公 ...

  8. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  9. POJ 3274 Gold Balanced Lineup(哈希)

    http://poj.org/problem?id=3274 题意 :农夫约翰的n(1 <= N <= 100000)头奶牛,有很多相同之处,约翰已经将每一头奶牛的不同之处,归纳成了K种特 ...

随机推荐

  1. 修改BUG心得

      修改BUG心得 分类: 项目管理/CMMI2013-01-14 22:06 845人阅读 评论(0) 收藏 举报 目录(?)[-] 一 二 三 一. 1.写第一版时就杜绝这些的发生. 2.思维要开 ...

  2. 2018.09.15 poj1041John's trip(欧拉路输出方案)

    传送门 一个欧拉路输出方案的板子题. 竟然难在读入233. 代码: #include<iostream> #include<cstdio> #include<cstrin ...

  3. js动态添加删除行,兼容ie和火狐

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Linux学习--2

    字符串 字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似 单引号字符串的限制:单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的:单引号字串中不能出现单引号(对 ...

  5. PCL(point cloud library) 学习——简介

    Point Cloud Library (PCL) 是开源点云处理库, 包括 filtering, feature estimation, surface reconstruction, regist ...

  6. PHP数据库抽象层--PDO(PHP Data Object) [一]

    1.简介:(PDO数据库访问抽象层,统一各种 数据库的访问接口 ) PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口.实现 PDO 接口的每个数据库驱动可以公开具体数据库 ...

  7. (转)Entity Framework Extended Library (EF扩展类库,支持批量更新、删除、合并多个查询等)

    转自:http://www.cnblogs.com/jinzhao/archive/2013/05/31/3108755.html 今天乍一看,园子里居然没有关于这个类库的文章,实在是意外毕竟已经有很 ...

  8. ( KMP 求循环节的个数)Power Strings -- poj -- 2406

    链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bi ...

  9. POJ3273 Monthly Expense 2017-05-11 18:02 30人阅读 评论(0) 收藏

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25959   Accepted: 10021 ...

  10. bootstrap modal

    模态框提供了两个可选尺寸,通过为 .modal-dialog 增加一个样式调整类实现.加modal-lg,加modal-sm,不加也可以,共有三种尺寸. 触发方式,data-target, 感觉比js ...