AcWing每日一题-2022-4-20
题目链接:https://www.acwing.com/problem/content/1931/
个人题解:


点击查看代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};//分别是:向上,向右,向下,向左
char g[1010][1010];
int ans, n, m;
void dfs(int x, int y, int d, int u)
{
ans = max(ans, u);
int nd;
if(g[x][y] == '/') nd = d ^ 1;
else nd = d ^ 3;
int nx = x + dx[nd], ny = y + dy[nd];//下一个位置
if(nx && ny && nx <= n && ny <= m)//如果下一个位置依旧在规定位置里
dfs(nx, ny, nd, u + 1);
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> g[i][j];
for(int i = 1; i <= m; i++){
dfs(1, i, 2, 1);//最上面的一行,入射方向向下
dfs(n, i, 0, 1);//最下面的一行,入射方向向上
}
for(int i = 1; i <= n; i++){
dfs(i, 1, 1, 1);//最左边的一行,入射方向向右
dfs(i, m, 3, 1);//最右边的一行,入射方向向左
}
cout << ans;
return 0;
}

浙公网安备 33010602011771号