Loading

2022.10.21 总结

1. 逐月 P5081

逐月 P5081

题意

有一个 \(n \times m\) 的镜子矩阵,每个镜子要么是 \,要么是 /。

有一道光从镜子牧场外面射了进来,请你求出这道光最多能折射多少次。

思路

100 分

按题意模拟,枚举从哪个镜子照射进去,暴力做折射。

时间复杂度

每个镜子最多被折射两次,\(O(n \times m)\)

空间复杂度

数组记录镜子矩阵,\(O(n \times m)\)

代码

#include <bits/stdc++.h>

using namespace std;

const int N = 1010, M = 1010;

const int dir1[] = {1, 0, 3, 2};
const int dir2[] = {3, 2, 1, 0};

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int n, m, cnt;
char c[N][M];

void dfs(int ct, int x, int y, int lf){
  if (x < 1 || x > n || y < 1 || y > m) {
    cnt = max(cnt, ct - 1);
    return ;
  }
  if (c[x][y] == '/') {
  	int d = dir1[lf];
  	dfs(ct + 1, x + dx[d], y + dy[d], d);
  } else {
  	int d = dir2[lf];	
  	dfs(ct + 1, x + dx[d], y + dy[d], d);
  }
}

int main(){
  freopen("mirror.in", "r", stdin);
  freopen("mirror.out", "w", stdout);
  cin >> n >> m; 
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    dfs(1, i, m, 3);
    dfs(1, i, 1, 1);
  }
  for (int i = 1; i <= m; i++) {
    dfs(1, 1, i, 2);
    dfs(1, n, i, 0);
  }
  cout << cnt;
  return 0;
}
posted @ 2023-03-02 22:43  chengning0909  阅读(12)  评论(0编辑  收藏  举报