[ACM] HDU 5024 Wang Xifeng's Little Plot (构造,枚举)

2024-01-28 13:18

本文主要是介绍[ACM] HDU 5024 Wang Xifeng's Little Plot (构造,枚举),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Wang Xifeng's Little Plot




Problem Description
《Dream of the Red Chamber》(also 《The Story of the Stone》) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin. But the last 40 chapters of the original version is missing, and that part of current version was written by Gao E. There is a heart breaking story saying that after Cao Xueqin died, Cao's wife burned the last 40 chapter manuscript for heating because she was desperately poor. This story was proved a rumor a couple of days ago because someone found several pages of the original last 40 chapters written by Cao. 

In the novel, Wang Xifeng was in charge of Da Guan Yuan, where people of Jia family lived. It was mentioned in the newly recovered pages that Wang Xifeng used to arrange rooms for Jia Baoyu, Lin Daiyu, Xue Baochai and other teenagers. Because Jia Baoyu was the most important inheritor of Jia family, and Xue Baochai was beautiful and very capable , Wang Xifeng didn't want Jia Baoyu to marry Xue Baochai, in case that Xue Baochai might take her place. So, Wang Xifeng wanted Baoyu's room and Baochai's room to be located at two ends of a road, and this road should be as long as possible. But Baoyu was very bad at directions, and he demanded that there could be at most one turn along the road from his room to Baochai's room, and if there was a turn, that turn must be ninety degree. There is a map of Da Guan Yuan in the novel, and redists (In China English, one whose job is studying 《Dream of the Red Chamber》is call a "redist") are always arguing about the location of Baoyu's room and Baochai's room. Now you can solve this big problem and then become a great redist.

Input
The map of Da Guan Yuan is represented by a matrix of characters '.' and '#'. A '.' stands for a part of road, and a '#' stands for other things which one cannot step onto. When standing on a '.', one can go to adjacent '.'s through 8 directions: north, north-west, west, south-west, south, south-east,east and north-east.

There are several test cases.

For each case, the first line is an integer N(0<N<=100) ,meaning the map is a N × N matrix.

Then the N × N matrix follows.

The input ends with N = 0.

Output
For each test case, print the maximum length of the road which Wang Xifeng could find to locate Baoyu and Baochai's rooms. A road's length is the number of '.'s it includes. It's guaranteed that for any test case, the maximum length is at least 2.

Sample Input
  
3 #.# ##. ..# 3 ... ##. ..# 3 ... ### ..# 3 ... ##. ... 0

Sample Output
  
3 4 3 5

Source
2014 ACM/ICPC Asia Regional Guangzhou Online


解题思路:

N * N的矩阵, 走的方向为8个方向,其中' . '表示可走,下面用点表示,' #' 表示不可走,找出一条最长的路径包含几个点,输出点的个数,其中路径的要求是 最多包含一个直角(拐一个弯)。

int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}}   定义方向, 按照 上,左上,右,右下,下,左下,左,左上 的顺序定义,编号为0,1,2,3,4,5,6,7

那么构成直角的两个方向有7对,分别是  0 2,  1 3 , 2  4, 3 5 ,4 6, 5 7 ,6 0 ,7 1

那么枚举每个可走的点,求出该点向8个方向分别走的最远距离,然后按照上面的配对,找出一条最长的合法路径。

枚举完所有可走的点,也就得出答案了。

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;char mp[102][102];
int n;
int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
int ds[8];//一个点每个方向最长可以走多远
int all[8];//每个点为直角的拐点两边最长走多远int main()
{while(scanf("%d",&n)!=EOF){if(n==0)break;getchar();for(int i=0;i<n;i++){for(int j=0;j<n;j++)scanf("%c",&mp[i][j]);getchar();}int ans=-1;for(int i=0;i<n;i++)for(int j=0;j<n;j++){if(mp[i][j]=='.'){memset(all,0,sizeof(all));memset(ds,0,sizeof(ds));for(int k=0;k<8;k++){int x=i,y=j;while(x>=0&&x<n&&y>=0&&y<n&&mp[x][y]=='.'){ds[k]++;//每个方向上最远走多少x+=dir[k][0];y+=dir[k][1];}}for(int i=0;i<8;i++){all[i]=ds[i]+ds[(i+2)%8];//构成直角的两个方向ans=max(ans,all[i]);//cout<<"ans"<<ans<<endl;}}}printf("%d\n",ans-1);}return 0;
}



这篇关于[ACM] HDU 5024 Wang Xifeng's Little Plot (构造,枚举)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/653735

相关文章

C#实现获得某个枚举的所有名称

《C#实现获得某个枚举的所有名称》这篇文章主要为大家详细介绍了C#如何实现获得某个枚举的所有名称,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... C#中获得某个枚举的所有名称using System;using System.Collections.Generic;usi

Java 枚举的常用技巧汇总

《Java枚举的常用技巧汇总》在Java中,枚举类型是一种特殊的数据类型,允许定义一组固定的常量,默认情况下,toString方法返回枚举常量的名称,本文提供了一个完整的代码示例,展示了如何在Jav... 目录一、枚举的基本概念1. 什么是枚举?2. 基本枚举示例3. 枚举的优势二、枚举的高级用法1. 枚举

Rust中的Option枚举快速入门教程

《Rust中的Option枚举快速入门教程》Rust中的Option枚举用于表示可能不存在的值,提供了多种方法来处理这些值,避免了空指针异常,文章介绍了Option的定义、常见方法、使用场景以及注意事... 目录引言Option介绍Option的常见方法Option使用场景场景一:函数返回可能不存在的值场景

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while