当前位置:网站首页>信息学奥赛一本通 1212:LETTERS | OpenJudge 2.5 156:LETTERS
信息学奥赛一本通 1212:LETTERS | OpenJudge 2.5 156:LETTERS
2022-04-23 04:49:00 【君义_noip】
【题目链接】
ybt 1212:LETTERS
OpenJudge 2.5 156:LETTERS
【题目考点】
1. 深搜
设bool类型vis数组,长度为128。vis[i]为真表示ascii码为i的字母已经访问过。
设变量step记录已经访问过的字母数量,mx记录访问过的字母数量的最大值。
每遍历到一个位置,搜索其上下左右四个位置,如果这个位置在地图内,且这个位置的字符没有被访问过,则更改字母的访问状态,包括vis数组及step变量(在此处更新step最大值mx),接着搜索这一新的位置。在搜索后,记得状态还原。
深搜的具体写法上,有在函数调用前访问,以及在函数调用内访问两种写法,区别仅仅在于作为参数传入的位置在函数内部是否处于已访问的状态。
【题解代码】
写法1:在函数调用前访问
#include <bits/stdc++.h>
using namespace std;
#define N 25
int r, s, step, mx;
char mp[N][N];
bool vis[128];
int dir[4][2] = {
{
0,1},{
0,-1},{
-1,0},{
1,0}};
void dfs(int sx, int sy)//在函数调用前访问
{
for(int i = 0; i < 4; ++i)
{
int x = sx + dir[i][0], y = sy + dir[i][1];
if(x >= 1 && x <= r && y >= 1 && y <= s && vis[mp[x][y]] == false)
{
vis[mp[x][y]] = true;//更新状态
step++;
mx = max(step, mx);
dfs(x, y);
step--;//状态还原
vis[mp[x][y]] = false;
}
}
}
int main()
{
cin >> r >> s;
for(int i = 1; i <= r; ++i)
for(int j = 1; j <= s; ++j)
cin >> mp[i][j];
vis[mp[1][1]] = true;//访问初始位置
step = 1;
mx = 1;
dfs(1,1);
cout << mx;
return 0;
}
写法2:在函数调用内访问
#include <bits/stdc++.h>
using namespace std;
#define N 25
int r, s, step, mx;
char mp[N][N];//地图
bool vis[128];//vis[i]:ascii码为i的字母是否已经被访问过
int dir[4][2] = {
{
0,1},{
0,-1},{
-1,0},{
1,0}};//方向数组
void dfs(int sx, int sy)//在函数调用内访问
{
if(sx >= 1 && sx <= r && sy >= 1 && sy <= s && vis[mp[sx][sy]] == false)//如果(sx,sy)在地图内且该位置的字母没有访问过
{
vis[mp[sx][sy]] = true;//访问该字母
step++;//移动步数加1
mx = max(step, mx);//更新最大值
for(int i = 0; i < 4; ++i)//搜索上下左右四个位置
{
int x = sx + dir[i][0], y = sy + dir[i][1];
dfs(x, y);
}
step--;//状态还原
vis[mp[sx][sy]] = false;
}
}
int main()
{
cin >> r >> s;
for(int i = 1; i <= r; ++i)
for(int j = 1; j <= s; ++j)
cin >> mp[i][j];
dfs(1,1);
cout << mx;
return 0;
}
版权声明
本文为[君义_noip]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lq1990717/article/details/124358517
边栏推荐
- PHP 统计指定文件夹下文件的数量
- Practice and exploration of knowledge map visualization technology in meituan
- Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
- Eight misunderstandings that should be avoided in data visualization
- Innovation training (IX) integration
- Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]
- 泰克示波器DPO3054自校准SPC失败维修
- Innovation training (V) mid term inspection
- AWS eks add cluster user or Iam role
- Innovation training (IV) preliminary preparation - server
猜你喜欢

Painless upgrade of pixel series
![[paper reading] [3D target detection] point transformer](/img/c5/b1fe5f206b5fe6e4dcd88dce11592d.png)
[paper reading] [3D target detection] point transformer

Pixel mobile phone brick rescue tutorial

Unity RawImage背景无缝连接移动

程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说

Repair of self calibration SPC failure of Tektronix oscilloscope dpo3054

使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘

Record the ThreadPoolExecutor main thread waiting for sub threads

Learning Android II from scratch - activity

泰克示波器DPO3054自校准SPC失败维修
随机推荐
【数据库】MySQL单表查询
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
C list field sorting contains numbers and characters
Key points of AWS eks deployment and differences between console and eksctl creation
C language: Advanced pointer
MySQL time function query
Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
Small volume Schottky diode compatible with nsr20f30nxt5g
Basic operation of sequence table
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
Innovation training (VI) routing
Unity摄像头跟随鼠标旋转
JS generates a specified number of characters according to some words
Alibaba tip: it is better to create threads manually
QML advanced (IV) - drawing custom controls
QML advanced (V) - realize all kinds of cool special effects through particle simulation system
General enumeration constant class
Druid -- JDBC tool class case
Innovation training (II) task division
redis和mysql区别