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 ...
随机推荐
- Linux git 在自己的服务器上建立 git 仓库(repository)
Linux git 在自己的服务器上建立 git 仓库(repository) 服务器端: 在这里使用 ssh 方式登陆: ssh [username]@server_address(建议用超级用户登 ...
- 用Eclipse上传项目到github
1.安装EGit插件 点击菜单栏help->Eclipse Marketplace 2.配置Git 这里是配置相关账户信息 3.把项目提交到本地仓库 右键项目->Team->Shar ...
- this 相关(2)
this 的指向与所在方法的调用位置有关,而与方法的声明位置无关 var obj = { val: 1, getVal: function() { console.log(this.val); } } ...
- shiro学习总结(一)----初识shiro
本系列内容大多总结自官网和张开涛的<跟我学Shiro> 一.shiro简介 1.1.shiro有什么用? shiro是一个功能强大使用简单的java安全框架,主要提供了五大功能: 1.认证 ...
- Alpha(7/10)
鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...
- CCF-炉石传说
#include<cstdio> typedef struct { int attack,health; } cus; cus info[][]; ]; ; void summon(); ...
- 因数表进阶:1--x的因数和
紧接着上一个文章,进阶一个因数表,来自牛客网一道比赛题: 打从1到n所有因数的和 代码如下: #include<cstdio> #define ll long long using nam ...
- 51nod 算法马拉松30
题目链接 附一个代码地址 A,这个容斥一下就好了 C,rxd大爷给讲的,首先如果分三种情况(成环,正在比配环,未访问)讨论复杂度是\(3^n * n ^ 2\)的,但是对于每一个环,都可以直接枚举环的 ...
- 2545 ACM 博客 比较树的路径长短
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2545 题意:比较树的路径长短 思路:利用数组存入父节点的值, 例如: 5 2 1 2 1 3 3 4 3 ...
- php数组和对象转换函数
/** * 数组 转 对象 * * @param array $arr 数组 * @return object */ function array_to_object($arr) { ...