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 ...
随机推荐
- day 39 mycql 数据库之约束
egon笔记: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 UNIQUE KEY (UK) 标识该字段的值是唯一的 AUTO_INCREMENT 标识该字段的值自动增 ...
- 3dsmax不同版本 pyside qt UI 设置max窗口为父窗口的方法
3dsmax不同版本 pyside qt widget 设置 max 窗口为父窗口的方法 前言: 3dsmax 在 2014 extension 之后开始集成 Python 和 PySide,但是在版 ...
- FastAdmin 基本知识流程一栏
fastadmin进阶 安装:出现登陆页无法显示:可能是php的gd2扩展未开启 FastAdmin 在 Nginx 中的配置 用的是AdminLTE后台模板require.js.less.Bow ...
- 在Adobe Html5 Extension的使用Nodejs的问题
前情回顾 之前为一个客户开发过一个基于Adobe Premiere的Html5扩展.原本是在Adobe Premiere Pro 2015下面进行调试开发的.一切进展的非常顺利,功能也都正常.但是20 ...
- Adobe Premiere Pro生成峰值文件假死
一.正文 使用Adobe的Premiere Pro CC进行视频剪辑制作的时候,有的时候在右下角总会出现一个“自动生成峰值文件”的提示符,并跟随一个进度条: 大部分时候,这并不会引起什么问题.虽然我也 ...
- 在Java中,关于.Class()与.getCalss与.getClass().getName()的区别
** * Java反射 *所谓反射,可以理解为在运行时期获取对象类型信息的操作. *传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下, *编程人员可以动态获取这些信息,从而编写更加 ...
- UVA 508 Morse Mismatches JAVA
题意:输入字母和数字的编码,输入词典,输入一段编码,求出对应的单词. 思路:来自https://blog.csdn.net/qq_41163933/article/details/82224703 i ...
- UVA 232 Corssword Answer
题意:输入m*n大小的字符串(里面有*,*为黑格,其他为白格),然后对它编号,编号规则为从左到右,从上往下,且左边或上面没有白格(可能是黑格或越界),如下图: 注意: ①除第一次输出答案外,其余每次输 ...
- POJ2386----Lake Counting
/* 利用宽搜将每块积水填满,添加一个计数器,记下填满几块积水即答案 */ #include<iostream> using namespace std; ][]; ][] = {{-,- ...
- [mariadb]Windows Mariadb 10.2安装过程
在学习Flask的过程中,碰到SQLAlchemy不支持Mariadb 10.2.9以前版本的问题,于是升级Mariadb到10.2.10. 升级过程中,我只能说,Mariadb及Mysql的文档结构 ...