当前位置:网站首页>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
边栏推荐
- JS determines whether the numeric string contains characters
- Leetcode001 -- returns the subscript of the array element whose sum is target
- 数据孤岛是什么?为什么2022年仍然存在数据孤岛?
- leetcode003--判断一个整数是否为回文数
- Leetcode002 -- inverts the numeric portion of a signed integer
- Unity攝像頭跟隨鼠標旋轉
- 解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
- Introduction to raspberry pie 3B - system installation
- Painless upgrade of pixel series
- 持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
猜你喜欢
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
Summary of MySQL de duplication methods
Painless upgrade of pixel series
Detailed explanation of the differences between TCP and UDP
Excel uses the functions of replacement, sorting and filling to comprehensively sort out financial data
Learning Android II from scratch - activity
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
C language: Advanced pointer
Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)
随机推荐
负载均衡简介
List&lt; Map&gt; Replication: light copy and deep copy
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
Case of using stream load to write data to Doris
Introduction to raspberry pie 3B - system installation
Leetcode005 -- delete duplicate elements in the array in place
selenium模式下切换窗口,抓取数据的实现
leetcode005--原地删除数组中的重复元素
js 判断数字字符串中是否含有字符
Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
Leetcode - > 1 sum of two numbers
Open the past and let's start over.
敏捷实践 | 提高小组可预测性的敏捷指标
Sword finger offer: push in and pop-up sequence of stack
Small volume Schottky diode compatible with nsr20f30nxt5g
Progress of innovation training (III)
Unity RawImage背景无缝连接移动
Innovation training (10)
Leetcode009 -- search the target value in the array with binary search
Recommended scheme of national manufactured electronic components