1 solutions

  • 1
    @ 2025-2-3 20:26:02
    #include <bits/stdc++.h>
    
    using namespace std;
    //首发题解+年后复苏第一题,差点崩了!!!!!
    int n, k;
    bool vis[3001][3001];
    char a[3001][3001];
    struct node {
    	int x, y, t;//x,y记录坐标,t记录时间
    };
    const int dx[8] = {0, 0, 1, -1, -1, 1, -1, 1};
    const int dy[8] = {1, -1, 0, 0, -1, 1, 1, -1};
    //int nck[3]={2,1,0};这个*数组硬控30分钟阳寿
    //废话不多说,开始正片
    int bfs(int x, int y) {
    	//标准BFS开头
    	queue<node> q;
    	q.push({x, y, 0});
    	vis[x][y] = 1;
    	while (!q.empty()) {
    		node fg = q.front();
    		int cx = fg.x;
    		int cy = fg.y;
    		q.pop();
    		if (cx == n - 2 && cy == n - 2)
    			return fg.t;
    		q.push({cx, cy, fg.t + 1});//重点!站着不动也加进去(此处硬控10分钟阳寿)
    		for (int i = 0; i < 4; i++) {//正常走地图
    			int nx = dx[i] + cx;
    			int ny = dy[i] + cy;
    			int fas;
    			//fas=nck[(fg.t)/k];万恶之源
    			//以自身为中心
    			//5*5范围
    			//0 0 0 0 0
    			//0 0 0 0 0
    			//0 0 1 0 0
    			//0 0 0 0 0
    			//0 0 0 0 0
    			//3*3范围
    			//0 0 0
    			//0 1 0
    			//0 0 0
    			if (fg.t < k)
    				fas = 2;
    			else if (fg.t < 2 * k)
    				fas = 1;
    			else
    				fas = 0;
    			//判断是否合法
    			if (nx + fas > n || nx - fas < 1 || ny + fas > n || ny - fas < 1 || vis[nx][ny] == 1) {
    				continue;
    			}
    			bool fld = true;
    			//判断以自身为中心的5*5或3*3或1*1范围内是否有障碍物
    			if (fas != 0) {
    				for (int j = nx - fas; j <= nx + fas; j++) {
    					for (int k = ny - fas; k <= ny + fas; k++) {
    						if (a[j][k] != '+') {
    							fld = false;
    						}
    					}
    				}
    			} else {
    				if (a[nx][ny] != '+') {
    					fld = false;
    				}
    			}
    			if (fld != true)
    				continue;
    			//加入队列
    			vis[nx][ny] = 1;
    			q.push({nx, ny, fg.t + 1});
    		}
    	}
    	return -1;
    }
    int main() {
    	cin >> n >> k;
    	for (int i = 1; i <= n; i++) {
    		for (int j = 1; j <= n; j++) {
    			cin >> a[i][j];
    		}
    	}
    	cout << bfs(3, 3);
    
    	return 0;
    }
    
    • 1

    Information

    ID
    995
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    8
    Tags
    # Submissions
    33
    Accepted
    6
    Uploaded By