1154:LETTERS
- 题目链接http://bailian.openjudge.cn/practice/1154/
- 总时间限制: 1000ms 内存限制: 65536kB
- 描述
- A single-player game is played on a rectangular board divided in R rows and C columns. There is a single uppercase letter (A-Z) written in every position in the board.
Before the begging of the game there is a figure in the upper-left corner of the board (first row, first column). In every move, a player can move the figure to the one of the adjacent positions (up, down,left or right). Only constraint is that a figure cannot visit a position marked with the same letter twice.
The goal of the game is to play as many moves as possible.
Write a program that will calculate the maximal number of positions in the board the figure can visit in a single game. - 输入
- The first line of the input contains two integers R and C, separated by a single blank character, 1 <= R, S <= 20.
The following R lines contain S characters each. Each line represents one row in the board. - 输出
- The first and only line of the output should contain the maximal number of position in the board the figure can visit.
- 样例输入
-
3 6
HFDFFB
AJHGDH
DGAGEH - 样例输出
-
6
- 来源
- Croatia OI 2002 Regional Competition - Juniors
算法:深搜
代码一:
#include<iostream>
using namespace std;
int bb[]={},s,r,sum=,s1=;
char aa[][];
int dir[][]={-,,,,,-,,};
void dfs(int a,int b)
{
int a1,b1;
if(s1>sum) sum=s1; //更新最大数值
for(int i=;i<;i++)
{
a1=a+dir[i][]; //用bb数组记录访问过的字母
b1=b+dir[i][];
if(a1>=&&a1<s&&b1>=&&b1<r&&!bb[aa[a1][b1]-'A'])
{
s1++;
bb[aa[a1][b1]-'A']=; //如果在这条单线上没有记录改字母被访问过,则总数++;
dfs(a1,b1); //第一个字母总要被访问,所以不用回溯;
bb[aa[a1][b1]-'A']=; //回溯反标记
s1--; //临时记录恢复
}
}
}
int main()
{
cin>>s>>r;
for(int i=;i<s;i++)
for(int j=;j<r;j++)
cin>>aa[i][j];
bb[aa[][]-'A']=;
dfs(,);
cout<<sum<<endl;
return ;
}
代码二:
#include <stdio.h>
#include<iostream>
using namespace std;
int qq[][];
int fx[]={,,-,},fy[]={,-,,},pd[],sum,ans;//右下左上
void fun(int x,int y)
{
if(ans<sum)ans=sum;
if(qq[x][y]==) return;
for(int i=;i<;i++)
{
if(qq[x+fx[i]][y+fy[i]]!=&&pd[qq[x+fx[i]][y+fy[i]]]==)
{
sum++;
pd[qq[x+fx[i]][y+fy[i]]]=;
fun(x+fx[i],y+fy[i]);
pd[qq[x+fx[i]][y+fy[i]]]=;
sum--;
}
}
}
int main(int argc, char *argv[])
{
int r,s;
scanf("%d%d",&r,&s);
for(int i=;i<=r;i++)
for(int j=;j<=s;j++)
{
char t;
cin>>t;
qq[i][j]=t-'A'+;
}
pd[qq[][]]=;
sum=ans=;
fun(,);
printf("%d",ans);
return ;
}
1154:LETTERS的更多相关文章
- poj 1154 letters (dfs回溯)
http://poj.org/problem?id=1154 #include<iostream> using namespace std; ]={},s,r,sum=,s1=; ][]; ...
- dfs(最长路径)
http://poj.org/problem?id=1154 LETTERS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- 九度OJ 1154:Jungle Roads(丛林路径) (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:832 解决:555 题目描述: The Head Elder of the tropical island of Lagrishan has ...
- 九度 题目1154:Jungle Roads
题目描写叙述: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid mon ...
- [LeetCode] Remove Duplicate Letters 移除重复字母
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- 316. Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...
- Remove Duplicate Letters I & II
Remove Duplicate Letters I Given a string which contains only lowercase letters, remove duplicate le ...
- [CareerCup] 18.13 Largest Rectangle of Letters
18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...
- LeetCode Remove Duplicate Letters
原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains on ...
随机推荐
- 使用 PySide2 开发 Maya 插件系列二:继承 uic 转换出来的 py 文件中的类 Ui_Form
使用 PySide2 开发 Maya 插件系列二:继承 uic 转换出来的 py 文件中的类 Ui_Form 开发环境: Wing IDE 6.1 步骤1: 打开 Wing IDE,创建一个新的 pr ...
- scrapy选择器归纳
python 爬虫: srcrapy框架xpath和css选择器语法 Xpath基本语法 一.常用的路径表达式: 表达式 描述 实例 nodename 选取nodename节点的所有子节点 //div ...
- Django分页(二)
Django分页(二) 要求 .设定每页显示数据条数 # # .用户输入页码(第一页.第二页...) # # .设定显示多少页号 # # .获取当前数据总条数 # # .根据设定显示多少页号和数据总条 ...
- docker部署springboot项目
本文介绍一下docker如何部署springboot项目. 前提条件: 1.可以运行jar包的环境 2.机器上已经安装了docker 3.准备部署的springboot的jar包 4.Dockerfi ...
- 熔断器---Hystrix
Hystrix:熔断器,容错管理工具,旨在通过熔断机制控制服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力. 说到熔断器,先要引入另外一个词,雪崩效应. 雪崩效应,百度百科的解释是这样的: ...
- XamarinAndroid组件教程RecylerView适配器使用动画
XamarinAndroid组件教程RecylerView适配器使用动画 为RecylerView使用RecylerViewAnimators组件中提供的适配器动画,需要使用RecyclerView类 ...
- Java并发编程:volatile关键字解析zz
volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...
- Bluestein's Algorithm
网上很少有人提到,写的也很简单,事实上就是很简单... \(Bluestein's\ Algorithm\),用以解决任意长度\(DFT\). 考虑\(DFT\)的形式:\[\begin{aligne ...
- [P1396]营救 (并查集)
大佬都是用最短路做的 我用最小生成树 #include<bits/stdc++.h> #include<algorithm> using namespace std; stru ...
- bzoj3255 一个关于序列的游戏
题意是啥 给你一个数列,可以任意删去一段,记其长度为$s$,得到$val_s$的价值,问你最大价值和为多少.. 其中这一段数要满足成一个上凸且相邻数差为$1$ 显然,删掉一段数后剩下的左右会相邻.. ...