样例没过,求调!!
查看原帖
样例没过,求调!!
519573
Daniel_yao楼主2022/3/31 12:33
#include<bits/stdc++.h>
using namespace std;
const int N = 305;

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

struct node{
  int x, y;
}f[27][2];

int n, m;
int cx, cy, fx, fy, ans;
char a[N][N];

void dfs(int x, int y, int step){
  if(x < 1 || x > n || y < 1 || y > n || a[x][y] == '#'){
    return ;
  }
  if(a[x][y] >= 'A' && a[x][y] <= 'Z'){
    if(f[a[x][y]][0].x == x && f[a[x][y]][0].y == y){
      dfs(f[a[x][y]][1].x, f[a[x][y]][1].y, step);
    }
    else{
      dfs(f[a[x][y]][0].x, f[a[x][y]][0].y, step);
    }
  }
  if(x == fx && y == fy){
    ans = max(ans, step);
    return ;
  }
  for(int i = 0;i < 4;i++){
    dfs(x + dx[i], y + dy[i], step + 1);
  }
}

int main(){
  cin >> n >> m;
  for(int i = 1;i <= n;i++){
    for(int j = 1;j <= m;j++){
      cin >> a[i][j];
      if(a[i][j] == '='){
        fx = i, fy = j;
      }
      if(a[i][j] == '@'){
        cx = i, cy = j;
      }
      if(a[i][j] <= 'A' && a[i][j] >= 'Z'){
        if(!f[a[i][j]][0].x && !f[a[i][j]][0].y){
          f[a[i][j]][0].x = i, f[a[i][j]][0].y = j;
        } else {
          f[a[i][j]][1].x = i, f[a[i][j]][1].y = j;
        }
      }
    }
  }
  dfs(cx, cy, 0);
  cout << ans << endl;
}

2022/3/31 12:33
加载中...