POJ1154
2016-07-21 15:10
197 查看
LETTERS
Description
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.
Input
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.
Output
The first and only line of the output should contain the maximal number of position in the board the figure can visit.
Sample Input
Sample Output
Source
题意:
给出一个大写字母矩阵,一开始位于左上角,可以上下左右移动但不能移动到曾经经过的字母,问最多可以经过几个字母。
思路:
基础DFS,以前做这道题时,总是不理解怎么统计经过字母的个数和怎么让vis返回,现在重新做终于可以自己理解并解决了。
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int r,s,sum,cnt,dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
bool vis[30];
char map[30][30];
void dfs(int x,int y)
{
if(cnt>sum) sum=cnt;
vis[map[x][y]-'A']=1;
for(int i=0;i<4;i++)
{
int x1=x+dir[i][0];
int y1=y+dir[i][1];
if(x1>=1&&x1<=r&&y1>=1&&y1<=s&&!vis[map[x1][y1]-'A'])
{
cnt++;
dfs(x1,y1);
vis[map[x1][y1]-'A']=0;
cnt--;
}
}
}
int main()
{
while(cin>>r>>s)
{
for(int i=1;i<=r;i++)
{
for(int j=1;j<=s;j++)
cin>>map[i][j];
}
memset(vis,0,sizeof(vis));
sum=1;cnt=1;
dfs(1,1);
cout<<sum<<endl;
}
return 0;
}
LETTERS
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8119 | Accepted: 3661 |
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.
Input
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.
Output
The first and only line of the output should contain the maximal number of position in the board the figure can visit.
Sample Input
3 6 HFDFFB AJHGDH DGAGEH
Sample Output
6
Source
题意:
给出一个大写字母矩阵,一开始位于左上角,可以上下左右移动但不能移动到曾经经过的字母,问最多可以经过几个字母。
思路:
基础DFS,以前做这道题时,总是不理解怎么统计经过字母的个数和怎么让vis返回,现在重新做终于可以自己理解并解决了。
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int r,s,sum,cnt,dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
bool vis[30];
char map[30][30];
void dfs(int x,int y)
{
if(cnt>sum) sum=cnt;
vis[map[x][y]-'A']=1;
for(int i=0;i<4;i++)
{
int x1=x+dir[i][0];
int y1=y+dir[i][1];
if(x1>=1&&x1<=r&&y1>=1&&y1<=s&&!vis[map[x1][y1]-'A'])
{
cnt++;
dfs(x1,y1);
vis[map[x1][y1]-'A']=0;
cnt--;
}
}
}
int main()
{
while(cin>>r>>s)
{
for(int i=1;i<=r;i++)
{
for(int j=1;j<=s;j++)
cin>>map[i][j];
}
memset(vis,0,sizeof(vis));
sum=1;cnt=1;
dfs(1,1);
cout<<sum<<endl;
}
return 0;
}
相关文章推荐
- 训练之搜索约会2
- 不良资产处置(六):不良资产处置案例二
- [libevent]event_add()
- 几种查找数组的前K个最小值的算法
- 重建控制文件
- Android基础入门教程——4.1.3 Activity登堂入室
- <JAVA与模式>之外观模式
- 设计模式(八) 桥接模式
- 彻底解决安卓5.0软件冲突无法兼容安装失败的方法
- 网络安全-tomcat配置https
- App架构设计经验谈:接口的设计
- linux iostat 性能指标说明
- android图表框架 MPAndroidChart 学习记录
- js实现动态时间显示
- [android小知识]clearAnimation的事件
- poj3264
- cloudera-manager 5.4.10的安装
- 背景色#888888
- leetcode.374. Guess Number Higher or Lower
- 八 shader中数据传递