1 条题解

  • 0
    @ 2024-6-6 9:10:17

    image

    #include <cstring>
    #include <cstdio>
    #include <queue>
    #include <cmath>
    using namespace std;
    
    int xx[] = {1, -1, -1, 1}, yy[] = {-1, 1, -1, 1};
    struct point {
    	int x, y, ans;
    } ;
    int vis[510][510];
    char s[510][510];
    int n, m, t;
    
    bool check(int x, int y) {
    	if (x < 1 || n + 1 < x || y < 1 || m + 1 < y)	return 1;
    	if (vis[x][y])	return 1;
    }
    
    void bfs() {
    	deque<point> que;
    	que.push_back({1, 1, 0});
    	while (!que.empty()) {
    		point now = que.front();
    		que.pop_front();
    		int x = now.x, y = now.y, ans = now.ans;
    		if (x == n + 1 && y == m + 1) {
    			printf("%d\n", ans);
    			break;
    		}
    		if (check(x, y))	continue;
    		vis[x][y] = 1;
    		if (s[x][y - 1] == '/') {
    			que.push_front({x + 1, y - 1, ans});
    		} else {
    			que.push_back({x + 1, y - 1, ans + 1});
    		}
    		if (s[x - 1][y] == '/') { 
    			que.push_front({x - 1, y + 1, ans});
    		} else {
    			que.push_back({x - 1, y + 1, ans + 1});
    		}
    		if (s[x - 1][y - 1] == '\\') {
    			que.push_front({x - 1, y - 1, ans});
    		} else {
    			que.push_back({x - 1, y - 1, ans + 1});
    		}
    		if (s[x][y] == '\\') {
    			que.push_front({x + 1, y + 1, ans});
    		} else {
    			que.push_back({x + 1, y + 1, ans + 1});
    		}
    	}
    } 
    
    int main() {
    	scanf("%d", &t);
    	while (t--) {
    		memset(s, 0, sizeof s);
    		memset(vis, 0, sizeof vis);
    		scanf("%d%d", &n, &m);
    		for (int i = 1; i <= n; ++i) {
    			scanf("%s", s[i] + 1);
    		}
    		bfs();
    		if (abs(m - n) % 2) { 
    			printf("NO SOLUTION\n");
    			continue;
    		}
    	} 
    	return 0;
    }
    
    • 1

    信息

    ID
    792
    时间
    3000ms
    内存
    512MiB
    难度
    10
    标签
    递交数
    3
    已通过
    0
    上传者