题目链接:Codeforces 382D Ksenia and Pawns
题目大意:给出一张图,然后任意选取两个位置摆放卒,这两个卒按照图上的方向移动,问说总步数的最大值是多少,两个卒不可以同时在一个位置上。
解题思路:逆向dfs,以为卒一旦放在图上,他的路线就已经被确定,所以只要枚举所有的‘#’,然后逆向的dfs出路线维护最大值和第二大值。如果最大值大于第二大值,那么答案就是2*n-1(只要让一个卒跟着另一个就肯定比其他方案要优);但是如果不相同的话,就要判断说两个值的路线是否重叠,我是直接在dfs的过程中完成的,保证同一条路线只取一个最大值,但是要注意的是‘#’是不算的,就像样例4中的一样。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 2005;
const int d[4][2] = { {0, -1}, {0, 1}, {1, 0}, {-1, 0} };
const char sign[10] = "><^v";
int r, c, a, b, cnt, n, m;
char g[N][N];
void Max (int k) {
m = max(k, m);
if (m > n) swap(n, m);
}
void init () {
cnt = 0;
scanf("%d%d%*c", &r, &c);
for (int i = 0; i < r; i++) gets(g[i]);
}
int dfs(int x, int y) {
int ans = 0;
cnt++;
for (int i = 0; i < 4; i++) {
int p = x + d[i][0], q = y + d[i][1];
if (p < 0 || p >= r || q < 0 || q >= c) continue;
if (g[p][q] != sign[i]) continue;
if (g[x][y] == '#') Max(dfs(p, q));
else ans = max(ans, dfs(p, q));
}
return ans + 1;
}
int solve () {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) if (g[i][j] == '#') {
dfs(i, j);
}
}
if (cnt < r * c) return -1;
return n == m ? 2 * n : 2 * n - 1;
}
int main () {
init();
printf("%d\n", solve ());
return 0;
}
分享到:
相关推荐
Codeforces 1925D Good Trip 题解
暴枚最长桌脚的长度$l$,然后长度比$l$长的桌脚全部都要砍掉长度比$l$短的桌脚选择代价前$k$小的砍掉用线段树维护;示例程序 :typedef long l
Codeforces 题库 101-200 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
codeforces每日一题。 题意: 给出一个数组,让你挑选出能组成任意pair差值为2的幂的序列,并且使这个序列长度尽可能大。 思路: 针对于挑选出来的序列,任意pair对的差值为2的幂数。 假设有多个pair对,设dis(a,b)=2...
Codeforces 题库 001-100 共~500题 codeforces.com版权所有。 程序可提交至该网站评测。
codeforces编程网站预测分数插件
Educational Codeforces Round 157D. XOR Construction
【并查集】Codeforces 566D Restructuring Company题面在这里对于本题,只需要再维护一个并查集表示i所在联通块的最右位置因为相邻
Codeforces 1105B - Zuhair and Strings 测试点37个(全)
使用于Google Chrome的Codeforces Enhancer 1.1.2插件安装包。 版本:codeforces enhancer 1.1.2 使用浏览器:Google Chrome
Codeforces 149 D-Coloring Brackets,动态规划求解
题意: 给出 nnn 个点,n−1n-1n−1 条边,最多询问 n2\frac{n}{2}2n 次,每次询问 u,vu,vu,v,会给出 uvuvuv的最近公共祖先,求树的根。 ...操作就是一个删除叶子节点的过程。 AC代码: const int N = 1010;...
Codeforces 185A - Plant 全测试点49个
codeforces 19 E Fairy 一道比较难的题目的解题报告 推荐阅读
题目大意:给出一个由 n 个点组成的树,现在可以询问 n/2 次(向下取整)LCA,确定根节点是哪个节点 题目分析:因为最多只能求 n/2 次lca,每次需要两个节点作为参数,也就是说每个点我们至多遍历一遍,读完题后没什么...
Codeforces global round 10 codes
Codeforces round 678 division 2 codes
Some of the Codeforces problems codes
Codeforces round 678 D2_Codeforces_源码
一个Codeforces、牛客竞赛、AtCoder平台的编程竞赛查询插件,ACMer必备.zip