bfs预处理一点到边界的最小距离,IDA*求出可行方案。注意按字典序初始化dir数组。并且存在中间点全为1,边界含0的可能性(wa了很多次)。此时不输出任何命令。

 /* 1813 */
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; #define MAXN 80
#define INF 0xffffff int n, m;
char ops[][] = {
"east",
"north",
"south",
"west"
};
int op[];
int x[MAXN], y[MAXN];
int dis[][];
int visit[][];
char map[][];
int dir[][] = {
,,-,,,,,-
}; inline bool isEnd(int x, int y) {
return x== || x==n- || y== || y==n-;
} inline bool check(int x, int y) {
return x< || x>=n || y< || y>=n;
} int bfs(int x, int y) {
int xx, yy;
int i, j, k;
queue<int> Q; k = *x + y;
Q.push(k);
memset(visit, -, sizeof(visit));
visit[x][y] = ; while (!Q.empty()) {
k = Q.front();
Q.pop();
x = k/;
y = k%;
for (i=; i<; ++i) {
xx = x + dir[i][];
yy = y + dir[i][];
if (map[xx][yy]=='' || visit[xx][yy]>=)
continue;
visit[xx][yy] = visit[x][y]+;
if (isEnd(xx, yy))
return visit[xx][yy];
k = *xx + yy;
Q.push(k);
}
}
return ;
} bool dfs(int d, int *px, int *py) {
int mmax = -;
int x[MAXN], y[MAXN];
int i, j, k; for (i=; i<m; ++i)
mmax = max(mmax, dis[px[i]][py[i]]);
if (mmax > d)
return false;
if (d == )
return true; for (i=; i<; ++i) {
op[d] = i;
for (j=; j<m; ++j) {
x[j] = px[j] + dir[i][];
y[j] = py[j] + dir[i][];
if (isEnd(px[j], py[j]) || map[x[j]][y[j]]=='') {
x[j] = px[j];
y[j] = py[j];
}
}
if (dfs(d-, x, y))
return true;
}
return false;
} int main() {
int t = ;
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d",&n) != EOF) {
m = ;
for (i=; i<n; ++i)
scanf("%s", map[i]);
for (i=; i<n; ++i) {
for (j=; j<n; ++j) {
if (map[i][j] == '') {
dis[i][j] = INF;
} else {
if (isEnd(i, j)) {
dis[i][j] = ;
} else {
dis[i][j] = bfs(i, j);
x[m] = i;
y[m] = j;
++m;
}
}
}
}
if (t++)
printf("\n");
if (m) {
for (i=; ; ++i) {
if (dfs(i, x, y))
break;
}
for (j=i; j>; --j)
printf("%s\n", ops[op[j]]);
}
} return ;
}

【HDOJ】1813 Escape from Tetris的更多相关文章

  1. 【HDOJ】1811 Rank of Tetris

    并查集+拓扑排序.使用并查集解决a = b的情况. #include <iostream> #include <cstdio> #include <cstring> ...

  2. 【HDOJ】1760 A New Tetris Game

    博弈,主要是求SG值.终于做出点儿感觉. /* 1760 */ #include <cstdio> #include <cstring> #include <cstdli ...

  3. 【HDOJ】2364 Escape

    bfs.题目做的不细心,好多小错误.尤其注意起始点就是边界的情况.wa了八次. #include <iostream> #include <cstdio> #include & ...

  4. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  5. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  6. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  7. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  8. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  9. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

随机推荐

  1. Qt 学习之路 :Repeater

    前面的章节我们介绍过模型视图.这是一种数据和显示相分离的技术,在 Qt 中有着非常重要的地位.在 QtQuick 中,数据和显示的分离同样也是利用这种“模型-视图”技术实现的.对于每一个视图,数据元素 ...

  2. Java读取WEB-INF目录下的properties配置文件

    如何在Java代码中读取WEB-INF目录下的properties配置文件,下文给出了一个解决方案. 我们习惯将一些配置信息写在配置文件中,比如将数据库的配置信息URL.User和Password写在 ...

  3. jQuery Capty 图片标题插件

    jQuery Capty是可以为图片添加漂亮的字幕的插件 文件包含: <link type="text/css" rel="stylesheet" hre ...

  4. CentOS 6.4 编译 Hadoop 2.5.1

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/4058956.html ...

  5. HDU 4288 Coder(线段树)

    题意: 给定三种操作 1. add x 向序列中添加x,添加之后序列还保持有序 2. del x  删除序列中值为x的元素 3. sum  求下边模5等于3的元素和 思路: 直接暴力也可以过,就是看暴 ...

  6. rabbitmq pika connection closed

    You are here: Home / rabbitmq pika connection closed rabbitmq pika connection closed By lijiejie on  ...

  7. Jquery 判断滚动条到达顶部或底部

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

  8. HBase0.98.1 通过协调器进行表的行数统计

    1. 启用表aggregation,只对特定的表生效.通过HBase Shell 来实现 (1)disable指定表.hbase> disable 'student' (2)添加aggregat ...

  9. HTTP请求返回的NSData无法转换为NSString

    最近在做的一个项目中有一个功能是有一个网页,模拟http请求获取到这个网页返回的相应的数据. 在请求完成后获取到的数据为NSData类型,按照我们通常的转换为NSString的方法: NSString ...

  10. VC++读取资源中文件

    //查找目标资源 HRSRC hResource = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_MAINPROG), TEXT(& ...