#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. crontab误删除

    命令如下: cat /var/log/cron* | grep -i "`which cron`" > ./all_temp cat ./all_temp | grep -v ...

  2. Ehcache配置参数简介

    < defaultCache         maxElementsInMemory = "10000"         maxElementsOnDisk = " ...

  3. 2018.10.15 NOIP训练 hyc的等比数列(数论+枚举)

    传送门 一道不错的枚举题. 显然桶排序之后瞎枚举一波. 考虑枚举首项和末项,假设首项除去一个最大的平方因子得到的结果为xxx. 那么末项一定等于xxx乘上一个平方数. 于是我们枚举首项,算出xxx然后 ...

  4. 2018.09.09 bzoj3505: [Cqoi2014]数三角形(容斥原理+简单计数)

    传送门 正难则反. 可以直接把问题转化成求出三点共线的情况数量. 如果同在一排或一列显然可以直接算,关键是如何求出斜着的. 我们知道,对于一个整点矩形. 如果长为x,宽为y,那么这个矩形任意一条对角线 ...

  5. [GO]kafka的生产者和消费者

    生产者: package main import ( "github.com/Shopify/sarama" "fmt" "time" ) ...

  6. redis只加载AOF文件

    如果同时配置写AOF和RDB两种文件,但在redis启动时,只会加载AOF,除非配置只写RDB,才会加载RDB文件,也因此AOF文件必须是全量数据,所以会越来越大,这缺点也将是redis优化的一个方向 ...

  7. HDU6029 Graph Theory 2017-05-07 19:04 40人阅读 评论(0) 收藏

    Graph Theory                                                                 Time Limit: 2000/1000 M ...

  8. HDU1349 Minimum Inversion Number 2016-09-15 13:04 75人阅读 评论(0) 收藏

    B - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  9. win7 环境安装Python + IDE(vs2010)开发

    1.下载python安装文件 python-2.7.10.msi 网址:https://www.python.org/downloads/release/python-2710/ ,根据自己环境,选择 ...

  10. Scala中()与{}

    Scala中()与{}的关系 在Scala中有些时候()和{}是可以相互替换的,但是如果不弄清楚到底什么时候该用(),什么时候该用{},什么时候二者可以相互替换,那么在写代码的时候难免为出错,并且自己 ...