6 条评论

  • @ 2026-8-5 21:43:02
    #include <bits/stdc++.h>
    using namespace std;
    typedef pair<int, int> PII;
    const int N = 110;
    char c[N][N];
    int n, m, tim[N][N], dist[N][N];
    int dx[] = {1, -1, 0, 0};
    int dy[] = {0, 0, 1, -1};
    // tim[i][j]:求火源到(i,j)的时间
    // dist[i][j]:起点到达(i,j)的时间
    bool st[N][N];
    void bfs(int sx, int sy) {
        memset(st, false, sizeof st);
        queue<PII> q;
        q.push({sx, sy});
        st[sx][sy] = true;
        tim[sx][sy] = 0;
        while (q.size()) {
            auto it = q.front();
            q.pop();
            int x = it.first, y = it.second;
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i], ny = y + dy[i];
                if (nx < 1 || nx > n || ny < 1 || ny > m)
                    continue;
                if (st[nx][ny] || c[nx][ny] == '#')
                    continue;
                st[nx][ny] = true;
                tim[nx][ny] = tim[x][y] + 1;
                q.push({nx, ny});
            }
        }
    }
    void bfs2(int sx, int sy) {
        memset(st, false, sizeof st);
        queue<PII> q;
        q.push({sx, sy});
        st[sx][sy] = true;
        dist[sx][sy] = 0;
        while (q.size()) {
            auto it = q.front();
            q.pop();
            int x = it.first, y = it.second;
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i], ny = y + dy[i];
                if (nx < 1 || nx > n || ny < 1 || ny > m)
                    continue;
                if (st[nx][ny] || c[nx][ny] == '#' || dist[x][y] + 1 >= tim[nx][ny])
                    continue;
                st[nx][ny] = true;
                dist[nx][ny] = dist[x][y] + 1;
                q.push({nx, ny});
            }
        }
    }
    int main() {
        int t;
        cin >> t;
        while (t--) {
            cin >> n >> m;
            int sx, sy, ex, ey; // 起点坐标和终点坐标
            int fx, fy;         // 火源坐标
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++) {
                    cin >> c[i][j];
                    if (c[i][j] == 'S') {
                        sx = i, sy = j;
                    }
                    if (c[i][j] == 'E') {
                        ex = i, ey = j;
                    }
                    if (c[i][j] == 'F') {
                        fx = i, fy = j;
                    }
                }
            }
            bfs(fx, fy);
            bfs2(sx, sy);
            if (st[ex][ey]) {
                cout << "YES" << '\n';
            } else {
                cout << "NO" << '\n';
            }
        }
        return 0;
    }
    
    • @ 2026-8-5 21:38:46
      #include <bits/stdc++.h>
      using namespace std;
      typedef pair<int, int> PII;
      const int N = 510;
      int a[N][N], dist[N][N];
      int n, m, k;
      bool st[N][N];
      int dx[] = {1, -1, 0, 0};
      int dy[] = {0, 0, 1, -1};
      bool check(int mid) {
          memset(st, false, sizeof st);
          memset(dist, 0, sizeof dist);
          queue<PII> q;
          q.push({1, 1});
          dist[1][1] = 0;
          st[1][1] = false;
          while (q.size()) {
              auto it = q.front();
              q.pop();
              int x = it.first, y = it.second;
              for (int i = 0; i < 4; i++) {
                  int nx = x + dx[i], ny = y + dy[i];
                  if (nx < 1 || nx > n || ny < 1 || ny > m)
                      continue;
                  // 判断是否走过 以及 mid等级是否够用
                  if (st[nx][ny] || a[nx][ny] > mid)
                      continue;
                  dist[nx][ny] = dist[x][y] + 1;
                  st[nx][ny] = true;
                  q.push({nx, ny});
              }
          }
          if (st[n][m] && dist[n][m] <= k) {
              return true;
          }
          return false;
      }
      int main() {
          cin >> n >> m >> k;
          for (int i = 1; i <= n; i++) {
              for (int j = 1; j <= m; j++) {
                  cin >> a[i][j];
              }
          }
          int l = 0, r = 1e9, ans = -1;
          while (l <= r) {
              int mid = (l + r) / 2;
              if (check(mid)) {
                  ans = mid;
                  r = mid - 1;
              } else {
                  l = mid + 1;
              }
          }
          cout << ans << '\n';
          return 0;
      }
      
      • @ 2026-8-5 19:17:18
        #include <bits/stdc++.h>
        using namespace std ;
        
        struct Pos {
        	int _x ;
        	int _y ;
        };
        
        const int LEN = 1e3 + 10 ;
        int place [LEN][LEN] = {};
        bool is_lev [LEN][LEN]= {};
        int ans [LEN][LEN] = {};
        
        int n , m , k ;
        queue <Pos> squ_pos ;
        
        int dx[] = {1,-1,0,0};
        int dy[] = {0,0,1,-1};
        
        void Bfs (int need_x , int need_y) {
        	squ_pos.push({1,1}) ;
        	is_lev[1][1] = true ;
        //	ans[1][1] = 1 ;
        	
        	while (squ_pos.size()) {
        		Pos now_squ = squ_pos.front() ;
        		squ_pos.pop() ;
        		
        		for (int i = 0 ; i < 4 ; i++) {
        			int to_x = now_squ._x + dx[i] ;
        			int to_y = now_squ._y + dy[i] ;
        			
        			
        			if ( (to_x < 1) || (to_x > n) || (to_y < 1) || (to_y > m) )
        				continue ;
        			
        			if (place[to_x][to_y]-place[now_squ._x][now_squ._y] > k || is_lev[to_x][to_y])
        				continue ;
        			
        			ans[to_x][to_y] = ans[now_squ._x][now_squ._y] + 1 ;
        			
        			if (to_x == n && to_y == m){
        				cout << ans[to_x][to_y] ;
        				return ;
        			}
        			
        			squ_pos.push({to_x , to_y}) ;
        			is_lev[to_x][to_y] = true ;
        		}
        	}
        	
        	cout << "-1" ;
        	return ;
        }
        
        int main() {
        	
        	cin >> n >> m >> k ;
        	
        	for (int i = 1 ; i <= n ; i++){
        		for (int j = 1 ; j <= m ; j++){
        			cin >> place[i][j] ;
        		}
        	}
        	
        	Bfs(1,1) ;
        	
        	return 0 ;
        }
        
        🤔 2
        👎 1
        👀 1
        • @ 2026-8-2 21:06:00

          • @ 2026-8-2 21:05:15

            • @ 2026-8-2 21:04:33
              #include <bits/stdc++.h>
              using namespace std;
              int ans = 0;
              set<pair<int, int>> s;
              void black(int l, int r) {
                  auto it = s.lower_bound({l, -1e9});
                  if (it != s.begin()) {
                      auto pre = it;
                      --pre;
                      if (pre->second >= l - 1) {
                          it = pre;
                      }
                  }
                  while (it != s.end() && it->first <= r + 1) {
                      l = min(l, it->first);
                      r = max(r, it->second);
                      ans -= it->second - it->first + 1;
                      it = s.erase(it);
                  }
                  s.insert({l, r});
                  ans += (r - l + 1);
              }
              void white(int l, int r) {
                  auto it = s.lower_bound({l, -1e9});
                  if (it != s.begin()) {
                      auto pre = it;
                      pre--;
                      if (pre->second >= l) {
                          it = pre;
                      }
                  }
                  while (it != s.end() && it->first <= r) {
                      int a = it->first;
                      int b = it->second;
                      ans -= b - a + 1;
                      it = s.erase(it);
                      if (a < l) {
                          s.insert({a, l - 1});
                          ans += l - a;
                      }
                      if (b > r) {
                          s.insert({r + 1, b});
                          ans += b - r;
                          break;
                      }
                  }
              }
              int main() {
                  int q;
                  cin >> q;
                  while (q--) {
                      int op, l, r;
                      cin >> op;
                      if (op == 1) {
                          cin >> l >> r;
                          black(l, r);
                      } else if (op == 2) {
                          cin >> l >> r;
                          white(l, r);
                      } else {
                          cout << ans << '\n';
                      }
                  }
                  return 0;
              }
              
              👍 4
              🤔 3
              👎 2
              🤡 2
              • 1