Wang Xifeng's Little Plot - HDU 5024 搜索

寻找最长路径算法

Wang Xifeng's Little Plot

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 262    Accepted Submission(s): 170


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
 

题意:找到一条最多只有一个90度拐角的路,使得路径最长。

思路:先搜索找到一个直线的最长距离,然后再考虑拐弯。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[110][110];
int ox[9]={0,-1,-1,0,1,1,1,0,-1},oy[9]={0,0,1,1,1,0,-1,-1,-1},ans,dir[20];
int t,rem[110][110][9],vis[110][110][9];
void dfs(int xx,int yy,int x,int y,int k,int l)
{
    if(vis[x][y][k]==t && rem[x][y][k]>=2)
    {
        int ret=rem[x][y][k]-1;
        dfs(xx,yy,x+ox[k]*ret,y+oy[k]*ret,k,l+ret);
    }
    else if(s[x+ox[k]][y+oy[k]]!='#')
      dfs(xx,yy,x+ox[k],y+oy[k],k,l+1);

        if(vis[xx][yy][k]!=t)
        {
            vis[xx][yy][k]=t;
            rem[xx][yy][k]=l;
        }
        else
          rem[xx][yy][k]=max(rem[xx][yy][k],l);
}
int main()
{
    int n,m,i,j,k,x,y,ret;
    dir[0]=8;
    for(i=1;i<=8;i++)
    {
        dir[i]=i;
        dir[i+8]=i;
    }
    while(~scanf("%d",&n) && n)
    {
        t++;
        for(i=1;i<=n;i++)
           scanf("%s",s[i]+1);
        for(i=0;i<=n+1;i++)
        {
            s[0][i]='#';
            s[n+1][i]='#';
            s[i][0]='#';
            s[i][n+1]='#';
        }

        ans=2;
        for(i=1;i<=n;i++)
           for(j=1;j<=n;j++)
              if(s[i][j]=='.')
                for(k=1;k<=8;k++)
                   dfs(i,j,i,j,k,1);
        for(i=1;i<=n;i++)
           for(j=1;j<=n;j++)
              if(s[i][j]=='.')
                for(k=1;k<=8;k++)
                {
                    ret=rem[i][j][k]-1;
                    if(ret==0)
                      continue;
                    else
                    {
                        x=i+ox[k]*ret;
                        y=j+oy[k]*ret;
                        ans=max(ans,ret+rem[x][y][dir[(k+2)%8]]);
                        ans=max(ans,ret+rem[x][y][dir[(k+6)%8]]);
                    }
                }
        printf("%d\n",ans);
    }
}





### DS18B20温度传感器驱动程序及接口文件获取 对于DS18B20温度传感器而言,其独特的64位ID使得多个设备可以连接在同一根总线上,并允许通过ROM搜索来识别和读取各个传感器的数据[^1]。 针对不同平台上的开发需求,通常可以从以下几个途径获得适用于DS18B20的驱动程序或库: - **官方资源**:Maxim Integrated作为制造商提供了详细的文档和支持材料。官方网站上不仅有完整的数据手册,还可能提供了一些基础的例子代码用于帮助开发者快速启动项目。 - **开源社区贡献**:GitHub等平台上有很多由爱好者们分享出来的现成实现方案。这些第三方提供的库往往已经被广泛测试过,在很多情况下可以直接拿来使用或者稍作修改就能满足特定应用场景的需求。 具体到编程环境的选择方面,如果是基于Arduino这样的微控制器进行开发,则可以通过Arduino IDE中的库管理器轻松安装`OneWire`以及专门处理DS18B20通信协议的库如`DallasTemperature`[^2]。 而对于嵌入式Linux系统或者其他更复杂的操作系统来说,可能会涉及到编写底层硬件抽象层(HAL),此时除了查阅上述提到的相关技术文档外,还可以参考其他相似案例的学习笔记或是论坛讨论帖来进行辅助理解与实践[^3]。 ```cpp // Arduino环境下简单的DS18B20初始化示例 #include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 2 // 定义DQ脚接在哪个GPIO引脚上 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup(void){ Serial.begin(9600); sensors.begin(); } void loop(void){ float temperature; sensors.requestTemperatures(); temperature=sensors.getTempCByIndex(0); Serial.print("Current Temperature : "); Serial.println(temperature); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值