应该是O(m)的,但为什么TLE
查看原帖
应该是O(m)的,但为什么TLE
156353
GoldenFishX楼主2020/12/20 16:16

记录

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 5;
struct Node{
  int x, y;
};
vector<Node> a[MAXN][MAXN]; 
bool vis[MAXN][MAXN];
int dx[4] = {0,0,1,-1}, dy[4] = {1,-1,0,0};
int n, m, ans = 0;
void bfs() {
  queue<Node> q, q1;
  Node x1;
  x1.x = 1, x1.y = 1;
  q.push(x1);
  vis[1][1] = 1;
  while(!q.empty()) {
    Node x = q.front();
    q.pop();
    for(int i = 0; i < a[x.x][x.y].size(); i++) {//开灯 
	  x1.x = a[x.x][x.y][i].x, x1.y = a[x.x][x.y][i].y;
	  ans++;
	  q1.push(x1);
	}
	while(!q1.empty()) {//看哪个开灯的房间可以去 
	  for(int i = 0; i < 4; i++) {//周围有走过的房间的开灯房间就一定能去 
	  	int nx = q1.front().x + dx[i], ny = q1.front().y + dy[i];
	    if(nx >= 1 && ny >= 1 && nx < n && ny < n && vis[nx][ny] == 1) {
		  vis[q1.front().x][q1.front().y] = 1;
		  //cout << q1.front().x << " " << q1.front().y << endl;
		  Node xxx = {q1.front().x, q1.front().y};
		  q.push(xxx);
		  q1.pop();
		  break; 
		}
	  }
	}
  }
}
int main(){
  //freopen("lightson.in","r",stdin);
  //freopen("lightson.out","w",stdout);
  cin >> n >> m;
  for(int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    Node x;
    x.x = x2,x.y = y2;
    a[x1][y1].push_back(x);
  }
  bfs();
  cout << ans << endl;
  /*
  for(int i = 0; i <= n; i++) {
    for(int j = 0; j <= n; j++) {
	  cout << vis[i][j] << " ";
	}
	cout << endl;
  }*/
  return 0;
}


2020/12/20 16:16
加载中...