【HDOJ】2782 The Worm Turns
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的更多相关文章
- 【HDOJ】3088 WORM
状态压缩+BFS. /* 3088 */ #include <iostream> #include <cstdio> #include <cstring> #inc ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- HDU 2782 The Worm Turns (DFS)
Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
随机推荐
- Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7
下载软件包 PHP: http://windows.php.net/downloads/releases/php-5.6.20-Win32-VC11-x64.zip Apache httpd: h ...
- SetUID、SetGID中的大小写Ss和Sticky bit中的大小写Tt
大写:原文件/目录没有执行(x)权限 小写:原文件/目录有执行(x)权限 例如: 原文件:-rwxr-xr-x 增加SetUID后 4755 变为:-rwsr-xr-x 再如: 原文件:-rwxr-- ...
- php 5.3起弃用session_register
最近下了dedecms V5.7时,在登陆后台时,用户名和密码也没错,就是跳转不走,进不了后台管理页面,追踪了好久才发现根目录/include/userlogin.class.php中289行左右的位 ...
- YesNo列
比较,注意两边类型是否一致,以及boolean类型tostring之后的值 if(item["IsShow"].ToString() == "True")
- (转)ASP.NET QueryString乱码解决问题
正常的情况下,现在asp.net的网站很多都直接使用UTF8来进行页面编码的,这与Javascript.缺省网站的编码是相同的,但是也有相当一部分采用GB2312. 对于GB2312的网站如果直接用j ...
- dispatchkeyevent的调用机制
http://blog.csdn.net/look85/article/details/23740761 dispatchKeyEvent和onKeyDown关系: 当键盘按下时 首先触发dispat ...
- Linux上安装KDE, Gnome和VNC
安装所需环境:需要至少256m的可用内存(128也可以不过有点卡)CentOS或类似OS(Debian的话改成apt-get应该也可以) 1,安装KDE 1 yum install kdepim 或安 ...
- NPOI的使用
简介:NPOI是POI(APATCH的一个开源项目)项目的.NET版本,最初的POI只用于JAVA来操作EXCEL or WORD等微软OLE2组件项目.使用NPOI可以完成在你没有安装Office或 ...
- Performance tuning library cache lock & single-task message
My colleague suddenly encountered a problem today,a Database becomes very slow , and the a lot of se ...
- 完全步卸载oracle11g步骤
完全 步 卸载oracle11g骤: . 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务. . 开始->程序->Oracle - OraH ...