当前位置:网站首页>Informatics Aosai yibentong 1212: letters | openjudge 2.5 156: Letters
Informatics Aosai yibentong 1212: letters | openjudge 2.5 156: Letters
2022-04-23 04:51:00 【Jun Yi_ noip】
【 Topic link 】
ybt 1212:LETTERS
OpenJudge 2.5 156:LETTERS
【 Topic test site 】
1. Deep search
set up bool type vis Array , The length is 128.vis[i]
True means ascii Code for i The letters of have been visited .
Set variables step Record the number of letters that have been accessed ,mx Record the maximum number of letters accessed .
Every time you traverse to a location , Search its four positions up, down, left and right , If this location is on the map , And the character in this position has not been accessed , Then change the access status of letters , Include vis Array and step Variable ( Update here step Maximum mx), Then search this new location . After searching , Remember to restore the state .
The specific writing of deep search , Have access to... Before function call , And access in function calls , The only difference is whether the position passed in as a parameter is in the accessed state inside the function .
【 Solution code 】
How to write it 1: Access... Before a function call
#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)// Access... Before a function call
{
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;// Update status
step++;
mx = max(step, mx);
dfs(x, y);
step--;// State restoration
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;// Access the initial location
step = 1;
mx = 1;
dfs(1,1);
cout << mx;
return 0;
}
How to write it 2: Access... Within a function call
#include <bits/stdc++.h>
using namespace std;
#define N 25
int r, s, step, mx;
char mp[N][N];// Map
bool vis[128];//vis[i]:ascii Code for i Whether the letters of have been accessed
int dir[4][2] = {
{
0,1},{
0,-1},{
-1,0},{
1,0}};// Direction array
void dfs(int sx, int sy)// Access... Within a function call
{
if(sx >= 1 && sx <= r && sy >= 1 && sy <= s && vis[mp[sx][sy]] == false)// If (sx,sy) In the map and the letters at that location have not been visited
{
vis[mp[sx][sy]] = true;// Access the letter
step++;// Move steps plus 1
mx = max(step, mx);// Update Max
for(int i = 0; i < 4; ++i)// Search up, down, left and right
{
int x = sx + dir[i][0], y = sy + dir[i][1];
dfs(x, y);
}
step--;// State restoration
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;
}
版权声明
本文为[Jun Yi_ noip]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230449051302.html
边栏推荐
- Learning Android from scratch -- baseactivity and activitycollector
- MySQL - data read / write separation, multi instance
- leetcode009--用二分查找在数组中搜索目标值
- selenium模式下切换窗口,抓取数据的实现
- js 判斷數字字符串中是否含有字符
- Unity camera rotation with sliding effect (rotation)
- Unity3D 实用技巧 - 理论知识库(一)
- The programmer starts the required application with one click of window bat
- Teach you how to build the ruoyi system by Tencent cloud
- 【数据库】MySQL多表查询(一)
猜你喜欢
Detailed explanation of the differences between TCP and UDP
Wechat payment function
Pixel 5 5g unlocking tutorial (including unlocking BL, installing edxposed and root)
【数据库】MySQL基本操作(基操~)
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
PIP3 installation requests Library - the most complete pit sorting
Mysql50 basic exercises
What is a data island? Why is there still a data island in 2022?
Spark small case - RDD, spark SQL
Innovative practice of short video content understanding and generation technology in meituan
随机推荐
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
DIY 一个 Excel 版的子网计算器
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
Unity摄像头跟随鼠标旋转
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
leetcode002--将有符号整数的数字部分反转
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
View analysis of scenic spots in ArcGIS
MySQL - index
Repair of self calibration SPC failure of Tektronix oscilloscope dpo3054
Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
Solutions to the failure of sqoop connection to MySQL
Excel uses the functions of replacement, sorting and filling to comprehensively sort out financial data
Wine (COM) - basic concept
Recommended scheme for national production of electronic components of wireless keyboard
PHP 统计指定文件夹下文件的数量
Arduino UNO r3+LCD1602+DHT11
Innovation training (IX) integration
Innovation training (VI) routing
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)