DFS。

 /* 2782 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; #define MAXN 650 int n, m;
bool visit[MAXN][MAXN];
int ansd, ansx, ansy, ansb;
int dir[][] = {
// E, N, S, W.
,, -,, ,, ,-
};
int link[][] = {
,, ,, ,, ,
};
char op[] = "ENSW"; inline bool check(int x, int y) {
return x< || x>=n || y< || y>=m;
} int dfs(int x, int y, int d) {
int i, j, k, tmp;
int xx, yy;
int ret = ; if (d == -) {
for (i=; i<; ++i) {
xx = x + dir[i][];
yy = y + dir[i][];
if (check(xx, yy) || visit[xx][yy])
continue;
k = ;
while (!check(xx, yy) && !visit[xx][yy]) {
++k;
visit[xx][yy] = true;
xx += dir[i][];
yy += dir[i][];
}
xx -= dir[i][];
yy -= dir[i][];
tmp = k + dfs(xx, yy, i);
if (tmp > ansb) {
ansb = tmp;
ansx = x;
ansy = y;
ansd = i;
}
while (k--) {
visit[xx][yy] = false;
xx -= dir[i][];
yy -= dir[i][];
}
}
} else {
for (j=; j<; ++j) {
i = link[d][j];
xx = x + dir[i][];
yy = y + dir[i][];
if (check(xx, yy) || visit[xx][yy])
continue;
k = ;
while (!check(xx, yy) && !visit[xx][yy]) {
++k;
visit[xx][yy] = true;
xx += dir[i][];
yy += dir[i][];
}
xx -= dir[i][];
yy -= dir[i][];
tmp = k + dfs(xx, yy, i);
if (tmp > ret)
ret = tmp;
while (k--) {
visit[xx][yy] = false;
xx -= dir[i][];
yy -= dir[i][];
}
}
}
return ret;
} int main() {
int t = ;
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d %d",&n,&m)!=EOF && (n||m)) {
memset(visit, false, sizeof(visit));
scanf("%d", &k);
while (k--) {
scanf("%d %d", &i, &j);
visit[i][j] = true;
}
ansb = -;
for (i=; i<n; ++i) {
for (j=; j<m; ++j) {
if (!visit[i][j]) {
visit[i][j] = true;
dfs(i, j, -);
visit[i][j] = false;
}
}
}
printf("Case %d: %d %d %d %c\n", ++t, ansb+, ansx, ansy, op[ansd]);
} return ;
}

【HDOJ】2782 The Worm Turns的更多相关文章

  1. 【HDOJ】3088 WORM

    状态压缩+BFS. /* 3088 */ #include <iostream> #include <cstdio> #include <cstring> #inc ...

  2. 【HDOJ】4729 An Easy Problem for Elfness

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

  3. HDU 2782 The Worm Turns (DFS)

    Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...

  4. 【HDOJ】【3506】Monkey Party

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

  5. 【HDOJ】【3516】Tree Construction

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

  6. 【HDOJ】【3480】Division

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

  7. 【HDOJ】【2829】Lawrence

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

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

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

  9. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

随机推荐

  1. android本地定时通知

    android本地通知略有不同,分为立即触发和延时触发 1.即时通知 android默认的Notification为立即触发 Intent intent = new Intent(Intent.ACT ...

  2. SQL语句中格式化时间

    给数据库中的字段格式化(): to_char(CREATETIME,'yyyy-MM-dd') 给程序中的字段格式化(InTime为数据库字段): InTime = to_date( '" ...

  3. VS2010调试多进程--医疗His调试中使用

    有些时候碰到自己开发的程序嵌入到别人的框架中,而在接口的地方出了问题,而又不方便将自己的模快加入到别人的工程中这个时候VS的附加到进程调试变得不可或缺了,直奔主题 http://msdn.micros ...

  4. Liferay环境搭建实录

    1. 安装Liferay IDE:打开Eclipse的Help-->Eclipse Marketplace,在下图所示搜索框中输入liferay,回车搜索: 搜索结果如下图所示: 点击insta ...

  5. 爆牙齿的 Web 标准面试题 【转藏】

    <!DOCTYPE html> <html lang="zh-CN"><head> <meta http-equiv="cont ...

  6. MORE ABORT AWR?

    For some time, Oracle’s solution in this area has been its built-in tool, Statspack.Oracle Database ...

  7. Java Map集合按照key和value排序之法

    一.理论基点 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black-Tre ...

  8. CSDN Oracle版精华帖荟萃

    ⑴ 关于大数据量的数据库设计问题http://bbs.csdn.net/topics/390382930⑵ ORA-00904标识符无效http://bbs.csdn.net/topics/39033 ...

  9. ios 设置label的高度随着内容的变化而变化

    好吧 步骤1:创建label _GeRenJianJie = [[UILabel alloc]init]; 步骤2:设置label _GeRenJianJie.textColor = RGBAColo ...

  10. [lua]尝试一种Case语法糖

    function CaseT(arg) function proxy(caller) caller.yield(r) end -- proxy return function (cond) if (c ...