码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5024 Wang Xifeng's Little Plot(广州网络赛C题)

时间:2014-09-22 18:56:23      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   for   sp   on   

HDU 5024 Wang Xifeng‘s Little Plot

题目链接

思路:先利用记忆化搜索预处理出每个结点对应8个方向最远能走多远,然后枚举拐点记录最大值即可

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int d[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};

const int N = 105;
int n, sum[N][N][10];
char g[N][N];

int dfs(int x, int y, int dd) {
	if (x <= 0 || x > n || y <= 0 || y > n) return 0;
	if (sum[x][y][dd] != -1) return sum[x][y][dd];
	if (g[x][y] == '#') return sum[x][y][dd] = 0;
	sum[x][y][dd] = dfs(x + d[dd][0], y + d[dd][1], dd) + 1;
	return sum[x][y][dd];
}

int main() {
	while (~scanf("%d", &n) && n) {
		memset(sum, -1, sizeof(sum));
		for (int i = 1; i <= n; i++)
			scanf("%s", g[i] + 1);
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (g[i][j] == '.') {
					for (int k = 0; k < 8; k++)
						dfs(i, j, k);
				}
			}
		}
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				for (int k = 0; k < 4; k++) {
					if (g[i][j] == '#') continue;
					ans = max(ans, sum[i][j][k] + sum[i][j][(k + 1) % 4] - 1);
					ans = max(ans, sum[i][j][k + 4] + sum[i][j][(k + 1) % 4 + 4] - 1);
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}


HDU 5024 Wang Xifeng's Little Plot(广州网络赛C题)

标签:style   http   color   io   os   ar   for   sp   on   

原文地址:http://blog.csdn.net/accelerator_/article/details/39477593

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!