当前位置:网站首页>信息学奥赛一本通 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
边栏推荐
- Alibaba tip: it is better to create threads manually
- Record the blind injection script
- Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
- Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
- Manually write smart pointer shared_ PTR function
- La caméra Unity tourne avec la souris
- leetcode006--查找字符串数组中的最长公共前缀
- What is a data island? Why is there still a data island in 2022?
- Code007 -- determine whether the string in parentheses matches
- Improving 3D object detection with channel wise transformer
猜你喜欢

Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)

Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘

MySQL -- execution process and principle of a statement

【数据库】MySQL多表查询(一)

Learning Android from scratch -- Introduction

泰克示波器DPO3054自校准SPC失败维修

Unity rawimage background seamlessly connected mobile
![Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]](/img/99/095063b72390adea6250f7b760d78c.png)
Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]

Introduction to raspberry pie 3B - system installation

Spark FAQ sorting - must see before interview
随机推荐
leetcode003--判断一个整数是否为回文数
Flink's important basics
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
Recommended scheme for national production of electronic components for wireless charging
Innovative practice of short video content understanding and generation technology in meituan
leetcode009--用二分查找在数组中搜索目标值
New terminal play method: script guidance independent of technology stack
Painless upgrade of pixel series
redis和mysql区别
leetcode001--返回和为target的数组元素的下标
做数据可视化应该避免的8个误区
MySQL time function query
Spark optimization
Unity攝像頭跟隨鼠標旋轉
The object needs to add additional attributes. There is no need to add attributes in the entity. The required information is returned
io. Platform. packageRoot; // ignore: deprecated_ Member_ use
Recommended scheme for national production of electronic components of wireless keyboard
MySQL queries users logged in for at least N consecutive days
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
Spark small case - RDD, spark SQL