当前位置:网站首页>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
边栏推荐
- /etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
- Progress of innovation training (III)
- 使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
- IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
- Leetcode - > 1 sum of two numbers
- 简单的拖拽物体到物品栏
- Custom switch control
- Practice and exploration of knowledge map visualization technology in meituan
- Spark small case - RDD, broadcast
- Programmers complain: I really can't live with a salary of 12000. Netizen: how can I say 3000
猜你喜欢

DIY 一个 Excel 版的子网计算器

AQS源码阅读

Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience

Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!

Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)

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

Installation and deployment of Flink and wordcount test

Teach you how to build the ruoyi system by Tencent cloud

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

Windows remote connection to redis
随机推荐
[WinUI3]編寫一個仿Explorer文件管理器
Open the past and let's start over.
C list field sorting contains numbers and characters
List&lt; Map&gt; Replication: light copy and deep copy
Download PDF from HowNet (I don't want to use CAJViewer anymore!!!)
Simply drag objects to the item bar
Spark small case - RDD, spark SQL
PHP 统计指定文件夹下文件的数量
Learning Android II from scratch - activity
Unity camera rotation with sliding effect (rotation)
Raspberry pie + opencv + opencv -- face detection ------- environment construction
The last day of 2021 is the year of harvest.
Jetpack -- lifecycle usage and source code analysis
Leetcode004 -- Roman numeral to integer
Leetcode008 -- implement strstr() function
信息学奥赛一本通 1212:LETTERS | OpenJudge 2.5 156:LETTERS
Wechat payment function
What is the meaning of load balancing
Small volume Schottky diode compatible with nsr20f30nxt5g
MySQL - index